Examples
Here are some examples of how to use BotKit.
Greeting bot
The following example shows how to publish messages in various ways using BotKit. The bot performs the following actions:
- Sends a direct message with an image attachment when someone follows the bot.
- Sends a direct message when someone unfollows the bot.
- Replies to it when someone replies to a message from the bot.
- Replies to it when someone mentions the bot.
- Publishes a greeting message every minute.
- Deletes the greeting message after 30 seconds.
ts
import { createBot, Image, mention, text } from "@fedify/botkit";
import { DenoKvMessageQueue, DenoKvStore } from "@fedify/fedify/x/denokv";
const kv = await Deno.openKv();
const bot = createBot<void>({
username: "greetbot",
name: "Greet Bot",
summary: text`Hi, there! I'm a simple fediverse bot created by ${
mention("@hongminhee@hollo.social")
}.`,
icon: new URL(
"https://repository-images.githubusercontent.com/913141583/852a1091-14d5-46a0-b3bf-8d2f45ef6e7f",
),
kv: new DenoKvStore(kv),
queue: new DenoKvMessageQueue(kv),
behindProxy: true,
});
bot.onFollow = async (session, follower) => {
await session.publish(text`Thanks for following me, ${follower}!`, {
visibility: "direct",
attachments: [
new Image({
mediaType: "image/png",
url: new URL(
"https://repository-images.githubusercontent.com/913141583/852a1091-14d5-46a0-b3bf-8d2f45ef6e7f",
),
name: "BotKit logo",
width: 1280,
height: 640,
}),
],
});
};
bot.onUnfollow = async (session, follower) => {
await session.publish(text`Goodbye, ${follower}!`, {
visibility: "direct",
});
};
bot.onReply = async (session, message) => {
const botUri = session.actorId.href;
if (message.mentions.some((a) => a.id?.href === botUri)) return;
await message.reply(text`Thanks for your reply, ${message.actor}!`);
};
bot.onMention = async (_session, message) => {
await message.reply(text`Hi, ${message.actor}!`);
};
const session = bot.getSession(Deno.env.get("ORIGIN") ?? "http://localhost");
setInterval(async () => {
const message = await session.publish(
text`Hi, forks! It's a minutely greeting. It will be deleted in 30 seconds.`,
);
setTimeout(async () => {
await message.delete();
}, 1000 * 30);
}, 1000 * 60);
export default bot;
// cSpell: ignore greetbot