-
Notifications
You must be signed in to change notification settings - Fork 44
/
main-javascript.mjs
255 lines (224 loc) · 5.45 KB
/
main-javascript.mjs
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// This is the same example as the main-typescript.ts just without type anotations.
// Consider using TypeScript. If you compare both examples they are fairly similar.
// Learning TypeScript will be worth it!
//
// If you just want to use JavaScript, go ahead :)
import * as process from 'node:process';
import {Bot} from 'grammy';
import {
createBackMainMenuButtons,
MenuMiddleware,
MenuTemplate,
} from '../dist/source/index.js';
const menu = new MenuTemplate(() => 'Main Menu\n' + new Date().toISOString());
menu.url({text: 'EdJoPaTo.de', url: 'https://edjopato.de'});
let mainMenuToggle = false;
menu.toggle('toggle me', {
text: 'toggle me',
set(_, newState) {
mainMenuToggle = newState;
// Update the menu afterwards
return true;
},
isSet: () => mainMenuToggle,
});
menu.interact('interact', {
text: 'interaction',
hide: () => mainMenuToggle,
async do(ctx) {
await ctx.answerCallbackQuery({text: 'you clicked me!'});
// Do not update the menu afterwards
return false;
},
});
menu.interact('update afterwards', {
joinLastRow: true,
text: 'update after action',
hide: () => mainMenuToggle,
async do(ctx) {
await ctx.answerCallbackQuery({text: 'I will update the menu now…'});
return true;
// You can return true to update the same menu or use a relative path
// For example '.' for the same menu or '..' for the parent menu
// return '.'
},
});
let selectedKey = 'b';
menu.select('select', {
choices: ['A', 'B', 'C'],
async set(ctx, key) {
selectedKey = key;
await ctx.answerCallbackQuery({text: `you selected ${key}`});
return true;
},
isSet: (_, key) => key === selectedKey,
});
const foodMenu = new MenuTemplate(
'People like food. What do they like?',
);
const people = {Mark: {}, Paul: {}};
const foodSelectSubmenu = new MenuTemplate(ctx => {
const person = ctx.match[1];
const hisChoice = people[person].food;
if (!hisChoice) {
return `${person} is still unsure what to eat.`;
}
return `${person} likes ${hisChoice} currently.`;
});
foodSelectSubmenu.toggle('tea', {
text: 'Prefer tea',
set(ctx, choice) {
const person = ctx.match[1];
people[person].tee = choice;
return true;
},
isSet(ctx) {
const person = ctx.match[1];
return people[person].tee === true;
},
});
foodSelectSubmenu.select('food', {
choices: ['bread', 'cake', 'bananas'],
set(ctx, key) {
const person = ctx.match[1];
people[person].food = key;
return true;
},
isSet(ctx, key) {
const person = ctx.match[1];
return people[person].food === key;
},
});
foodSelectSubmenu.manualRow(createBackMainMenuButtons());
foodMenu.chooseIntoSubmenu('person', foodSelectSubmenu, {
columns: 2,
choices: () => Object.keys(people),
buttonText(_ctx, key) {
const entry = people[key];
if (entry?.food) {
return `${key} (${entry.food})`;
}
return key;
},
});
foodMenu.manualRow(createBackMainMenuButtons());
menu.submenu('food', foodMenu, {
text: 'Food menu',
hide: () => mainMenuToggle,
});
const MEDIA_OPTIONS = [
'animation',
'document',
'photo1',
'photo2',
'video',
'location',
'venue',
'just text',
];
let mediaOption = 'photo1';
const mediaMenu = new MenuTemplate(() => {
if (mediaOption === 'video') {
return {
type: 'video',
media: 'https://telegram.org/img/t_main_Android_demo.mp4',
text: 'Just a caption for a video',
};
}
if (mediaOption === 'animation') {
return {
type: 'animation',
media: 'https://telegram.org/img/t_main_Android_demo.mp4',
text: 'Just a caption for an animation',
};
}
if (mediaOption === 'photo2') {
return {
type: 'photo',
media: 'https://telegram.org/img/SiteAndroid.jpg',
text: 'Just a caption for a *photo*',
parse_mode: 'Markdown',
};
}
if (mediaOption === 'document') {
return {
type: 'document',
media:
'https://telegram.org/file/464001088/1/bI7AJLo7oX4.287931.zip/374fe3b0a59dc60005',
text: 'Just a caption for a <b>document</b>',
parse_mode: 'HTML',
};
}
if (mediaOption === 'location') {
return {
// Some point with simple coordinates in Hamburg, Germany
location: {
latitude: 53.5,
longitude: 10,
},
live_period: 60,
};
}
if (mediaOption === 'venue') {
return {
venue: {
location: {
latitude: 53.5,
longitude: 10,
},
title: 'simple coordinates point',
address: 'Hamburg, Germany',
},
};
}
if (mediaOption === 'just text') {
return {
text: 'Just some text',
};
}
return {
type: 'photo',
media: 'https://telegram.org/img/SiteiOs.jpg',
};
});
mediaMenu.interact('randomButton', {
text: 'Just a button',
async do(ctx) {
await ctx.answerCallbackQuery({text: 'Just a callback query answer'});
return false;
},
});
mediaMenu.select('type', {
columns: 2,
choices: MEDIA_OPTIONS,
isSet: (_, key) => mediaOption === key,
set(_, key) {
mediaOption = key;
return true;
},
});
mediaMenu.manualRow(createBackMainMenuButtons());
menu.submenu('media', mediaMenu, {
text: 'Media Menu',
});
const menuMiddleware = new MenuMiddleware('/', menu);
console.log(menuMiddleware.tree());
const bot = new Bot(process.env['BOT_TOKEN']);
bot.on('callback_query:data', async (ctx, next) => {
console.log(
'another callbackQuery happened',
ctx.callbackQuery.data.length,
ctx.callbackQuery.data,
);
return next();
});
bot.command('start', async ctx => menuMiddleware.replyToContext(ctx));
bot.use(menuMiddleware.middleware());
bot.catch(error => {
console.log('bot error', error);
});
await bot.start({
onStart(botInfo) {
console.log(new Date(), 'Bot starts as', botInfo.username);
},
});