-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (38 loc) · 1.63 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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
const axios = require('axios')
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({
extended: true
})); // for parsing application/x-www-form-urlencoded
//This is the route the API will call
app.post('/new-message', function(req, res) {
const {message} = req.body
//Each message contains "text" and a "chat" object, which has an "id" which is the chat id
if (!message || message.text.toLowerCase().indexOf('deepak') <0) {
// In case a message is not present, or if our message does not have the word marco in it, do nothing and return an empty response
return res.end()
}
// If we've gotten this far, it means that we have received a message containing the word "marco".
// Respond by hitting the telegram bot API and responding to the approprite chat_id with the word "Polo!!"
// Remember to use your own API toked instead of the one below "https://api.telegram.org/bot<your_api_token>/sendMessage"
axios.post('https://api.telegram.org/bot446623220:AAGo_zxKw48yPhk0gvIAkvavJKPRCtiJZuA/sendMessage', {
chat_id: message.chat.id,
text: 'Rayathurai!!'
})
.then(response => {
// We get here if the message was successfully posted
console.log('Message posted')
res.end('ok')
})
.catch(err => {
// ...and here if it was not
console.log('Error :', err)
res.end('Error :' + err)
})
});
// Finally, start our server
app.listen(3000, function() {
console.log('Telegram app listening on port 3000!');
});