0

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.

4

3 回答 3

0

您提到为您的 webhook 使用 IP 地址。如果该 IP 是本地 IP,那么 Kik API 将无法向您的服务器发送消息。

我建议在开发过程中使用https://ngrok.com/之类的服务将您的服务器公开给 Kik。您所要做的就是ngrok start 8080在您的终端中运行,您将获得一个 URL,它将所有内容重定向到本地计算机上的 8080 端口。

于 2016-04-14T22:08:43.660 回答
0
let server = http
    .createServer(bot.incoming())
    .listen(process.env.PORT || 8080);

这行得通吗?

我不确定确切的问题是什么,但如果这肯定有效。

于 2016-04-13T15:23:59.720 回答
0

检查您是否正确设置了 webhook。在您的代码段中,您没有指定incomingPath选项,因此它将默认为/incoming,因此请确保您的 webhook 设置为http://1.2.3.5:80/incoming。对另一条路径的所有请求都将被丢弃。

否则,您可以通过执行指定它

let bot = new Bot({
    username: '...',
    apiKey: '...',
    incomingPath: '/yourincomingpath'
});
于 2016-04-15T14:34:10.680 回答