-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegramBot.js
125 lines (102 loc) · 3.52 KB
/
telegramBot.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
'use strict';
let TelegramBot = require('node-telegram-bot-api')
let analytics = require('./analytics')
let db = require('./database')
let answer = require('./answer')
let https = require('https')
let firebase = require('firebase')
firebase.initializeApp({
apiKey: process.env.FIREBASE_API_KEY,
databaseURL: process.env.FIREBASE_DB_URL,
//authDomain: "projectId.firebaseapp.com",
storageBucket: process.env.FIREBASE_STORAGE_URL
})
db.init(firebase)
let bot = new TelegramBot(process.env.TELEGRAM_BOT_TOKEN, {webHook: true});
bot.setWebHook(process.env.TELEGRAM_WEBHOOK_URL)
bot.on("text", (msg, match) => {
// imageLink
if (/^(http:.*)|^(https:.*)/.exec(msg.text)) {
answerToPicture(msg.from.id, msg.text)
return
}
// '/start': send introduction
if (/\/start$/.exec(msg.text)) {
bot.sendMessage(msg.from.id, `Send me a picture (or image link) and I tell you what I see 🙈 \nI understand human faces the best`);
return
}
// admin commands
if (msg.from.id == process.env.TELEGRAM_ME) {
// '/requests': show most recent requests
if (/\/requests/.exec(msg.text)) {
db.getRequests().then(
data => bot.sendMessage(msg.from.id, prettyJsonString(data)),
error => bot.sendMessage(msg.from.id, error)
)
return
}
// '/users': show all users
if (/\/users/.exec(msg.text)) {
db.getUsers().then(
users => bot.sendMessage(msg.from.id, 'unique users: ' + users.length + '\n' + prettyJsonString(users)),
error => bot.sendMessage(msg.from.id, error)
)
return
}
}
notSupported(msg.from.id)
});
bot.on('document', (msg, match) => {
if (msg.document.mime_type.indexOf("image") < 0) {
notSupported()
return
}
let fileId = msg.document.file_id
bot.getFileLink(fileId).then(imageUri => answerToPicture(msg.from, imageUri))
});
bot.on('photo', (msg, match) => {
console.log(msg)
let fileId = msg.photo[msg.photo.length - 1].file_id
bot.getFileLink(fileId).then(imageUri => answerToPicture(msg.from, imageUri))
});
function prettyJsonString(object) {
return JSON.stringify(object, null, 2).replace(/"|,|{|}/g, '')
}
function notSupported(recipientId) {
bot.sendMessage(recipientId, "I can only look at pictures 😔");
}
function answerToPicture(recipient, imageUri) {
bot.sendChatAction(recipient.id, "typing")
analytics.getDescription(imageUri).then(desc => {
console.log("description", desc.description.captions)
let descAnswer = answer.buildDescriptionAnswer(desc.description)
bot.sendMessage(recipient.id, descAnswer);
analytics.getFaces(imageUri).then(fcs => {
console.log("faces", fcs)
if (fcs.faces && fcs.faces.length > 0) {
let ageAnswer = answer.buildAgeAnswer(fcs.faces, fcs.faces[0].gender)
bot.sendMessage(recipient.id, ageAnswer);
analytics.getEmotions(imageUri).then(emotions => {
let emotionsAnswer = answer.buildEmotionsAnswer(emotions, fcs.faces[0].gender)
bot.sendMessage(recipient.id, emotionsAnswer);
db.addRequest({
user: recipient,
description: desc.description.captions[0].text,
emotions: emotionsAnswer,
age: ageAnswer,
imageUri: imageUri,
})
})
} else {
db.addRequest({
user: recipient,
description: desc.description.captions[0].text,
imageUri: imageUri,
})
}
})
})
}
module.exports = {
processUpdate: update => bot.processUpdate(update)
}