text stringlengths 0 59.1k |
|---|
<source src="https://cdn.voltagent.dev/voltagent-recipes-guides/slack-4.mp4" type="video/mp4" /> |
Your browser does not support the video tag. |
</video> |
<br/> |
<br/> |
Open [VoltAgent Console](https://console.voltagent.dev/actions) and go to **Actions** → **Create Action**. |
1. Select **Slack** and the same credential |
2. Save the action |
Add the VoltOps client and `sendSlackMessage` tool to your code: |
<ExpandableCode title="src/index.ts" previewLines={15}> |
```ts |
import { openai } from "@ai-sdk/openai"; |
import { Agent, VoltAgent, createTool, createTriggers } from "@voltagent/core"; |
import { VoltOpsClient } from "@voltagent/sdk"; |
import { createPinoLogger } from "@voltagent/logger"; |
import { honoServer } from "@voltagent/server-hono"; |
import { z } from "zod"; |
import { weatherTool } from "./tools/weather"; |
type SlackMessagePayload = { |
channel?: string; |
thread_ts?: string; |
ts?: string; |
text?: string; |
user?: string; |
}; |
const logger = createPinoLogger({ name: "with-slack", level: "info" }); |
const voltOps = new VoltOpsClient({ |
publicKey: process.env.VOLTAGENT_PUBLIC_KEY ?? "", |
secretKey: process.env.VOLTAGENT_SECRET_KEY ?? "", |
}); |
const sendSlackMessage = createTool({ |
name: "sendSlackMessage", |
description: "Send a message to a Slack channel or thread.", |
parameters: z.object({ |
channelId: z.string(), |
text: z.string(), |
threadTs: z.string().optional(), |
}), |
execute: async ({ channelId, text, threadTs }) => { |
const credentialId = process.env.SLACK_CREDENTIAL_ID; |
if (!credentialId) { |
throw new Error("SLACK_CREDENTIAL_ID is not set"); |
} |
return voltOps.actions.slack.postMessage({ |
credential: { credentialId }, |
channelId, |
text, |
threadTs, |
linkNames: true, |
}); |
}, |
}); |
const slackAgent = new Agent({ |
name: "slack-agent", |
instructions: [ |
"You are a Slack assistant.", |
"Use sendSlackMessage to reply in the same channel/thread.", |
"Use getWeather for weather questions.", |
].join(" "), |
tools: [weatherTool, sendSlackMessage], |
model: openai("gpt-4o-mini"), |
}); |
new VoltAgent({ |
agents: { slackAgent }, |
server: honoServer(), |
logger, |
triggers: createTriggers((on) => { |
on.slack.messagePosted(async ({ payload, agents }) => { |
const event = (payload as SlackMessagePayload | undefined) ?? {}; |
const channelId = event.channel; |
const threadTs = event.thread_ts ?? event.ts; |
const text = event.text ?? ""; |
const userId = event.user ?? "unknown-user"; |
if (!channelId || !text) { |
logger.warn("Missing channel or text in Slack payload"); |
return; |
} |
await agents.slackAgent.generateText(`Slack channel: ${channelId} |
Thread: ${threadTs ?? "new thread"} |
User: <@${userId}> |
Message: ${text} |
Respond in Slack via sendSlackMessage; use getWeather for weather questions.`); |
}); |
}), |
}); |
``` |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.