I'm trying to create a Telegram API bot for clients registration , so I chose the ' setWebHook ' method and wrote some codes :
<?php
include("config.php");
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$user_id = $update["message"]["chat"]["id"];
if( $update["message"]["text"] == "/start" OR $update["message"]["text"] == "/menu" ){
$keyboard = array(
'keyboard' => array(
array("📝 Register","🔑 Login")
),'one_time_keyboard'=>true,'resize_keyboard'=>true);
$replyKeyboard = json_encode($keyboard);
$replyMessage = "Hello 😊
Welcome to our bot ✋🏼
What do you want to do ?
.
";
}
if( $update["message"]["text"] == "📝 Register"){
$replyMessage = "Please enter username :";
}
$url = $bot_url.'sendMessage';
$data = array('chat_id' => $user_id,'text' => $replyMessage,'parse_mode' => 'Markdown','reply_markup' => $replyKeyboard );
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
)
);
$context = stream_context_create($options);
$update = #file_get_contents($url, false, $context);
?>
Ok so the robot shows the options for register or login , and when you choose register , it asks you username. It's ok up to here but ,
what I have to do for the rest of the registration process ??
How should I grab username and store it in database and after that grab password and store it and for other informations ?
I appreciate any logical ways for registering by telegram bots.
I wish I could completely describe my problem , I need your help my friends.
Thank you
( config.php includes database and bot token information )
As I understand from your question these are some tips maybe helpful for you:
Each message that delivers from telegram (users) have an id that tell which user sends the message(==chat_id a unique number).
Your bot should log messages and save the important parts of messages in a db. In this manner you have a track of user's conversation with your bot and you can recognize that for which question of your bot ,user answers. If you look for each Json object delivers from telegram you can see which data telegram reveals from user.
Related
I am trying to Ping a URL with name, email, and list variable in the URL. Exp:
https://myurl.com/?name=Example-Name&email=example#email.com&list=123456789hak.
So I am using $_GET to grab the variables from the URL, and want to then insert into a database using a POST array.
The code seems to grab the variables correctly, but the array fails inserting. Can anybody see where I am going wrong?
Language is PHP. This is supposed to insert contacts into Sendy via their API.
Here is the code:
<?php
//-------------------------- You need to set these --------------------------//
$your_installation_url = 'https://myURL.com'; //Your Sendy installation (without the trailing slash)
$api_key = 'API CODE FOR THE DATABASE IS HERE'; //Can be retrieved from your Sendy's main settings
$success_url = 'http://google.com'; //URL user will be redirected to if successfully subscribed
$fail_url = 'http://yahoo.com'; //URL user will be redirected to if subscribing fails
//---------------------------------------------------------------------------//
//POST variables
$name = $_GET['name'];
$email = $_GET['email'];
$list = $_GET['list'];
$boolean = 'true';
//Check fields
if($name=='' || $email=='')
{
echo 'Please fill in all fields.';
exit;
}
//Subscribe
$postdata = http_build_query(
array(
'name' => $name,
'email' => $email,
'list' => $list,
'api_key' => $api_key,
'boolean' => 'true'
)
);
$opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata));
$context = stream_context_create($opts);
$result = file_get_contents($your_installation_url.'/subscribe', false, $context);
//check result and redirect
if($result)
header("Location: $success_url");
else
header("Location: $fail_url");
?>
Thanks!
I program the bot telegram.
I want Get message_id By Bot ,when I sent a message to Group.
My Code is PHP.
$token = "MY_BOT's_TOKEN";
$data = [
'text' => 'my message here',
'chat_id' => 'the_chat_id_here'
];
file_get_contents("https://api.telegram.org/bot$token/sendMessage?" . http_build_query($data) );
How Do it?
best regard.
Your file_get_contents call returns a result containing status code and messageId.
$result = file_get_contents("https://api.telegram.org/bot$token/sendMessage?" . http_build_query($data) );
I'm using artdarek/oauth-4-laravel so the user can login via Facebook and post to their feed. I'm able to login in via Facebook using the following bit of code
// get data from input
$code = Input::get( 'code' );
// get fb service
$fb = OAuth::consumer( 'Facebook' );
// check if code is valid
// if code is provided get user data and sign in
if ( !empty( $code ) ) {
$token = $fb->requestAccessToken( $code );
$_SESSION['facebook_access_token'] = $token->getAccessToken();
// Send a request with it
$user = json_decode( $fb->request( '/me' ), true );
...
And everything works as expected. In the App I also have a share button, but when I try and share I get the following error
{"error":{"type":"OAuth\\Common\\Http\\Exception\\TokenResponseException","message":"Failed to request resource.","file":"\/var\/www\/html\/myApp\/vendor\/lusitanian\/oauth\/src\/OAuth\/Common\/Http\/Client\/StreamClient.php","line":68}}
Here is the code that I'm using for posting
if ( isset($_SESSION['facebook_access_token']) ) {
$fb = OAuth::consumer( 'Facebook' );
// $user = json_decode( $fb->request( "/me?access_token={$_SESSION['facebook_access_token']}" ), true );
$postMessage = json_decode( $fb->request
(
'POST',
"/me/feed?access_token={$_SESSION['facebook_access_token']}",
array (
'message' => 'This is a test message'
)
),
true );
return $postMessage;
}
I know that the $_SESSION[] is set because I ran the commented out line first and it was returning what I expected. But the code for posting on the wall is giving me that error mentioned earlier.
Please help.
Thanks
I found the mistake ... The call is supposed to be as follows
$postMessage = json_decode( $fb->request
(
"/me/feed?access_token={$_SESSION['facebook_access_token']}",
'POST',
array (
'message' => 'This is a test message'
)
),
true );
The route is supposed to be first and then the POST.
I am trying to post a status update on the wall of a group which I am a member of. Here is the code I am using
<?php
require 'facebook-php-sdk/src/facebook.php';
$appId = 'xxxxxxxxxxxxxxxx';
$appSecret = 'xxxxxxxxxxxxxxxx';
$extended_access_token = 'xxxxxxxxxxxxxxxxxxxxx';
$facebook = new Facebook(array('appId' => $appId, 'secret' => $appSecret));
$msg_body = array(
'message' => 'Good evening',
'type' => 'status',
'access_token' => $extended_access_token,
);
$groups = array(
'Group name' => '1234567',
);
foreach($groups as $group_name => $group_id){
try {
$post_url = "/$id/feed";
$postResult = $facebook->api($post_url, 'post', $msg_body );
print_r($postResult);
} catch (FacebookApiException $e) {
echo $e;
}
}
?>
If I login to fb via browser and hit this page in a new tab, the message is getting posted to the group wall. But If I am not logged into facebook, then if I hit this page, no message is getting posted and I am getting an error message
OAuthException: (#200) The user hasn't authorized the application to perform this action
How can I post to this group via offline mode? Searched a lot for this, could not find any useful info.
you need to have the permissions from the group owner
Change $id to $group_id and you'll be fine.
you ned to get apps this permession : publish_actions,publish_stream,user_groups,
I haven't tried messing with the facebook PHP API for months. Since template bundles are apparently now defunct, how can I publish a story into my users news feed for their friends? I've also already requested permissions.
Edit: The issue seems to arise from requested permissions not being set for the user when They are granted.
So far I have this
$appapikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$appsecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$facebook = new Facebook($appapikey, $appsecret);
$fb_user = $facebook->require_login();
try {
$facebook->api_client->feed_publishUserAction();
} catch(Exception $e) { }
Edit: I've looked through the facebook "api documentation" multiple times it's just not clear to me. I can't tell what's actually deprecated or not. They link to tutorials 2-3 years old!
If you have a problem with your iframe application reloading over and over and over try using
$facebook->require_frame()
Have you already looked up the topic on the facebook wiki? http://wiki.developers.facebook.com/index.php/Stream.publish
There's a nice example which should help you out. If not, you'll have to describe your problem more accurate.
EDIT: You can check and request for the permissions like this (and also request them)
function check_perms() {
global $facebook, $uid;
$data = $facebook->api_client->fql_query( "SELECT uid, publish_stream FROM permissions WHERE uid = " . $uid );
if( $data[0]['publish_stream'] != true ) {
echo '<br /><p>No \'publish_stream\' permissons found!<br />';
echo '<fb:prompt-permission perms="publish_stream"> Allow me to publish to your wall (*click*) </fb:prompt-permission>';
echo '<br />You\'ll have to refresh the page to continue.</p>';
die();
}
}
<?php
$message ="Your Message";
$attachment = array(
'name' => 'Application Name or message',
'href' => 'http://apps.facebook.com/tshirtquote',
'description' => 'Choose/Write your T-shirt Quote, Get A Tshirt Free printed with your Favorite Quote',
'media' => array(array('type' => 'image', 'src' => 'http://linkdoo.com/tshirtquote/images/tshirt1.JPG', 'href' => 'http://apps.facebook.com/tshirtquote/')),
);
$action_links = array( array('text' => 'WriteYourTShirtQuote', 'href' => 'http://apps.facebook.com/tshirtquote'));
$attachment = json_encode($attachment);
$action_links = json_encode($action_links);
$message = json_encode($message);
?>
<script>
var attachment = <?= $attachment ?>;
var message = <?= $message ?>;
var action_links = <?= $action_links ?>;
Facebook.streamPublish(message,attachment,action_links);
</script>
Use above script , It is most easy way to publish