-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
1007 lines (862 loc) · 26.9 KB
/
bot.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const axios = require("axios").default;
const fs = require("fs");
const FormData = require("form-data");
const request = require("request");
const express = require("express");
const cors = require("cors");
const morgan = require("morgan");
const helmet = require("helmet");
const TeleBot = require("telebot");
const { fetchMeme, fetchMemeTemplate } = require("./meme");
const { sendLogs } = require("./log");
const { connectToRedis } = require("./redis");
const { cleanUpImages } = require("./utils");
require("dotenv").config();
// Created bot object
const bot = new TeleBot({
token: process.env.TELEGRAM_KEY, // Required. Telegram Bot API token.
webhook: {
url: process.env.APP_URL + "/bot", // HTTPS url to send updates to.
},
// polling: {
// interval: 1000, // Optional. How often check updates (in ms).
// timeout: 0, // Optional. Update polling timeout (0 - short polling).
// limit: 100, // Optional. Limits the number of updates to be retrieved.
// retryTimeout: 5000, // Optional. Reconnecting timeout (in ms).
// },
});
let client = null;
const url = "https://api.imgflip.com/caption_image";
const app = express();
const port = process.env.PORT || 3000;
// parse the updates to JSON
app.use(express.json());
app.use(cors());
app.use(helmet());
app.use(morgan("combined"));
// We are receiving updates at the route below!
app.post(`/bot/${process.env.TELEGRAM_KEY}`, (req, res) => {
bot.receiveUpdates([req.body]);
// bot.receiveUpdates(req.body);
res.status(200).send("ok");
});
// Start Express Server
app.listen(port, async () => {
console.log(`Memer bot server is listening on ${port}`);
client = await connectToRedis();
setInterval(() => cleanUpImages(), 300000);
});
bot.on("error", (err) => {
console.log("Some error occured", err);
});
// Admin Message
bot.on(/\/message (.+)/, async (msg, props) => {
console.log(props);
const message = props.match[1];
if (msg.chat.id == process.env.MY_CHAT_ID && message) {
const text = message.substring(9);
try {
const keys = await client.keys("*");
console.log("Sending message to ", keys.length, " people");
let success = 0;
for (const chatId of keys) {
console.log("sending msg to", chatId);
try {
bot.sendMessage(chatId, text);
success++;
} catch (err) {
console.log("Error while sending message", err);
}
}
console.log(
"Successfully sent message to ",
success + "/" + keys.length + " people"
);
bot.sendMessage(
process.env.MY_CHAT_ID,
"Successfully sent message to " +
success +
"/" +
keys.length +
" people"
);
} catch (err) {
console.log("Error while sending message");
bot.sendMessage(msg.chat.id, "Error while sending message ");
return;
}
}
});
// Start
bot.on(/\/start/, async (msg) => {
bot.sendMessage(msg.chat.id, "Welcome to Memer Bot");
bot.sendMessage(
msg.chat.id,
`Hey there ${msg.from.first_name}, I am Memer Bot!
You can search & create memes using the following commands:
/search <phrase> - Search for a meme for a word/phrase
/create - Create a meme from a template or custom image
/reset - Reset the current state of the bot (if not responding)
`
);
await client.set(msg.chat.id, JSON.stringify({ state: "NONE" }));
// Send Logs
sendLogs(
{
Event: "Welcome",
User: msg.chat.username,
},
"memer_welcome"
);
});
// Reply to hey, hi, hello
bot.on(/^hi$|^hey$|^hello$/i, async (msg) => {
bot.sendMessage(
msg.chat.id,
`Hey there ${msg.from.first_name}, I am Memer Bot!
You can search & create memes using the following commands:
/search <search-term> - Search for a meme for a term
/create - Create a meme from a template or custom image
/reset - Reset the current state of the bot (if not responding)
`
);
await client.set(msg.chat.id, JSON.stringify({ state: "NONE" }));
// Send Logs
sendLogs(
{
Event: "Welcome",
User: msg.chat.username,
},
"memer_welcome"
);
});
// Reset states
bot.on(/\/reset/, async (msg) => {
await client.set(msg.chat.id, JSON.stringify({ state: "NONE" }));
bot.sendMessage(
msg.chat.id,
"Resetted state. Now you can try searching or creating memes again"
);
});
// Search error
bot.on(/^\/search$/, async (msg) => {
await client.set(msg.chat.id, JSON.stringify({ state: "NONE" }));
bot.sendMessage(msg.chat.id, "Send search term like /search <search-term>");
});
// Search
bot.on(/^\/search (.+)$/, async (msg, props) => {
await client.set(msg.chat.id, JSON.stringify({ state: "NONE" }));
console.log({ props });
const text = props.match[0];
if (text) {
const searchText = text.substring(7);
// Send Logs
sendLogs(
{
Event: "Search Meme Request",
Search: searchText,
User: msg.chat.username,
},
"memer_search"
);
console.log("searchText", searchText);
bot.sendMessage(msg.chat.id, "Seaching meme for you...");
let memeSrcs = await fetchMeme(searchText);
if (memeSrcs && memeSrcs.length > 0) {
console.log("Got Search " + memeSrcs);
bot.sendMessage(
msg.chat.id,
`${msg.from.first_name}, Here are some top memes I found 👇`
);
memeSrcs.forEach((memeSrc) => {
if (memeSrc.substring(0, 2) === "//") {
memeSrc = "http://" + memeSrc.substring(2);
} else {
memeSrc = "https://imgflip.com" + memeSrc;
}
bot.sendPhoto(msg.chat.id, memeSrc);
});
// Send Logs
sendLogs(
{
Event: "Search Meme Processed",
Search: searchText,
Status: "Success",
User: msg.chat.username,
},
"memer_search"
);
} else {
bot.sendMessage(
msg.chat.id,
"Sorry " + msg.from.first_name + ", I couldn't find a meme for you 😢"
);
// Send Logs
sendLogs(
{
Event: "Search Meme Processed",
Search: searchText,
Status: "Error",
User: msg.chat.username,
},
"memer_search"
);
}
}
});
// Create
bot.on(/\/create/, async (msg) => {
await client.set(msg.chat.id, JSON.stringify({ state: "NONE" }));
bot.sendMessage(
msg.chat.id,
"Choose do you want to create a meme from a template or custom image",
{
replyMarkup: {
inline_keyboard: [
[
{
text: "Template",
callback_data: "TEMPLATE_TYPE",
},
{
text: "Custom Image",
callback_data: "CUSTOM_TYPE",
},
],
],
},
}
);
await client.set(msg.chat.id, JSON.stringify({ state: "CREATE_STARTED" }));
});
// Handle callback queries
bot.on("callbackQuery", async (callbackQuery) => {
const action = callbackQuery.data;
const msg = callbackQuery.message;
console.log(action);
if (action == "TEMPLATE_TYPE") {
// Send Logs
sendLogs(
{
Event: "Create Meme Started",
Type: "Template",
User: msg.chat.username,
},
"memer_create"
);
bot.sendMessage(
msg.chat.id,
"Please enter a search term to get a meme template"
);
await client.set(
msg.chat.id,
JSON.stringify({ state: "CREATE_TEMPLATE_SEARCH" })
);
} else if (action.includes("TEMPLATE_YES")) {
let templateId = action.split(" ")[2];
const chatDataString = await client.get(msg.chat.id);
const chatData = chatDataString ? JSON.parse(chatDataString) : null;
const tempMsgMap = chatData.tempMsgMap;
if (
tempMsgMap &&
tempMsgMap.find((m) => m.templateId == templateId) &&
tempMsgMap.find((m) => m.templateId == templateId).messageId
) {
console.log("templateId", templateId);
bot.sendMessage(msg.chat.id, "Great, You have a great choice!", {
replyToMessage: tempMsgMap.find((m) => m.templateId == templateId)
.messageId,
});
bot.sendMessage(msg.chat.id, "Please enter a top text (send . to skip)");
await client.set(
msg.chat.id,
JSON.stringify({
...chatData,
state: "CREATE_TEMPLATE_TOP",
templateId: templateId,
})
);
}
} else if (action == "CUSTOM_TYPE") {
// Send Logs
sendLogs(
{
Event: "Create Meme Started",
Type: "Custom",
User: msg.chat.username,
},
"memer_create"
);
bot.sendMessage(
msg.chat.id,
"Please send me a photo to create a meme from"
);
await client.set(
msg.chat.id,
JSON.stringify({ state: "CREATE_CUSTOM_IMAGE_UPLOAD" })
);
}
});
// Handle create meme data
bot.on(/(.*)/, async (msg, props) => {
try {
const chatDataString = await client.get(msg.chat.id);
const chatData = chatDataString ? JSON.parse(chatDataString) : null;
if (chatData && chatData.state === "CREATE_TEMPLATE_SEARCH") {
const searchText = props.match[0];
console.log("searchText", searchText);
bot.sendMessage(msg.chat.id, "Seaching meme template for you...");
const memeTemplates = await fetchMemeTemplate(searchText);
if (!!memeTemplates && memeTemplates.length > 0) {
bot.sendMessage(
msg.chat.id,
`${msg.from.first_name}, Here are some meme templates from which you can choose 👇`
);
bot.sendMessage(
msg.chat.id,
`Select any one which you want to use by clicking "Select" button below each image`
);
const tempMsgMap = [];
for (let memeTemplate of memeTemplates) {
let { image, id } = memeTemplate;
if (image && id) {
console.log("Got Search " + image);
if (image.substring(0, 2) === "//") {
image = "http://" + image.substring(2);
} else {
image = "https://imgflip.com" + image;
}
const message = await bot.sendPhoto(msg.chat.id, image, {
replyMarkup: {
inline_keyboard: [
[
{
text: "Select",
callback_data: "TEMPLATE_YES ID: " + id,
},
],
],
},
});
console.log(message);
if (message)
tempMsgMap.push({
templateId: id,
messageId: message.message_id,
});
}
}
await client.set(
msg.chat.id,
JSON.stringify({ state: "CREATE_TEMPLATE_YES", tempMsgMap })
);
} else {
bot.sendMessage(
msg.chat.id,
"Sorry " +
msg.from.first_name +
", I couldn't find a meme template for you 😢. Please enter some other search term"
);
}
} else if (chatData && chatData.state === "CREATE_TEMPLATE_TOP") {
const text = props.match[0];
console.log("topText", text);
let topText = "";
if (text === ".") {
} else {
topText = text;
}
bot.sendMessage(
msg.chat.id,
"Please enter a bottom text (send . to skip)"
);
await client.set(
msg.chat.id,
JSON.stringify({
state: "CREATE_TEMPLATE_BOTTOM",
templateId: chatData.templateId,
topText: topText,
})
);
} else if (chatData && chatData.state === "CREATE_TEMPLATE_BOTTOM") {
const text = props.match[0];
console.log("bottomText", text);
let bottomText = "";
if (text === ".") {
} else {
bottomText = text;
}
bot.sendMessage(
msg.chat.id,
`
Top Text is ${
chatData.topText === "" ? "None" : chatData.topText
} \nBottom Text is ${bottomText === "" ? "None" : bottomText}
`
);
// Generate meme
const response = await axios.post(
url,
new URLSearchParams(
{
template_id: chatData.templateId,
username: process.env.IMGFLIP_USERNAME,
password: process.env.IMGFLIP_PASSWORD,
text0: chatData.topText,
text1: bottomText,
},
{
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
}
)
);
if (
response.status == 200 &&
!!response.data &&
response.data.success &&
response.data.data.url
) {
bot.sendMessage(msg.chat.id, `Here is your meme 👇`);
bot.sendPhoto(msg.chat.id, response.data.data.url);
await client.set(
msg.chat.id,
JSON.stringify({ state: "CREATE_TEMPLATE_FINISHED" })
);
// If you like it do share this bot with your friends
// Also follow developer Amit Wani on Twitter @mtwn105
bot.sendMessage(
msg.chat.id,
"Do you like this meme? Share it with your friends and follow me @mtwn105 on Twitter for more cool stuff 😉. If you want to support me, consider clicking the button below 👇",
{
replyMarkup: {
inline_keyboard: [
[
{
text: "@mtwn105",
url: "https://twitter.com/mtwn105",
},
{
text: "Support",
url: "https://rzp.io/l/dQtgHoQ6",
},
],
],
},
}
);
// Send Logs
sendLogs(
{
Event: "Create Meme Processed",
Status: "Success",
Type: "Template",
User: msg.chat.username,
},
"memer_create"
);
} else {
console.log("Error occured while generating meme from meme template", {
response,
});
bot.sendMessage(
msg.chat.id,
`Sorry ${msg.from.first_name}, There was some error & I couldn't generate a meme for you 😢. Please try again 🥺`
);
await client.set(
msg.chat.id,
JSON.stringify({ state: "CREATE_TEMPLATE_FINISHED" })
);
// Send Logs
sendLogs(
{
Event: "Create Meme Processed",
Status: "Error",
Type: "Template",
User: msg.chat.username,
},
"memer_create"
);
}
} else if (chatData && chatData.state === "CREATE_CUSTOM_IMAGE_TOP") {
const text = props.match[0];
console.log("topText", text);
let topText = "";
if (text === ".") {
} else {
topText = text;
}
bot.sendMessage(
msg.chat.id,
"Please enter a bottom text (send . to skip)"
);
await client.set(
msg.chat.id,
JSON.stringify({
state: "CREATE_CUSTOM_IMAGE_BOTTOM",
image: chatData.image,
topText: topText,
})
);
} else if (chatData && chatData.state === "CREATE_CUSTOM_IMAGE_BOTTOM") {
const text = props.match[0];
console.log("bottomText", text);
let bottomText = "";
if (text === ".") {
} else {
bottomText = text;
}
bot.sendMessage(
msg.chat.id,
`
Top Text is ${
chatData.topText === "" ? "None" : chatData.topText
} \nBottom Text is ${bottomText === "" ? "None" : bottomText}
`
);
bot.sendMessage(msg.chat.id, `Generating meme please wait...`);
await generateCustomMeme(msg, bottomText);
} else if (
msg.text.includes("/start") &&
msg.text.includes("/help") &&
msg.text.includes("/search") &&
msg.text.includes("/create") &&
msg.text.includes("hi") &&
msg.text.includes("hey") &&
msg.text.includes("hello")
) {
// await client.set(msg.chat.id, JSON.stringify({ state: "NONE" }));
// Tell user I can't understand this and show help
bot.sendMessage(
msg.chat.id,
"Sorry " +
msg.from.first_name +
", I couldn't understand your message 😢. \n" +
`You can search & create memes using the following commands:
/search <search-term> - Search for a meme for a term
/create - Create a meme from a template or custom image
/reset - Reset the current state of the bot (if not responding)`
);
}
} catch (err) {
console.log("Error occurred ", err);
bot.sendMessage(
msg.chat.id,
`Sorry ${msg.from.first_name}, There was some error & I couldn't help you 😢. Please try again 🥺`
);
await client.set(
msg.chat.id,
JSON.stringify({ state: "CREATE_TEMPLATE_FINISHED" })
);
// Send Logs
sendLogs(
{
Event: "Create Meme Processed",
Status: "Error",
Type: "Custom",
User: msg.chat.username,
},
"memer_create"
);
}
});
// Custom meme image upload handle
bot.on("photo", async (msg) => {
const chatDataString = await client.get(msg.chat.id);
const chatData = chatDataString ? JSON.parse(chatDataString) : null;
if (chatData && chatData.state === "CREATE_CUSTOM_IMAGE_UPLOAD") {
bot.sendMessage(msg.chat.id, "Awesome!, Looking good!");
bot.sendMessage(msg.chat.id, "Please send me a top text (send . to skip)");
await client.set(
msg.chat.id,
JSON.stringify({
state: "CREATE_CUSTOM_IMAGE_TOP",
image: msg.photo[msg.photo.length - 1].file_id,
})
);
}
});
generateCustomMeme = async (msg, bottomText) => {
let stream = null;
try {
const chatDataString = await client.get(msg.chat.id);
const chatData = chatDataString ? JSON.parse(chatDataString) : null;
// Get image
const image = chatData.image;
// Get image file from telegram using file_id
const fileDetails = await bot.getFile(image);
console.log("File Details", fileDetails);
if (fileDetails && fileDetails.fileLink) {
// Download image
try {
const res = await axios.get(fileDetails.fileLink, {
responseType: "arraybuffer",
});
fs.writeFileSync(
`./images/${fileDetails.file_id}.jpg`,
Buffer.from(res.data),
"binary"
);
stream = fs.createReadStream(`./images/${fileDetails.file_id}.jpg`);
const uploadData = new FormData();
uploadData.append("image", stream);
uploadData.append("content-type", "application/octet-stream");
const options = {
method: "POST",
url: "https://ronreiter-meme-generator.p.rapidapi.com/images",
headers: {
"x-rapidapi-host": "ronreiter-meme-generator.p.rapidapi.com",
"x-rapidapi-key": process.env.RAPID_API_KEY,
...uploadData.getHeaders(),
useQueryString: true,
},
formData: {
image: {
value: stream,
options: {
filename: `${fileDetails.file_id}.jpg`,
contentType: "application/octet-stream",
},
},
},
};
request(options, async (error, response, body) => {
stream.close();
if (error) {
console.log("Error while uploading meme", error);
bot.sendMessage(
msg.chat.id,
`Sorry ${msg.from.first_name}, There was some error & I couldn't generate a meme for you 😢. Please try again 🥺`
);
} else {
const response = JSON.parse(body);
if (
(response.status == "success" && !!response.name) ||
(response.status == "error" && response.message == "File exists")
) {
// Generate Meme
try {
const res = await axios.get(
"https://ronreiter-meme-generator.p.rapidapi.com/meme",
{
headers: {
"x-rapidapi-host":
"ronreiter-meme-generator.p.rapidapi.com",
"x-rapidapi-key": process.env.RAPID_API_KEY,
},
params: {
meme: fileDetails.file_id,
top: chatData.topText,
bottom: bottomText,
},
responseType: "arraybuffer",
}
);
fs.writeFileSync(
`./images/meme_${fileDetails.file_id}.jpg`,
Buffer.from(res.data),
"binary"
);
// Send meme to user
bot.sendMessage(msg.chat.id, `Here is your meme 👇`);
bot.sendPhoto(
msg.chat.id,
`./images/meme_${fileDetails.file_id}.jpg`
);
await client.set(
msg.chat.id,
JSON.stringify({ state: "CREATE_TEMPLATE_FINISHED" })
);
// If you like it do share this bot with your friends
// Also follow developer Amit Wani on Twitter @mtwn105
bot.sendMessage(
msg.chat.id,
"Do you like this meme? Share it with your friends and follow me @mtwn105 on Twitter for more cool stuff 😉. If you want to support me, consider clicking the button below 👇",
{
replyMarkup: {
inline_keyboard: [
[
{
text: "@mtwn105",
url: "https://twitter.com/mtwn105",
},
{
text: "Support",
url: "https://rzp.io/l/dQtgHoQ6",
},
],
],
},
}
);
// Send Logs
sendLogs(
{
Event: "Create Meme Processed",
Status: "Success",
Type: "Custom",
User: msg.chat.username,
},
"memer_create"
);
} catch (err) {
console.log(
"Error while generating custom meme from third party",
err
);
bot.sendMessage(
msg.chat.id,
`Sorry ${msg.from.first_name}, There was some error & I couldn't generate a meme for you 😢. Please try again 🥺`
);
await client.set(
msg.chat.id,
JSON.stringify({ state: "CREATE_TEMPLATE_FINISHED" })
);
// Send Logs
sendLogs(
{
Event: "Create Meme Processed",
Status: "Error",
Type: "Custom",
User: msg.chat.username,
},
"memer_create"
);
}
} else {
console.log(
"Error while uploading custom meme template to third party",
{ response }
);
bot.sendMessage(
msg.chat.id,
`Sorry ${msg.from.first_name}, There was some error & I couldn't generate a meme for you 😢. Please try again 🥺`
);
await client.set(
msg.chat.id,
JSON.stringify({ state: "CREATE_TEMPLATE_FINISHED" })
);
// Send Logs
sendLogs(
{
Event: "Create Meme Processed",
Status: "Error",
Type: "Custom",
User: msg.chat.username,
},
"memer_create"
);
}
}
});
} catch (err) {
console.log("Error while generating meme", err);
bot.sendMessage(
msg.chat.id,
`Sorry ${msg.from.first_name}, There was some error & I couldn't generate a meme for you 😢. Please try again 🥺`
);
await client.set(
msg.chat.id,
JSON.stringify({ state: "CREATE_TEMPLATE_FINISHED" })
);
// Send Logs
sendLogs(
{
Event: "Create Meme Processed",
Status: "Error",
Type: "Custom",
User: msg.chat.username,
},
"memer_create"
);
}
} else {
console.log("Error occured while downloading image: ", err);
bot.sendMessage(
msg.chat.id,
`Sorry ${msg.from.first_name}, There was some error & I couldn't generate a meme for you 😢. Please try again 🥺`
);
await client.set(
msg.chat.id,
JSON.stringify({ state: "CREATE_TEMPLATE_FINISHED" })
);
// Send Logs
sendLogs(
{
Event: "Create Meme Processed",
Status: "Error",
Type: "Custom",
User: msg.chat.username,
},
"memer_create"
);
}
} catch (err) {
console.log("Error occured while creating custom meme: ", err);
bot.sendMessage(
msg.chat.id,
`Sorry ${msg.from.first_name}, There was some error & I couldn't generate a meme for you 😢. Please try again 🥺`
);
await client.set(
msg.chat.id,
JSON.stringify({ state: "CREATE_TEMPLATE_FINISHED" })
);
// Send Logs
sendLogs(
{
Event: "Create Meme Processed",
Status: "Error",
Type: "Custom",
User: msg.chat.username,
},
"memer_create"
);
throw err;
}
};
// Admin messages
bot.on("*", async (msg) => {
if (
msg.chat.id == process.env.MY_CHAT_ID &&
!!msg.caption &&
msg.caption.includes("/message") &&
(!!msg.photo || !!msg.video || !!msg.animation)
) {
console.log("Message received from my chat id");
console.log(msg);
try {
const keys = await client.keys("*");
console.log("Sending message to ", keys.length, " people");
let success = 0;
for (const chatId of keys) {
console.log("sending msg to", chatId);
try {
if (!!msg.photo) {
bot.sendPhoto(chatId, msg.photo[msg.photo.length - 1].file_id);
} else if (!!msg.video) {
bot.sendVideo(chatId, msg.video.file_id);
} else if (!!msg.animation) {
bot.sendAnimation(chatId, msg.animation.file_id);
}
success++;
} catch (err) {
console.log("Failed to send message to ", chatId);
}
bot.sendMessage(
msg.chat.id,
"Sent messages successfully to " +
success +
"/" +
keys.length +
" people"
);
}
} catch (err) {
console.log("Error while sending message");