-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
120 lines (114 loc) · 3.32 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
const TelegramBot = require('node-telegram-bot-api');
const ogs = require('open-graph-scraper');
const mongoose = require('mongoose');
const axios = require('axios');
require('./db')
// Bot config
const token = process.env.TELEGRAM_BOT_TOKEN
const bot = new TelegramBot(token, {
polling: true
});
// Init Models
// const Chuck = mongoose.model('ChuckNoris');
const Bookmark = mongoose.model('Bookmark');
let siteUrl;
bot.on(/\/chuck/, (msg) => {
const chatId = msg.chat.id;
// fetch a random chucknoris joke
axios.get('https://api.chucknorris.io/jokes/random')
.then(function (response) {
// handle success
console.log(response);
bot.sendMessage(message.chat.id, response.value);
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
// always executed
});
// send a message to the chat acknowledging receipt of their message
bot.sendMessage(chatId, 'Received your message');
});
// Reply to /bookmark
bot.onText(/\/bookmark (.+)/, (msg, match) => {
siteUrl = match[1];
bot.sendMessage(msg.chat.id, 'Got it, in which category?', {
reply_markup: {
inline_keyboard: [
[{
text: 'Development',
callback_data: 'development'
}, {
text: 'Music',
callback_data: 'music'
}, {
text: 'Cute monkeys',
callback_data: 'cute-monkeys'
}]
]
}
});
});
// Callback query
bot.on("callback_query", (callbackQuery) => {
const message = callbackQuery.message;
// Scrap OG date
ogs({
'url': siteUrl
}, function (error, results) {
if (results.success) {
// Push to Firebase
// sitesRef.push().set({
// name: results.data.ogSiteName,
// title: results.data.ogTitle,
// description: results.data.ogDescription,
// url: siteUrl,
// thumbnail: results.data.ogImage.url,
// category: callbackQuery.data
// });
const data = {
name: results.data.ogSiteName,
title: results.data.ogTitle,
description: results.data.ogDescription,
url: siteUrl,
thumbnail: results.data.ogImage.url,
category: callbackQuery.data
};
const _bookmark = new Bookmark(data);
_bookmark.save()
.then(() => {
// Reply
bot.sendMessage(message.chat.id, 'Added \"' + results.data.ogTitle + '\" to category \"' + callbackQuery.data + '\"!');
})
.catch((err) => {
console.log(err);
// Reply
bot.sendMessage(message.chat.id, 'Sorry! Something went wrong.');
});
// Reply
// bot.sendMessage(message.chat.id,'Added \"' + results.data.ogTitle +'\" to category \"' + callbackQuery.data + '\"!');
} else {
// Push to Firebase
// sitesRef.push().set({
// url: siteUrl
// });
const _bookmark = new Bookmark({
url: siteUrl
});
_bookmark.save()
.then(() => {
// Reply
bot.sendMessage(message.chat.id, 'Added new website, but there was no OG data!');
})
.catch((err) => {
console.log(err);
// Reply
bot.sendMessage(message.chat.id, 'Sorry! Something went wrong.');
});
// Reply
// bot.sendMessage(message.chat.id, 'Added new website, but there was no OG data!');
}
});
});