我在ubuntu服務器(14.04.4 LTS)上運行node.js(v5.10.1)。 已安裝npm(@ kikinteractive/kik)。 (https://www.npmjs.com/package/@kikinteractive/kik)kik bot服務器接收POST但bot.onTextMessage(...)沒有響應
這是我寫的server.js文件:
'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);
注:被用於KIK配置網絡掛接一個IP地址,服務器偵聽端口80
我知道我的KIK配置似乎是正確的,因爲從KIK應用結果在我的服務器,這裏顯示POST請求的文本消息(關鍵數據已被替換爲這個貼出的問題):
{「主機」 :「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「}並確認在KIK應用程序收到消息:
bot.send(Bot.Message.text( '什麼!'), '用戶名');
所以,問題是:如果我的配置似乎是正確的,因爲我能夠在我的服務器上驗證POST,並且kik npm已正確安裝,因爲我可以將消息從我的服務器發送到kik,爲什麼bot.incoming()和bot.onTextMessage只是坐在那裏呈現我的服務器一個大的,笨的,昂貴的磚塊?我錯過了什麼?
任何幫助,非常感謝。
謝謝。
澄清指定:無論我在onTextMessage處理器都試過了,使用Kik的文檔提供的所有例子,任何我在其他地方發現的,沒有的server.js示例在我的服務器上按預期工作;但是當我添加console.log(請求。頭文件),我可以看到我的服務器確實收到了來自Kik的POST。什麼? – tbmac3