Squishy Software Series

Much to Think A-Bot: Extending Discord with Wasm

October 22, 2024
Chris Dickinson

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

Grow with customer use cases

Empower your users to customize your product precisely for them, while you focus on the core. A plugin system maximizes product flexibility to meet users' dynamic needs, so your product grows with your customers.

Meeting customers' needs →

Reduce churn & increase usage

Product adoption can be a double-edged sword. How do you prioritize a tidal wave of feature requests? Give users the freedom to make your product do more for them on their own timeline, and they'll stick around.

Improve customer retention →

Reclaim control of your roadmap

When customers can self-serve entire features, you can get back to building the vision. Take back valuable engineering time & innovate on your product.

On the road to roadmap freedom →

Deeper & advanced integrations

Going beyond the HTTP API, running customer code directly enables advanced use-cases compared to Webhooks & other system integration techniques.

More integration possibilities →

Pricing

Free

Create your proof-of-concept, integrating XTP into your app at no cost.

Enjoy full access to the XTP platform, limited by the number of apps, extension points, and plugins that can be created.

Full access, managing up to:

  • 1 App
  • 1 Team (up to 10 members)
  • 10 Authorized Guests (to push plugins)
  • 2 Extension Points
  • 100 Continuous plugin simulations (per month)

đź‘€ See what you can do with free XTP:

A quick intro to XTP - safely run user code in your program
Create a free account →

Enterprise

Fully white-label your implementation, contained entirely behind your system.
‍‍
Hands-on training, enablement, and onboarding provides your company with a solid path to integration success.

Meets advanced scenarios with:

  • Custom plan, fit to your needs
  • Tailored training sessions & assistance
  • Predictable, set pricing
  • Uptime SLA + up to 24/7 direct support
  • Private cloud & object storage integration
  • Access to compliance attestations including: SOC 2 Type 2, ISO 27001, & GDPR

Talk to our team to learn more about how best to leverage XTP in your product or platform.
‍‍

Contact Sales →
Custom

Leverage next-gen technology

Use XTP’s powerful plugin execution engine to easily and securely run customer code, directly within your application. Let us handle the storage, validation, distribution and monitoring all while presenting a top-notch experience for plugin developers.

Join the Waitlist
Have questions?
Connect with us!