Skip to main content

A Hello World Discord bot

In this article, I will show you how to create a flow function that responds to events from external services. This flow function is a Discord bot. If you send the bot a private message, it will echo the message back to you.


  1. A GitHub account to log into the platform. It's free.
  2. A Discord server that you have permission to add a bot.

Prepare the source code

For this tutorial, we already created a repo named hello-world-discord for you to fork.

Import, build and deploy

Click here to import your forked source code repo for the flow function into

For this flow function, we need to add a Discord token in settings so that it can listen for messages from a specific Discord bot. Please refer to How to create a Discord chat bot on how to get your Discord token. Click on the Advanced link to configure the settings.

discord_tokenCopied from Discord Developer Portal

We put the Discord token in the settings as opposed to in the source code for two reasons. First, we keep the token secure by keeping it out of the public GitHub repos. Second, we make it easy to reuse the flow function source code.

Next, click on the Build and Deploy buttons to create the flow funtion.

Finally, you will be redirected to the flow details page, where you can check for service status and logs. Discord should appear as a connected external service on this page.

Test it!

When the status of the flow is ready and running, you can invite the Discord bot to your sever. Refer to this guide to invite the bot to your server.

After the bot joined your server, you can find the bot on the right contact list and DM the bot. The bot will simply repeat what you just say.

Code walkthrough

The source code for the flow function is written in the Rust programming language. The on_deploy() function is called by the platform when the flow is first deployed. We create a bot from the discord_token and then call bot.listen_to_messages() to start up a listener in to listen for incoming messages from the bot.

pub async fn on_deploy() {
let token = std::env::var("discord_token").unwrap();
let bot = ProvidedBot::new(token);

The handler() function is annotated with #[message_handler], which is a macro defined in the Discord SDK. It is called by the platform whenever the bot listener receives a Discord message. The platform passes the Message struct, also defined in the Discord SDK, to the handler() function.

async fn handler(msg: Message) {
let token = std::env::var("discord_token").unwrap();
let bot = ProvidedBot::new(token);
let discord = bot.get_client();

if {
log::debug!("ignored bot message");
if msg.member.is_some() {
log::debug!("ignored channel message");

let channel_id = msg.channel_id;
let resp = format!("Welcome to\nYou just said: '{}'.\n", msg.content);

_ = discord.send_message(
"content": resp

The platform retrieves the Rust source code from your GitHub repo and then compiles it for you. Of course, you can also compile it locally by installing the Rust compiler toolchain and then run the command.

cargo build --target wasm32-wasi --release

Read more

Tutorials and sample code for related flow functions. Click on the Deploy link to configure and deploy your own flow without writing any code from a template.