-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
117 lines (94 loc) · 3.94 KB
/
app.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
require('dotenv').config();
const Telegraf = require('telegraf');
const Markup = require('telegraf/markup');
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const util = require('util');
const TawawaTwitter = require('./util/twitter.js');
const TawawaFirebase = require('./util/firebase');
const ListHandler = require('./list');
const bot = new Telegraf(process.env.TELEGRAM_TOKEN);
bot.telegram.getMe().then((botInfo) => {
bot.options.username = botInfo.username
});
let tawawaTwitter = new TawawaTwitter();
let tawawaFirebase = new TawawaFirebase();
/************************************
* BOT COMMANDS
************************************/
bot.use((ctx, next) => {
console.log(ctx.message);
return next();
});
ListHandler.init(bot, tawawaFirebase);
bot.start((ctx) => {
ctx.reply('Hi there! I\'m the WebComic bot (Tawawa Branch)! ' +
'I deliver weekly editions of Getsuyoubi no Tawawa automatically!' +
'\n\nYou can interact with me by sending me these commands:' +
'\n\n/help - Display this message.' +
'\n/latest - Get the latest Tawawa comic.' +
'\n/get <number> - Show the numbered comic' +
'\n/list - Show a list of all archived comics' +
'\n/subscribe - Get an automated update every Monday!')
});
bot.command('/subscribe', async (ctx)=>{
let isSubscribed = await tawawaFirebase.getSubscriber(ctx.chat.id);
if (isSubscribed) return ctx.reply('This chat is already subscribed!');
tawawaFirebase.subscribe(ctx.chat);
if (ctx.chat.type === 'group'){
return ctx.replyWithMarkdown(`*${ctx.chat.title}* is now subscribed to \`Tawawa on Mondays\`!`);
} else {
return ctx.replyWithMarkdown(`You are now subscribed to \`Tawawa on Mondays\`!`);
}
});
bot.command('/unsubscribe', async (ctx)=>{
let isSubscribed = await tawawaFirebase.getSubscriber(ctx.chat.id);
console.log(isSubscribed);
if (!isSubscribed) return ctx.replyWithMarkdown(`This chat is not currently subscribed to \`Tawawa on Mondays\`.`);
tawawaFirebase.unsubscribe(ctx.chat);
if (ctx.chat.type === 'group'){
return ctx.replyWithMarkdown(`*${ctx.chat.title}* is now unsubscribed from \`Tawawa on Mondays\`.`);
} else {
return ctx.replyWithMarkdown(`You are now unsubscribed from \`Tawawa on Mondays\`.`);
}
});
bot.command('/debug', (ctx)=>{
console.log(ctx.message);
ctx.reply("Debug: ID of chat = " + ctx.chat.id.toString());
});
bot.command(['/grab', '/fetch', '/get'], async (ctx)=>{
let numberRegex = /(\d+)/g;
let num = ctx.message.text.match(numberRegex);
if (num !== null){
let postsArray = await tawawaFirebase.getPostsAtIndex(num[0]);
if (postsArray.length === 0){
ctx.reply(`Sorry, comic no. ${num[0]} not found in the database. It may not have been archived yet.`);
} else {
ctx.reply(postsArray[0].full);
}
} else {
ctx.replyWithMarkdown(`*/grab, /fetch, /get <number>* to display a specific Tawawa comic.`);
}
});
bot.help((ctx)=>{
return ctx.reply('Hi there! I\'m the WebComic bot (Tawawa Branch)! ' +
'I deliver weekly editions of Getsuyoubi no Tawawa automatically!' +
'\n\nYou can interact with me by sending me these commands:' +
'\n\n/help - Display this message.' +
'\n/latest - Get the latest Tawawa comic.' +
'\n/get <number> - Show the numbered comic' +
'\n/list - Show a list of all archived comics' +
'\n/subscribe - Get an automated update every Monday!')
});
// bot.command('/largefetch', async (ctx)=>{
// let postArray = await tawawaTwitter.largeFetch();
// await tawawaFirebase.populate(postArray);
// });
bot.command('/latest', async (ctx)=>{
let postArray = await tawawaTwitter.smallFetch();
await tawawaFirebase.populate(postArray);
let latestPost = await tawawaFirebase.getLatest();
ctx.reply(latestPost.full);
});
bot.launch();