new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

AI SDK · Cookbook · all subjects

slackbot/setup

18 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Slackbot Agent guide

The AI SDK provides a guide on how to build an AI Agent in Slack using the AI SDK.

Slack bot token scopes required

To set up a Slack app for the AI SDK slackbot, add these bot token scopes in OAuth & Permissions: app_mentions:read, chat:write, im:history, im:write, assistant:write.

Slack app event types handled

The slackbot handles three event types: app_mention (when bot is mentioned in channels), assistant_thread_started (when user starts a thread with the assistant), and message type with channel_type 'im' (direct messages to the bot).

waitUntil function for Slack 3-second timeout

Slack expects a response within 3 seconds to confirm request handling. Use the waitUntil function from @vercel/functions to wrap AI logic, allowing the API endpoint to respond immediately while processing continues asynchronously. This prevents duplicate LLM calls from Slack retries.

Slack API event verification flow

Slack sends a url_verification request with a challenge string. Check if payload.type is 'url_verification' and respond with payload.challenge and status 200. For event_callback type requests, verify the request using verifyRequest before processing the event.

handleNewAppMention function implementation

The handleNewAppMention function processes @mentions of the bot in channels. It skips messages from bots, creates a status updater that posts an initial 'thinking' message, retrieves thread history if the mention is in a thread, calls generateResponse with the messages, and updates the initial message with the AI response.

assistantThreadMessage function for thread initialization

When assistant_thread_started event is triggered, the assistantThreadMessage function posts a welcome message to the thread and sets suggested prompts using client.assistant.threads.setSuggestedPrompts with channel_id, thread_ts, and an array of prompt objects containing title and message fields.

handleNewAssistantMessage for direct message handling

The handleNewAssistantMessage function handles direct messages to the bot. It verifies the message is not from a bot, updates status to 'thinking', retrieves conversation history from getThread, calls generateResponse with the context, and posts the response using chat.postMessage with blocks formatting for Slack markdown.

Slack message filtering conditions

For message events, the slackbot processes only messages where: type is 'message', no subtype exists, channel_type is 'im', bot_id is falsy, bot_profile is falsy, and bot_id does not equal the slackbot's user ID.

generateResponse function with generateText

The generateResponse function uses the AI SDK's generateText with: a model, a system prompt instructing the bot to keep responses concise and not tag users with current date context, and messages array. It converts markdown link format [text](url) to Slack format <url|text> and ** to * for bold.

Tool implementation for weather and web search

Two tools added to generateResponse: getWeather tool takes latitude, longitude, city and calls open-meteo API returning temperature, weatherCode, humidity, city; searchWeb tool takes query and optional specificDomain and calls exa.searchAndContents returning array of results with title, url, snippet fields.

stopWhen with isStepCount for multi-step agentic flow

Set stopWhen: isStepCount(10) in generateText to enable multi-step conversations. This automatically sends tool results back to the LLM to trigger additional tool calls or responses, converting a one-off LLM call into a multi-step agentic flow.

Slack markdown format conversion in responses

Convert markdown to Slack mrkdwn format by replacing [text](url) patterns with <url|text> and ** with * for bold text before posting to Slack.

Event subscription setup for Slack app

In Slack's Event Subscriptions page, enable events and point to the deployment URL (e.g., https://your-vercel-url.vercel.app/api/events). Subscribe to three event types: app_mention, assistant_thread_started, and message:im.

Deployment environment variables for slackbot

Required environment variables for the slackbot on Vercel: SLACK_BOT_TOKEN (the bot user OAuth token), SLACK_SIGNING_SECRET (for request verification), OPENAI_API_KEY (or other model provider key), and EXA_API_KEY (for web search tool).

Exa API search configuration

The searchWeb tool uses exa.searchAndContents(query, options) where options include: livecrawl set to 'always', numResults set to 3, and includeDomains as an optional array of domain names to restrict search to specific domains.

updateStatusUtil creates reusable message updater

The updateStatusUtil function posts an initial message and returns an updateMessage function that can be called to edit that message. It takes initialStatus and event, posts with chat.postMessage, and returns a function that calls chat.update on the same message ts.

getThread retrieves conversation history from Slack

The getThread function retrieves messages from a Slack thread given channel and thread_ts parameters, converting them to AI SDK compatible message format with role and content fields, and filtering out messages from the bot itself using the botUserId parameter.

Give your agent this brain