-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
132 lines (117 loc) · 3.94 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { startServer } from './server.js';
import TelegramBot from 'node-telegram-bot-api';
import dotenv from 'dotenv';
import { getForecast, getForecastAtTime, getWeather } from './weather.modue.js';
import { deserialize } from './utils.js';
dotenv.config();
const token = process.env.TELEGRAM_BOT_TOKEN;
startServer();
// Create a new bot instance
const bot = new TelegramBot(token, { polling: true });
async function handleCommand(command, msg) {
switch (command) {
case 'chatid':
await bot.sendMessage(msg.chat.id, `Current chat ID is: ${msg.chat.id}`);
break;
case 'userid':
// send back telegram user id who sent message
await bot.sendMessage(msg.chat.id, `Your ID is: ${msg.from.id}`);
break;
default:
await bot.sendMessage(msg.chat.id, 'Unknown command.');
}
}
async function handleCallbackQuery(chatId, stringData, message_id) {
const data = deserialize(stringData);
const [command, ...meta] = data;
console.log('command', command);
console.log('meta', meta);
// if message_id response to message
const options = message_id ? { reply_to_message_id: message_id } : undefined;
try {
switch (command) {
case 'weather': {
const [lat, long] = meta;
if (!lat || !long) {
throw new Error(`Wrong/Empty params: [${meta.join(', ')}]`)
}
let weather = await getWeather(lat, long);
await bot.sendMessage(chatId, weather, options);
break;
}
case 'forecastAt': {
const [time, lat, long] = meta;
if (!time || !lat || !long) {
throw new Error(`Wrong/Empty params: [${meta.join(', ')}]`)
}
const forecast = await getForecastAtTime(time, lat, long);
await bot.sendMessage(chatId, forecast, options);
break;
}
case 'forecast24': {
const [lat, long] = meta;
if (!lat || !long) {
throw new Error(`Wrong/Empty params: [${meta.join(', ')}]`)
}
const forecast = await getForecast(lat, long);
await bot.sendMessage(chatId, forecast, options);
break;
}
default: {
await bot.sendMessage(chatId, 'Unknown command.', options);
break;
}
}
} catch (error) {
// console.error('handleCallbackQuery error', error);
console.error(error);
console.error('handleCallbackQuery error', error.code, error.message);
await bot.sendMessage(chatId, 'Error: ' + error.message, options);
}
}
bot.on('callback_query', async (callbackQuery) => {
const { message, data } = callbackQuery;
console.log('callbackQuery data', data);
const { chat: { id: chat_id }, message_id } = message;
try {
await handleCallbackQuery(chat_id, data, message_id);
} catch (error) {
console.error('callback_query error', error);
}
});
// Listen for any messages and log them
bot.on('message', async (msg) => {
console.log('msg.chat', msg.chat);
const botId = (await bot.getMe()).id;
// handle callback_data CallbackQuery
// and handle as others
if (msg.callback_query) {
console.log('msg.callback_query', msg.callback_query);
await handleCommand('weather', msg.callback_query.data);
}
if (msg.new_chat_member && msg.new_chat_member.id === botId) {
console.log('Bot added to chat:', msg.chat.id);
} else if (msg.new_chat_members) {
msg.new_chat_members.forEach((member) => {
if (member.id === botId) {
console.log('Bot added to chat:', msg.chat.id);
}
});
} else if (msg.left_chat_member && msg.left_chat_member.id === botId) {
console.log('Bot removed from chat:', msg.chat.id);
}
// Check if the message text starts with "/echo"
if (msg.text) {
const match = msg.text.match(/\/(\w+)(@\w+)?/);
if (match) {
console.log('message', msg);
const command = match[1];
console.log('Command:', command);
await handleCommand(command, msg);
}
}
});
// Log any errors
bot.on('polling_error', (error) => {
console.log(error);
});