-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
94 lines (77 loc) · 2.3 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require("dotenv").config();
const { Telegraf } = require("telegraf");
const { TelegrafEncommands, InvalidCommand } = require("./lib");
// Example of cache store using AWS DynamoDB
// https://github.com/mhmd-22/dynamo-kvstore
// const dynamoStore = require("dynamo-kvstore");
/** @param {ReturnType<TelegrafEncommands>} commands */
function initializeCommands(commands) {
// create new command with default configs
commands.on("test", ({ ctx, command, query }) => {
return ctx.reply(`/${command} ${query}`);
});
// create command with custom configs
// Here we allow useRepliedTo option to take the replied to message as command query
commands.create("echo", {
required: false,
useRepliedTo: true,
handler: ({ ctx, reply_to, query }) => {
if (reply_to) {
return ctx.reply(`reply: ${query}`);
}
},
});
commands.create("test2", {
required: true,
// helpMessage: "invalid command arguments",
handler: ({ ctx, query }) => {
return ctx.reply(query);
},
});
// Reply with file details
commands.create("file", {
subTypes: ["photo", "sticker", "video", "voice", "audio", "document"],
required: false,
handler: ({ ctx, data, type }) => {
return ctx.replyWithMarkdownV2(`${type}: \`${JSON.stringify(data, null, 2)}\``);
},
oninvalid(reason, ctx, next) {
console.log(reason);
if (reason == InvalidCommand.INVALID_MESSAGE_TYPE) {
return ctx.reply("File is required")
}
return next()
},
});
}
async function init() {
try {
const bot = new Telegraf(process.env.BOT_TOKEN);
const commands = TelegrafEncommands({
// Example of using custom cache store
// cacheStore: await new dynamoStore("bot-replies", {
// endpoint: "http://localhost:7000",
// region: "us-west-2",
// }),
replyCacheAge: 60, // one minute
defaults: {
required: true,
useReply: true,
helpMessage: "*set your help message here*",
},
});
initializeCommands(commands);
bot.use(commands.middleware);
// next()
bot.on("message", ctx => {
ctx.reply("hello");
});
bot.catch(err => {
console.error(err);
});
await bot.launch();
} catch (e) {
console.error(e);
}
}
init();