Skip to content

Commit cb6c231

Browse files
committed
Add example file
1 parent 809cb20 commit cb6c231

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

example/index.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import Client from 'switch-bots';
2+
import { Button, InlineMarkup, BotCommand} from 'switch-bots';
3+
import * as dotenv from 'dotenv';
4+
import { exit } from 'process';
5+
6+
dotenv.config();
7+
8+
const client = new Client(process.env.BOT_TOKEN!);
9+
10+
client.setBotCommands([{
11+
"command": "start",
12+
"description": "Start the bot",
13+
"channel": true
14+
},
15+
{
16+
"command": "help",
17+
"description": "Show this help message",
18+
"channel": true
19+
},
20+
{
21+
"command": "about",
22+
"description": "About this bot",
23+
"channel": true
24+
},
25+
{
26+
"command": "ping",
27+
"description": "Check bot latency",
28+
"channel": true
29+
}
30+
])
31+
32+
// Handle /start command
33+
client.onCommand("start", async (message) => {
34+
await message.replyText({
35+
message: "👋 Welcome! I'm an example bot.",
36+
inlineMarkup: new Button().url("Documentation", "https://github.com/New-dev0/switch-bots")
37+
});
38+
});
39+
40+
// Handle /help command
41+
client.onCommand("help", async (message) => {
42+
await message.replyText({
43+
message: "Available commands:\n/start - Start the bot\n/help - Show this help message\n/about - About this bot\n/ping - Check bot latency"
44+
});
45+
});
46+
47+
// Handle /about command
48+
client.onCommand("about", async (message) => {
49+
await message.replyText({
50+
message: "I'm a demo bot built with switch-bots SDK. I can help you test various bot features!"
51+
});
52+
});
53+
54+
// Handle /ping command
55+
client.onCommand("ping", async (message) => {
56+
const start = Date.now();
57+
const msg = await message.replyText({ message: "Pinging..." });
58+
const latency = Date.now() - start;
59+
await msg?.editText({ message: `Pong! Latency is ${latency}ms` });
60+
});
61+
62+
// Handle messages
63+
client.onMessage(async (message) => {
64+
if (message.message.toLowerCase().includes('hello')) {
65+
await message.replyText({
66+
message: "Hi there! How can I help you?"
67+
});
68+
} else if (message.message.toLowerCase().includes('bye')) {
69+
await message.replyText({
70+
message: "Goodbye! Have a great day!"
71+
});
72+
}
73+
});
74+
75+
// Start the bot
76+
client.start().then(() => {
77+
console.log('Bot started successfully!');
78+
});

0 commit comments

Comments
 (0)