text
stringlengths
0
59.1k
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 slackAgent = new Agent({
name: "slack-agent",
instructions: "You are a Slack assistant.",
tools: [weatherTool],
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}
If the user asks for weather, call getWeather.`);
});
}),
});
```
</ExpandableCode>
<ExpandableCode title="src/tools/weather.ts" previewLines={10}>
```ts
import { createTool } from "@voltagent/core";
import { z } from "zod";
const weatherOutputSchema = z.object({
weather: z.object({
location: z.string(),
temperature: z.number(),
condition: z.string(),
humidity: z.number(),
windSpeed: z.number(),
}),
message: z.string(),
});
export const weatherTool = createTool({
name: "getWeather",
description: "Get the current weather for a specific location",
parameters: z.object({
location: z.string().describe("City or location to get weather for (e.g., San Francisco)"),
}),
outputSchema: weatherOutputSchema,
execute: async ({ location }) => {
// Mocked weather data for demo purposes; replace with a real API if desired.
const mockWeatherData = {
location,
temperature: Math.floor(Math.random() * 30) + 5,
condition: ["Sunny", "Cloudy", "Rainy", "Snowy", "Partly Cloudy"][
Math.floor(Math.random() * 5)
],
humidity: Math.floor(Math.random() * 60) + 30,
windSpeed: Math.floor(Math.random() * 30),
};
return {
weather: mockWeatherData,
message: `Current weather in ${location}: ${mockWeatherData.temperature}°C and ${mockWeatherData.condition.toLowerCase()} with ${mockWeatherData.humidity}% humidity and wind speed of ${mockWeatherData.windSpeed} km/h.`,
};
},
});
```
</ExpandableCode>
</StepSection>
<StepSection stepNumber={6} title="Add the Slack Action and Reply Tool">
<video controls loop muted playsInline style={{width: '100%', height: 'auto'}}>