-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViberBotkit.js
162 lines (137 loc) · 5.51 KB
/
ViberBotkit.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var ViberMessageTypes = require('./ViberMessageTypes');
function ViberBot(config) {
var server = require('express')();
var botkit = require('botkit');
var httpClient = require('request-promise');
var bodyParser = require('body-parser');
function setWebhook() {
server.use(bodyParser.json());
server.use('/' + removeHostFromWebhook(config.webhookUri) , function (req, res) {
controller.handleWebhookPayload(req, res);
});
server.listen(config.serverPort);
console.log("Bot is listening at port:", config.serverPort);
registerWebhookOnViberPlatfrom(config.webhookUri);
}
function removeHostFromWebhook(url) {
var urlSplit = url.split('/');
var host = urlSplit[0] + "//" + urlSplit[2] + "/";
return url.replace(host, '');
}
function registerWebhookOnViberPlatfrom(webhookUri) {
let opts = {
method: "POST",
uri: 'https://chatapi.viber.com/pa/set_webhook',
headers: {
"X-Viber-Auth-Token": config.viberToken,
"Content-Type": "application/json"
},
body: {
"url": webhookUri,
},
json: true
}
httpClient(opts)
.catch(function (err) {
console.log("Some problem happend while setting webhook.");
});
}
function sendMessageToViberPlatform(message) {
message.receiver = message.channel;
if (message.sender) {
message.sender.name = config.botName;
message.sender.avatar = config.botAvatar;
}
httpClient({
method: 'POST',
uri: 'https://chatapi.viber.com/pa/send_message',
headers: {
'X-Viber-Auth-Token': config.viberToken
},
body: message,
json: true
})
.catch(function (err) {
console.log("Some problem happend while sending message to Viber platform.");
});
}
var controller = botkit.core(config || {});
controller.defineBot(function (botkit, config) {
var bot = {
type: 'my_platform',
botkit: botkit,
config: config || {},
utterances: botkit.utterances,
}
// here is where you make the API call to SEND a message
// the message object should be in the proper format already
bot.send = function (message, cb) {
sendMessageToViberPlatform(message);
if (cb) {
cb();
}
}
// this function takes an incoming message (from a user) and an outgoing message (reply from bot)
// and ensures that the reply has the appropriate fields to appear as a reply
bot.reply = function (src, resp, cb) {
resp.channel = src.channel;
bot.say(resp, cb);
}
bot.startConversation = function (message, cb) {
botkit.startConversation(this, message, cb);
};
bot.createConversation = function (message, cb) {
botkit.createConversation(this, message, cb);
};
// this function defines the mechanism by which botkit looks for ongoing conversations
// probably leave as is!
bot.findConversation = function (message, cb) {
for (var t = 0; t < botkit.tasks.length; t++) {
for (var c = 0; c < botkit.tasks[t].convos.length; c++) {
if (
botkit.tasks[t].convos[c].isActive() &&
botkit.tasks[t].convos[c].source_message.user == message.user &&
botkit.excludedEvents.indexOf(message.type) == -1 // this type of message should not be included
) {
cb(botkit.tasks[t].convos[c]);
return;
}
}
}
cb();
};
return bot;
})
// provide one or more normalize middleware functions that take a raw incoming message
// and ensure that the key botkit fields are present -- user, channel, text, and type
controller.middleware.normalize.use(function (bot, message, next) {
message.user = message.raw_message && message.raw_message.sender ? message.raw_message.sender.id : null;
message.channel = message.raw_message && message.raw_message.sender ? message.raw_message.sender.id : null;
message.text = message.raw_message && message.raw_message.message && message.raw_message.message.text ? message.raw_message.message.text : null
message.type = message.event;
next();
});
// provide one or more ways to format outgoing messages from botkit messages into
// the necessary format required by the platform API
// at a minimum, copy all fields from `message` to `platform_message`
controller.middleware.format.use(function (bot, message, platform_message, next) {
for (var k in message) {
platform_message[k] = message[k]
}
next();
});
// provide a way to receive messages - normally by handling an incoming webhook as below!
controller.handleWebhookPayload = function (req, res) {
var payload = req.body;
controller.ingest(controller.spawn({}), payload, res);
res.status(200);
res.send({});
};
setWebhook();
controller.startTicking();
return controller;
}
module.exports = {
ViberBot: ViberBot ,
ViberMessageTypes: ViberMessageTypes
}