-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.ts
62 lines (50 loc) · 1.82 KB
/
bot.ts
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
import { Bot } from 'grammy';
import { I18n } from '@grammyjs/i18n';
import { AWSLambdaEvent, AWSLambdaResponse, BotMode, MyContext, isBotMode } from './types';
import configuration from './configuration';
import { handleStart, handleHelp } from './commands';
/*--------------------
| B O T C O N F |
--------------------*/
// Choose the bot token based on the environment
const arg: string = process.argv.slice(2)[0];
const mode: BotMode = isBotMode(arg) ? arg : BotMode.Production;
const BOT_TOKEN = configuration[mode].token;
// Initialize i18n
const i18n = new I18n<MyContext>({
defaultLocale: 'en',
directory: 'locales', // Load all translation files from locales directory
});
// Create bot instance with context type
const bot = new Bot<MyContext>(BOT_TOKEN);
bot.use(i18n.middleware());
// Set up AWS Lambda handler for production mode
export const handler = async (event: AWSLambdaEvent): Promise<AWSLambdaResponse> => {
if (mode === BotMode.Development) {
return { statusCode: 200, body: 'Bot is running in development mode' };
}
// Process the update
const body = JSON.parse(event.body);
await bot.handleUpdate(body);
// Return a response to acknowledge receipt of the event
return { statusCode: 200, body: JSON.stringify({ ok: true }) };
};
/*--------------------
| H A N D L E R S |
--------------------*/
// Register command handlers
bot.command('start', handleStart);
bot.command('help', handleHelp);
// Handle text messages
bot.on('message:text', async (ctx) => {
// Simple echo functionality as an example
await ctx.reply(`You said: ${ctx.message.text}`);
});
/*--------------------
| R U N B O T |
--------------------*/
// Start the bot in development mode
if (mode === BotMode.Development) {
console.log('Running in development mode...');
bot.start();
}