Quickstart Composer Actions 101
Quickstart your Setup
Getting setup is as simple as running the command below. Simply copy & paste into your terminal.
We are cloning the dTech starter/example for Composer Actions which uses NextJS and getting the dependencies installed so we’re good to go.
You could deploy this publicly or test locally already. On deployment and testing in Warpcast you would see a Composer Action where on clicking the button you are sharing a predefined post.
Shipping custom Composer Actions
Let’s get Snake in there. It’s a fun game and I would love to play it from my social media feed and brag about no one beating my score!
Wait we can do that? Yes. Composer Actions aren’t limited to Forms where you input data and they then generate a new post for you, but that we can do as well.
So let’s start simple and move to Snake.
A simple post template form as Composer Action
A simple post template takes in some user choices and fills the post.
What we will do is ask for you three favorite ice cream flavors and publishing an engaging post showcasing that.
Of course you can build Issue templates for bug reports, customer feedback, input into your application, game interactions or much much more.
To start understand where to start look at the layout of the template
The API Route in compose.ts
defines what the Farcaster client (e.g. Warpcast) will show as Composer Action.
Looking at the code we see a type, title, url being returned. Which the client will use and show that URL to the user.
Additionally if there’s a GET request sent then we return the metadata about our Composer Action. This information is shown to users seeing our Action when browsing all registered actions or seeing previews.
Make sure the image is square, no more than 100x100px.
the other file src/pages/index.tsx
has our user interface. This is what the user sees.
The Farcaster client knows what to show by our API Route and the user flow.
Now remember this could be an external page as well. Then it’s just shown and if it doesn’t return a new cast well then you just have a mini app in the Farcaster client.
But we’re building an ice cream flavor picker to share as cast.
What we need to let the Farcaster client know that the user is done in our app and what the new cast is push the new cast.
This is done by the following code snippet in the example src/pages/index.tsx
.
The snippet pushes to the Farcaster client the JSON data and information that this “createCast” is our new post.
Now as seen in the sample this is part of a button so the user triggers it, though anyway we invoke this we prefill the cast and we can have embeds like images, videos and other media in there too!
To build our Form we now need to build a Form UI and prefill the cast text with the variables selected in our Form.
We will change the <p> Hello main test? </p>
part of the sample and exchange it with our form.
If you wish to test your user interface remember it is a full blown webpage.
npm run dev
and navigate to your browser.
What is missing? We have not adjusted our Submit Button to make sure that the ice cream flavors are reflected. Right now we still have the predefined message.
Let’s change that by first remembering what we picked, and then making sure that it is used in the template.
The following code is an excerpt, merge it with the above adjustment and you’re good.
First adjust the button to use a handleShare button and only work if the user selected all three of his favorite flavors.
Now we add the handleShare function above our return statement and below the flavors
Remember it’s just a webpage, so if we log the castText variable using console.log(castText);
before the window.parent… we can see in our browser at localhost that it works.
What is left? We need to deploy it
Deploying our cast action
Once you have it up (works anywhere you can deploy NodeJS), copy the link of your api endpoint (/api/compose) and paste that into the Warpcast Debugger
Type something in the cast composer and click Test Action.
You’ll see you working cast action producing the Ice Cream Flavors
Snake as Composer Action sharing Scores
Coming Soon!