FB Messanger bot - can not make sending messages work - php

I am making a FB messenger bot. I can not get the part with making the bot sending a message back to work.
I am using Heroku for the Webhook.
I am following this tutorial: https://www.youtube.com/watch?v=__SWFhHocDI
Here is my code (I changed the token variable, I do use the real token):
file_put_contents('fb.txt', file_get_contents('php://input'));
$fb = file_get_contents('fb.txt');
$fb = json_decode($fb);
$rid = $fb->entry[0]->messaging[0]->sender->id;
$token = "MYTOKEN";
$data = array(
'recipient' => array('id'=>"$rid"),
'message' => array('text'=>"Nice to meet you!")
);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode($data),
'header' => "Content-Type: application/json\n"
)
);
$context = stream_context_create($options);
file_get_contents("https://graph.facebook.com/v2.8/me/subscribed_apps?access_token=$token", false, $context);
Can you find any problems? :-) Thank you for taking time.

maybe you should try something like this -
file_get_contents("https://graph.facebook.com/v2.6/me/messages?access_token=$token", false, $context);

Related

Messenger Chat Bot sending too many messages

Having some problem on the chatbot that I'm currently developing it replies to messages nonstop.
I'm pretty new to this, the code below I just copied the code below to a youtube video the code is working but I'm not able to get why it was sending too many messages
<?php
file_put_contents("fb.txt", file_get_contents("php://input"));
$fb = file_get_contents('fb.txt');
$fb = json_decode($fb);
$sid = $fb->entry[0]->messaging[0]->sender->id;
if($sid == "105140841657941"){
exit();
}
$data = array(
'messaging_type' => 'RESPONSE',
'recipient' => array('id' => "$sid"),
'messsage' => array('text' => "Nice to Meet you")
);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode($data),
'header' => "Content-type: application/json\n"
)
);
$context = stream_context_create($options);
$token = "SAMPLE TOKEN";
file_get_contents("https://graph.facebook.com/v10.0/me/messages?access_token=$token", false, $context);
file_put_contents("fb.txt", "");
?>

reCAPTCHA v.2 file_get_contents error

I made custom register page on wordpress using google recaptcha. It worked fine, without any errors but when i copied a code to new project it isnt working. No clue why. No secret and sitekey errors.
if(!empty($_POST['g-recaptcha-response']))
{
$gurl = 'https://www.google.com/recaptcha/api/siteverify';
$gdata = array(
'secret' => 'secret',
'response' => $_POST['g-recaptcha-response']
);
// use key 'http' even if you send the request to https://...
$goptions = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($gdata),
),
);
$gcontext = stream_context_create($goptions);
$gresult = file_get_contents($gurl, false, $gcontext);
var_dump($_POST['g-recaptcha-response']);
$gresult=json_decode($gresult);
if(!$gresult->success)
$register_errors[]='Error reCAPTCHA.';
}
UPDATE:
output of var_dump($_POST['g-recaptcha-response']) is some string propably hashed. $gresult = file_get_contents($gurl, false, $gcontext); is giving FALSE on var_dump($gresult);

How to log into a web service with php post request

So i need to gain access to a web service containing some json, but to do so I was told to make use of PHP POST method to first log into the web service. I was giving an array with 3 types/values.
{
"Username":"user",
"password":"1234",
"LoginClient":"user"
}
I have been searching all day for a solution, but have come up short :(.
Any advice or push into a right direction would be much appreciated.
Hope I have explained this clearly enough.
you could do as follows:
$url = 'http://yourDomain.net/api/auth/';
$data = array('Username' => 'user', 'password' => '1234', 'LoginClient' => 'user');
$opts = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
)
);
$context = stream_context_create($opts); //Creates and returns a stream context with any options supplied in options preset.
$response = file_get_contents($url, false, $context);
var_dump($response);
Or you could read about CURL as another option to make POST requests.

When calling the MediaWiki-API I only get '+\' as an csrf-token

I can edit with the simple token '+\' but with this simple token I can edit only as an unregistered IP, not as an registered user. Can someone help me?
My PHP code:
$parameters = array('action' => 'query', 'meta' => 'tokens', 'format' => 'json');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($parameters),
),
);
$context = stream_context_create($options);
$result = file_get_contents($wiki, false, $context);
echo "$result";
You need to be logged in to get a real CSRF token. For details on how to log in, see:
http://www.mediawiki.org/wiki/API:Login
Note that staying logged in will require that you store cookies across requests. This will be difficult with file_get_contents(); using cURL with a cookie jar is recommended.
To get a login token use the type=login parameter. Example:
curl 'https://www.mediawiki.org/w/api.php?action=query&meta=tokens&format=json&type=login'

Linkedin Invitation API Internal server error

Yo!
I'm trying to use the linkedin invitation api to allow users to conncect on linkedin from my application using email-addresses. I am able to find people, access the api and so on. I can't get the invites to work though. I am using php (Laravel).
I based myself on the example from the linkedin documentation ( Linkedin Invite API ). I send my data in a post using JSON (that contains the same info as their example).
I ask permission to use w_messages, the post works and my variables contain the correct information. I get a Internal Server error as a result.
$data = array(
"recipients" => array(
"values" => array(
"person" => array(
"_path" => "/people/email=".$email,
"first-name" => $firstname,
"last-name" => $lastname
)
)
),
"subject" => "Bla",
"body"=> "BlaBLa",
"item-content" => array(
"invitation-request" => array(
"connect-type" => "friend"
)
)
);
$dataString = json_encode($data);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/json\r\n".
"Connection: close\r\n" .
"Content-length: " . strlen($dataString) . "\r\n",
'content' => $dataString
)
);
$params = array('oauth2_access_token' => Session::get('access_token'),
'format' => 'json'
);
$url = "https://api.linkedin.com/v1/people/~/mailbox".'?' . http_build_query($params);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
Log::info($result);
return Response::json(array("invite" => "sent"));
I assume I'm doing something wrong but don't really know where to look.
Looks like you doing this manually, have you tried using a tried & tested third party library like simple-linkedinphp - A PHP-based wrapper for the LinkedIn API.
https://code.google.com/p/simple-linkedinphp/wiki/Reference
Usage:
// Connect
$API_CONFIG = array(
'appKey' => '<your application key here>',
'appSecret' => '<your application secret here>',
'callbackUrl' => NULL
);
$linkedin = new LinkedIn($API_CONFIG);
// Send Invite
$linkedin->invite($method, $recipient, $subject, $body, $type = 'friend');
Doc: https://code.google.com/p/simple-linkedinphp/wiki/Reference

Categories