-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (98 loc) · 2.9 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
var maps = require('./maps.js'),
config = require('./config.js'),
telegramBot = require('node-telegram-bot-api')
var mapsKey = config.googleMapsKey
var teleToken = config.telegramToken
var chatId = null
const makeBotResponse = (displayText) => {
const body = {
displayText,
speech: displayText,
data: {},
contextOut: [],
source: "lambda"
}
return {
"statusCode": 200,
"headers": {},
"body": JSON.stringify(body)
}
}
const searchLocation = (lat, lng, address, bank) => {
console.log('searchLocation', lat, lng, bank)
return new Promise( (resolve ,reject) => {
if( address ){
maps.searchNearByAddress(mapsKey, address, 'bank', bank, (err, res) => {
searchLocationCallback(err, res, resolve)
})
} else {
maps.searchNearByLoc(mapsKey, lat, lng, 'bank', bank, (err, res) => {
searchLocationCallback(err, res, resolve)
})
}
})
}
const searchLocationCallback = (err, res, resolve) => {
var responseText = ''
console.log(res)
if( res && res.name ){
responseText = 'Here is the nearest office from the provided location: ' + res.vicinity
if( chatId ){
console.log('sending venue to ' + chatId)
var bot = new telegramBot(teleToken);
bot.sendVenue(chatId, res.lat, res.lng, res.name, res.vicinity)
} else {
console.log('NO CHAT ID to send venue')
}
} else {
console.log('cannot find places', err)
responseText = 'Cannot find a location near you. :('
}
const apiResponse = makeBotResponse(responseText)
console.log(apiResponse)
resolve(apiResponse)
}
const getChatId = (data) => {
if( data.data && data.data.message && data.data.message.chat)
return data.data.message.chat.id
return null
}
const botResponse = (request) => {
if( request.metadata && request.metadata.intentName && request.contexts){
const contextParams = request.contexts[0].parameters
switch(request.metadata.intentName){
case 'userLocationAttachment':
return searchLocation(contextParams.lat, contextParams.lng, null, contextParams.bank)
break;
case 'userLocation':
return searchLocation(null, null, request.resolvedQuery, contextParams.bank)
break;
default:
return Promise.resolve('Opps, we have an error')
}
} else {
return Promise.resolve('Opps, we have an error')
}
}
const botRequest = (event, context, callback) => {
console.log('BOT REQUEST ACTION START')
console.log(event.body)
const body = JSON.parse(event.body)
const request = body.result
console.log(request)
console.log(request.metadata.intentName)
chatId = getChatId(body.originalRequest)
console.log('Chat id from: ' + chatId)
botResponse(request)
.then( result => {
console.log('results', result)
console.log('BOT REQUEST ACTION END')
callback(null, result)
})
.catch( err => {
console.log('error', err)
console.log('BOT REQUEST ACTION END')
callback(err)
})
};
exports.handler = botRequest