php: how can I handle telegram bot request error? - php

I use this: https://telegram-bot-sdk.readme.io/docs
$response = $telegram->sendMessage([
'chat_id' => '#username',
'text' => 'Hello World'
]);
when the username is true, it works. but how can I handle errors? for example when username is wrong or not exist.

1-Some people does not have username!(you can have telegram account with just first name and valid phone number)
2-You cannot send to any valid username(Your bot can just send to users subscribed to your bot (open a chat at least once) and not blocked afterwards)
3-By telegram you can just send to users by chat_id( a large unique number) and not by #username .

Related

I need to send long telegram message through php url request

I need to send a long message to a telegram group with a bot. The message comes from a wordpress site after placing an order,I need to send all raw data an order creates to the telegram. The bot and php code works so far, it is sending me the message if I type something basic, but when I put the order information it doesn´t send any message.
I am looking for some code that somehow parses this information and makes it url ready to send to telegram.
This is my code:
* SEND ORDER TO TELEGRAM GROUP
*/
$apiToken = "Token";
$data = [
'chat_id' => 'ChatId',
'text' => 'New Order, Open App'
];
$response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" .http_build_query($data) );
/**
* SEND ORDER TO TELEGRAM GROUP
*/
Maybe someone did it already previously?
Thanks.

How to send Push with different data for each token in Firebase?

There is such a code, but it sends a single message to all the tokens specified in the array (1000 tokens). You need to add a personal parameter for each token somewhere to track the unique click - from whom it comes and so on. For example, in click_action. But again, if you add a parameter there, the link in this form will be sent to everyone. Sending one message for each user is not an option, of course.
'registration_ids' => $tokens,
'data' => array(
'title' => 'Title',
'body' => 'Message',
'icon' => 'icon.png',
'image' => 'image.png',
'click_action' => 'https://site.ru'
);
There is no (mail merge like) customization for each recipient within a single API call. Each call to the FCM API to send a message, sends precisely the message you pass in that call to the topics/tokens you target in that call.
If you want recipients to receive a different message, you will need to perform one API call for each message.
You say:
Sending one message for each user is not an option
Calling the API fo each unique message is precisely the only option. I'm not sure why it wouldn't be, as it's what all apps using FCM do if they want to deliver personalized content.

Telegram Bot Not Sending Messages To User

I am in a hurry of completing a telegram bot project. I am using Telegram bot sdk (https://telegram-bot-sdk.readme.io/) with Laravel. The problem is that I've stored all my users chat_id in database and while I am trying to send a message to all my users(I do consider Telegram limitations mentioned in its official docs) the app will be blocked and none of the users get any message. Here is the simple code $Telegram::sendMessage(['chat_id' => 'CHAT_ID', 'text' => 'Hello World']);
i remember i faced to your problem when i develop the telegram bot , i just use the method in my cladd to send messages and my problem is solved , this is the method based on the library :
use Telegram\Bot\Api;
public function sendMsg($botToken,$chat_id,$text)
{
$telegram = new Api($botToken);
$response = $telegram->sendMessage([
'chat_id' => $chat_id,
'text' => $text,
]);
return $response;
}

Can we get email ID from Twitter oauth API?

How to get email id of the user who accepted my Twitter application?
I have gone through lot of forums. But they have mentioned, it is not possible. Also those posts are older than a year. May I know whether it is possible to get the user email id through twitter API using PHP?
I am getting Twitter user details using the following URL:
https://api.twitter.com/1.1/account/verify_credentials.json
This is now possible by filling out a form to request elevated permissions:
Go to https://support.twitter.com/forms/platform
Select "I need access to special permissions"
Enter Application Name and ID. These can be obtained via https://apps.twitter.com/ -- the application ID is the numeric part in the browser's address bar after you click your app.
Permissions Request: "Email address"
Submit & wait for response
After your request is granted, an addition permission setting is added in your twitter app's "Permission" section. Go to "Additional Permissions" and just tick the checkbox for "Request email addresses from users".
Note that user e-mail address has to be verified, otherwise Twitter refuses to provide it. (See include_email parameter description in elevated permissions doc page.)
Now you can fetch user email address from twitter API and it's a lot much easier. Just follow these steps...
Goto Twitter Apps
Click on 'Create New App'
Fill all required credentials and click on 'Create your Twitter application'
Now click on 'Permissions' tab -> check 'Request email addresses from users' field and click on 'Update Settings'. (check given picture)
Now in your PHP code, set all app details and add this code!
$params = array('include_email' => 'true', 'include_entities' => 'false', 'skip_status' => 'true');
$data = $connection->get('account/verify_credentials', $params); // get the data
// getting twitter user profile details
$twt_id = $data->id; //twitter user id
$twt_email = $data->email; //twitter user email
All Done.
Hope it help you, good luck. ;)
It is now possible to retrieve a user's email address from Twitter (if the user permits it, of course).
You'll need to apply to have your application white-listed to receive xAuth.
Check here for more info:
https://dev.twitter.com/rest/reference/get/account/verify_credentials
Yes you can get email address.This is now possible by filling out a some permission.
1.Go to this Link: https://apps.twitter.com/
2.After go to permission tab inside your created Application
3.Select Addition permission checkbox in your APP.
4.After successfully update setting you can get email address from Twitter.
If in some case you can not getting email address then please check your Oauth Api request url.
Your request url should be this type :https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true
It is not possible to get user's email address from twitter. You can see it here. You can open a form page on your callback page and get user's email address on that page. You can refer here for example usage
Here is how I have done this in ASP.Net using linqtoTwitter library
http://www.bigbrainintelligence.com/Post/get-users-email-address-from-twitter-oauth-ap
// call verify credentials api
var twitterCtx = new TwitterContext(authTwitter);
var verifyResponse =
await
(from acct in twitterCtx.Account
where (acct.Type == AccountType.VerifyCredentials) && (acct.IncludeEmail == true)
select acct)
.SingleOrDefaultAsync();
if (verifyResponse != null && verifyResponse.User != null)
{
User twitterUser = verifyResponse.User;
//assign email to existing authentication object
loginInfo.Email = twitterUser.Email;
}

how send message facebook friend through graph api using Accessstoken

Can anyone help me to send message to facebook friends using graph api.
I tried
$response = $facebook->call_api("/me/feed", "post", "to=john","message=You have a Test message");
It's not working.
I have the accesstoken of the user in my hand.only I am confused on sending process.
You can't send messages using a Facebook application. You used to be able to do that, but the (predictable?) colossal amount of abuse led to the revocation of this ability.
Provided Alice, your user, has given you the necessary extended permissions, you have the following options:
Post to Alice's wall on her behalf
Send email to Alice
Create events on behalf of Alice
invite Bob (not your user) to said events
Issue a request/invitation on behalf of Alice to Bob
Issue a request from the App to Alice
You could open the Send Dialog in a popup.
$parameters = array(
'app_id' => $facebook->getAppId(),
'to' => $facebookUserId,
'link' => 'http://google.nl/',
'redirect_uri' => 'http://my.app.url/callback'
);
$url = 'http://www.facebook.com/dialog/send?'.http_build_query($parameters);
echo '<script type="text/javascript">window.open('.json_encode($url).', ...
For detailed options see:
https://developers.facebook.com/docs/reference/dialogs/send/
$attachment = array(
'access_token' => $access_token,
'message' => $msg,
'name' => $name,
'link' => $link,
'description' => $desc,
);
facebook->api('/'.$uesr_id.'/feed', 'POST', $attachment);
Technically you can do feed or cross feed post with privacy settings that allows only the feed owner to see the post but its not really sending a message to a person.
You can use
HTTP POST with
PATH
https://graph.facebook.com/friend_facebook_id/feed
PARAMETER
MESSAGE = your message
ACCESS_TOKEN = your oauth2 access token
You can send to their facebook email. Facebook email is consisting profile nickname+'#facebook.com'. The email will goes to their facebook inbox message. Note that facebook email does not accept spoofing email. You will need whitelabel domain or use SendGrid.
You will need to integrate xmpp chat to reply a message and to write a new message.
I saw this post and noticed it was not right. Using the javascriot api you can post to a friend's feed like so:
In this example "friendID" is the FB user ID of the friend.
This api call requires the "publish_stream" permission.
FB.api('/'+friendID+'/feed', 'post',
{
method: 'feed',
message: messageText,
name: 'write a title here',
caption: 'Put a caption here.',
description: 'Put your description here.',
link: 'http://stackoverflow.com/questions/2943297/how-send-message-facebook-friend-through-graph-api-using-accessstoken',
picture: 'link to the preview thumbnail',
},
function(response) {
if (!response || response.error) {
//alert('Error occured');
} else {
//alert('Post ID: ' + response.id);
}
});
So this does it with the javasfcript SDK- the PHP method must be similar.
Instead of using the below code
[facebook dialog:#"feed"
andParams:params
andDelegate:self];
Use the following solution
[facebook requestWithGraphPath:#"me/feed"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];

Categories