-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (92 loc) · 3.13 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
"use strict";
var express = require('express'),
request = require('request'),
bodyParser = require('body-parser'),
_ = require('underscore');
var helpers = require('./helpers/helpers'),
tg_commands = require('./telegram/commands'),
messenger = require('./messenger/messenger'),
nlp = require('./nlp/nlp');
/**
*
* If you want to add analytics just uncomment these lines
*
* var ua = require('universal-analytics');
* var visitor = ua(process.env.GA_KEY);
*
* // How to track a view
* visitor.pageview("/your-bot", "YourBotName", "http://telegram.me/YourBotName").send();
*
*/
/**
* process.env.TELEGRAM_BOT_TOKEN = Your Telegram bot token (without the word "bot" at the beginning)
*/
var app = express(),
telegram_token = process.env.TELEGRAM_BOT_TOKEN,
port = process.env.PORT || 8888 ;
/**
* Middleware that only parses json
*/
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
/***********************
* FACEBOOK MESSENGER
***********************/
/**
* GET request from FB to authorize your app
* process.env.FB_VALIDATION_TOKEN = Token created from you and set in your FB page settings
*/
app.get('/fb', function(req, res) {
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === process.env.FB_VALIDATION_TOKEN) {
console.log("Validating webhook");
res.status(200).send(req.query['hub.challenge']);
} else {
console.error("Failed validation. Make sure the validation tokens match.");
res.sendStatus(403);
}
});
/**
* POST request from FB when users send you a message
*/
app.post('/fb', function (req, res) {
if (req.body.entry && req.body.entry.length>0) {
var messaging_events = req.body.entry[0].messaging;
for (var i = 0; i < messaging_events.length; i++) {
var event = req.body.entry[0].messaging[i];
var sender = event.sender.id;
if (event.message && event.message.text) {
var text = event.message.text;
//messenger.sendTextMessage(sender, "Text received, echo: " + text, function(res, err){});
}
}
}
res.sendStatus(200);
});
/***********************
* TELEGRAM
***********************/
/**
* POST request from TELEGRAM when users send you a message
*/
app.post("/telegram", function(req, res) {
var chat_id = req.body.message.chat.id,
user_text = req.body.message.text;
var username = req.body.message.chat.username;
switch (helpers.messageType(req)) {
case 'text':
if (helpers.isCommand(user_text)) {
//tg_commands.sendSimpleMessage(chat_id, telegram_token, "Welcome "+user.info.telegram_username+"!", function(err) {});
} else {
//tg_commands.sendSimpleMessage(chat_id, telegram_token, "Welcome "+user.info.telegram_username+"!", function(err) {});
}
break;
case 'location':
var longitude = location.longitude;
var latitude = location.latitude;
break;
};
res.sendStatus(200);
});
app.listen(port);
console.log('Server running with love on port ' + port);