I am running node.js (v5.10.1) on an ubuntu server (14.04.4 LTS). And the npm (@kikinteractive/kik) has been installed. (https://www.npmjs.com/package/@kikinteractive/kik)
Here is the server.js file I have written:
'use strict';
const util = require('util');
const http = require('http');
const Bot = require('@kikinteractive/kik');
const port = 80;
let bot = new Bot({
username: 'botnamehere',
apiKey: 'blablabla-1001-0110-1001-2112blablabla'
});
bot.onTextMessage((message)=>{
message.reply(message.body);
});
var server = http.createServer(bot.incoming());
server.on('request', function (request, response) {
bot.incoming();
// I added this to write the request
// so that I could verify my server was receiving from kik
console.log(request.method);
console.log(request.headers);
console.log(request.url);
var fs = require('fs');
fs.writeFile("./logfile.log", JSON.stringify(request.headers), function(err) {
if(err) {
return console.log(err);
}
console.log("The kik request was saved!");
});
});
server.listen(port);
Note: An IP address was used for the kik configuration webhook, and the server is listening on port 80.
I know that my kik configuration seems to be correct as the text messages from the kik app result in POST requests at my server, shown here (key data has been replaced for this posted question):
{ "host": "ipaddressofhost", "x-kik-signature": "8EEBLA44C3BB9769BLAE56E7E9CBLA2BA4179445", "content-type": "application/json", "x-kik-username": "botname", "x-cloud-trace-context": "272c0f7616d6189bla9540d1e47668f5/5407793903926111947", "content-length": "307", "connection": "Keep-alive", "user-agent": "AppEngine-Google; (+http://code.google.com/appengine; appid: s~bot-dashboard)", "accept-encoding": "gzip,deflate,br" }
And I know that my server can send messages to kik, as I have tested the following and confirmed the message received in the kik app:
bot.send(Bot.Message.text('What!'), 'username');
So the question is: If my configuration seems correct, in that I am able to verify a POST at my server, and the kik npm is installed correctly as I am able to send messages from my server to kik, why is bot.incoming() and bot.onTextMessage just sitting there rendering my server a big, dumb, expensive brick? What am I missing?
Any help is greatly appreciated.
Thank you.
Check that you set your webhook properly.
In your snippet, you don't specify the incomingPath
option, so it will default to /incoming
, so make sure your webhook is set to http://1.2.3.5:80/incoming
. All requests to another path will be dropped.
Otherwise, you can specify it by doing
let bot = new Bot({
username: '...',
apiKey: '...',
incomingPath: '/yourincomingpath'
});