designdit
designdit

Reputation: 859

Kik bot using cURL and php - no response

I'm having trouble getting a response from my Kik bot. The webhook config seems to be correct since I can successfully HTTP "Get" the webhook and it receives messages (when I send the bot a message in Kik, I receive the "S", "D", "R", sent, delivered, received, updates). However, I never receive a message back. I'm using php and cURL and the webhook is not running locally.

I've found a few cURL and php snippets online but nothing that indicates whether any special headers are required and exactly how the Kik Auth credentials need to be sent. I've tried several ways, including what's suggested in the official Kik documentation, but no success.

The code is below with the bot and API ids replaced. Appreciate any help.

<?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);		
	
}
?>

Upvotes: 0

Views: 537

Answers (2)

kriskotoo BG
kriskotoo BG

Reputation: 321

sorry for the late reply, im using the same code and what my problem was the ssl sertificate. added the following:curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); and it worked liek a charm!

//EDIT

oh and something i forgot although i dont recommend it send this:

$user_data = '{
        "messages" : [
            {
                "body" : "hey!",
                "to" : "' . $to . '",
                "type" : "text",
                "chatId" : "' . $chatId . '"
            }
        ]
    }';

and not the result of JSON_encode()

Upvotes: 0

David
David

Reputation: 1

While Kik doesn't have PHP examples- the JSON examples should help you get your bot talking. https://dev.kik.com/#/docs/messaging

The curl (command line) version of sending a message would look like this:

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

Upvotes: 0

Related Questions