我无法从我的Kik机器人获得回复。 webhook配置 似乎是正确的,因为我可以成功地HTTP“获取”webhook并且它接收消息(当我在Kik向机器人发送消息时,我收到“S”,“D”,“R”,发送,交付,接收,更新)。但是,我从未收到过回复的消息。我正在使用php和 cURL和webhook没有在本地运行。
我在网上找到了一些cURL和php代码段,但没有任何内容表明是否需要任何特殊标题以及Kik Auth凭据是如何发送的。我尝试了几种方法,包括官方Kik文档中的建议,但没有成功。
代码如下,替换了bot和API ID。感谢任何帮助。
<?php
// ID and token
$botID = 'mybot';
$authToken = 'longAPIvaluefromKik';
$newToken=$botID.':'.$authToken;
$update = file_get_contents("php://input");
$data = json_decode($update, true);
foreach ($data['messages'] as $message) {
$chatId = $message['chatId'];
$to = $message['from'];
// The data to send to the API
$postData = array(
'messages' => array('body' => 'Hello', 'to' => $to, 'type' => 'text', 'chatId' => $chatId )
);
$headers = array(
'Content-Type:application/json'
);
$user_data = json_encode($postData);
$request_url = 'https://api.kik.com/v1/message';
// cURL
$curlD = curl_init();
curl_setopt($curlD, CURLOPT_URL, $request_url);
curl_setopt($curlD, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($curlD, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curlD, CURLOPT_USERPWD, $newToken);
curl_setopt($curlD, CURLOPT_POSTFIELDS, $user_data); // Set POST data
curl_setopt($curlD, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($curlD);
curl_close($curlD);
}
?>
答案 0 :(得分:0)
虽然Kik没有PHP示例 - 但JSON示例应该可以帮助您进行机器人讨论。 https://dev.kik.com/#/docs/messaging
发送邮件的curl(命令行)版本如下所示:
curl \
-u "<username>:<api_key>" \
-H "Content-Type: application/json" \
-X "POST" \
-d '{"messages": [{"body": "bar", "to": "laura", "type": "text", "chatId": "b3be3bc15dbe59931666c06290abd944aaa769bb2ecaaf859bfb65678880afab"}]}'\
https://api.kik.com/v1/message
答案 1 :(得分:0)
很抱歉,我使用的是相同的代码,我的问题是ssl证书。添加了以下内容:curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
看起来很有魅力!
//编辑
哦,尽管我不推荐它,但我忘记了一些东西发送此消息:
$user_data = '{
"messages" : [
{
"body" : "hey!",
"to" : "' . $to . '",
"type" : "text",
"chatId" : "' . $chatId . '"
}
]
}';
而不是JSON_encode()