Skip to content

Slack bot

Time to complete: 10 minutes

This guide walks you through building a Slack bot that responds to mentions in channels.

If you just want to send simple Slack messages without interactivity, check out our Slack webhook guide. And if you want an AI agent Slack bot, see our Slack agent guide.

  1. Remix this val to get started:

View and run this example on Val Town

export const slackReplyToMessage = async (req: Request) => {
// Handle different request methods
if (req.method === "GET") {
return new Response("Slack bot is running!", { status: 200 });
}
// Parse JSON body safely
let body;
try {
const text = await req.text();
if (!text) {
return new Response("Empty request body", { status: 400 });
}
body = JSON.parse(text);
} catch (error) {
console.error("JSON parsing error:", error);
return new Response("Invalid JSON", { status: 400 });
}
// Verify the request is genuine
if (body.token !== Deno.env.get("slackVerificationToken")) {
return new Response(undefined, { status: 401 });
}
// Respond to the initial challenge (when events are enabled)
if (body.challenge) {
return Response.json({ challenge: body.challenge });
}
// Reply to app_mention events
if (body.event?.type === "app_mention") {
try {
const result = await fetch("https://slack.com/api/chat.postMessage", {
method: "POST",
headers: {
"Authorization": `Bearer ${Deno.env.get("slackToken")}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
channel: body.event.channel,
thread_ts: body.event.ts,
text: "Hello, ~World~ from Val Town!",
}),
});
const responseData = await result.json();
console.log(responseData);
// Return proper response to acknowledge receipt
return new Response(undefined, { status: 200 });
} catch (error) {
console.error("Error posting to Slack:", error);
return new Response(undefined, { status: 500 });
}
}
// Return 200 for other event types
return new Response(undefined, { status: 200 });
};
  1. Copy the HTTP endpoint URL (via the … menu for main.tsx) - this is where Slack will send events to your Val Town bot.
  1. Go to Slack API and create a new app From Scratch
  2. Name your app and select your workspace

Create Slack app

In your val’s sidebar, add these Environment Variables:

  • slackVerificationToken: From Settings → Basic Information → Verification Token
  • slackToken: Leave empty (you’ll fill this after installing)

Verification token

  1. Go to Features → Event Subscriptions
  2. Enable events and paste your val’s HTTP endpoint in Request URL
  3. Subscribe to app_mention under Subscribe to bot events

Event subscriptions

In OAuth & Permissions → Scopes, add:

  • app_mentions:read (should already be there)
  • chat:write

Bot scopes

Go to Settings → Install App and install to your workspace.

Install app

  1. Copy the Bot User OAuth Token from OAuth & Permissions
  2. Update your val’s slackToken environment variable

OAuth token

  1. Invite the bot to a channel Invite bot
  2. Mention the bot - it will reply! Bot reply

You can find more Slack examples on our Templates page.