After each big milestone on XTP, we take a bit of time to kick the tires on what we’ve built. It’s a chance to take a look at the service with fresh eyes– to switch contexts and find ways to make XTP more frictionless and powerful. And, of course, it’s a chance to build something useful for ourselves with the service! This demo week, I’d like to introduce something that Charles, Zach, Muhammad and I built: extendabot.
Why a chat bot? Well, as our Dylibso & Extism Discord grows we have a number of household tasks that would be nice to automate: announcements, polls, knowledge bases, etc. (Have you joined our Discord? You should! It’s fun! Lots of cool, stylish people there.) But more importantly, building a chatbot and deploying it to our community Discord has the following properties:
- If you’re in our Discord, chances are that you have some experience with Extism and Wasm– so this gives you a chance to see how easy XTP makes the guest plugin development experience.
- It’s a human-scale data stream. XTP scales up to high-volume, low-latency datastreams –some colleagues worked on a Kafka integration demo along those lines– but if you’re just getting a feel for the service, it’s handy to work with a slower, lower-volume data stream.
- There are stakes: deploying a host bot to our community server means committing to moderating the bot and making sure bad actors can’t abuse the plugin architecture. This gives us a chance to build a public demo that shows how XTP helps you build a secure multi-tenant service.
At this point, you’re probably wondering: how do I get started? I’M GLAD YOU ASKED:
- Join our Extism & Dylibso Discord server. It’s free, fun, and full of helpful, friendly people!
- Run
/extendabot signup
. You’ll be given a guest invite link. Click it and sign in using GitHub. - Think of a message pattern you want to listen for. Run
/extendabot listen-for <some regex>
. You’ll be given a plugin name to use! (You can also provide a name if you want.) - Download the xtp cli, run xtp auth login, then
xtp plugin init —name <the name you were given>
. You’ll be guided through app & extension point selection and a basic chat plugin app will be scaffolded for you.
Now that you have a plugin, open up the main file. For TypeScript plugins, this will be src/main.ts
, other languages will vary – src/lib.rs
, main.go
, etc. Your plugin receives events. Those events correspond to different registered “interests” – the most fundamental of which is “message”. Extendabot will call your plugin with a message event when someone sends a message matching your registered pattern. When you receive a “message” event, the message content, id, channel, and sender will be available to you in the event.message
object. You can then send messages to the channel with the sendMessage
host function, which may also reply to messages by using the reply: messageId
attribute. You can also watch a given message for replies or reactions using watchMessage
, or add your own reactions with react
. (React developers, sorry if you landed on this post by mistake. You might be interested in Reactables, though!)
import {
IncomingEvent,
OutgoingReaction,
Result,
OutgoingRequest,
OutgoingMessage,
} from "./pdk";
import { react, request, sendMessage, watchMessage } from "./pdk";
// For more on getting set up, see the examples directory in the github repo
// in discord #bots, run "/xtp listen-for hello hellobot"
export function handleImpl(input: IncomingEvent) {
// respond to "hello" with "hi"
if (input.kind === 'content') {
const message = sendMessage({
channel: input.channel,
message: 'hi',
reply: input.message!.id,
})
// watch our response for reactji!
watchMessage(message)
}
// say something nice if someone reacted
if (input.kind === 'watch:reaction:added') {
const message = sendMessage({
channel: input.channel,
message: 'thanks for reacting!',
reply: input.reaction!.message.id,
})
}
}
When you’re ready to test your plugin, run xtp plugin push
and head to the #bots channel. At first, your bot will be limited to this channel. You can ask us to add it to other channels once you’re ready! Write a message that trips your regex and wait for the results. If you run into trouble, ping us! We’re happy to share logs with you. Your bot will be limited to 500 milliseconds of runtime and ratelimiting quotas. (Each sendMessage
call costs 10 tokens, each watchMessage
call costs 100 tokens – see guests for a table of token costs.) Initially your bot will have a pretty conservative ratelimit quota set – you can ask us for more if you find yourself running into it. Additionally, you can ask us to allow-list certain domains so you can make http requests!
Finally, your bot has a persistent “brain” in the form of Extism Var.get/set
functions. Data stored here is persisted across function calls.
(See our whoabot for more! There are also other examples available!)
And the fun doesn’t stop there– you can run your own Extendabot in your own server. Instructions are provided on the README for provisioning needs. Your XTP account comes with the ability to create your own apps and extension points – so you can take the plugin.yaml definition from Extendabot and hack on your own version. Maybe even connect it to slack!
We’re excited to see what you build– and thanks again for being part of this community!
Chris Dickinson
Principal Engineer, Dylibso