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.
Related
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;
}
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 .
I'm trying to integrate Twilio onto my Wordpress site.
The idea is to allow users to type in their phone number to get a download link to our app. We've put up a simple form here - http://evntr.co/download - and upon submitting the form, the EvntrPHP.php code is run.
Using template code, we've easily been able to get the form to send a message to a verified number (the one currently in the To field) using a free Twilio number. However, when we add the StatusCallback parameter, it never calls our callback.php code. Both EvntrPHP.php and callback.php are in the root directory - evntr.co/.
<?php
require 'twiliophp/Services/Twilio.php';
$AccountSid = "--------";
$AuthToken = "---------";
$client = new Services_Twilio($AccountSid, $AuthToken);
$phonenum = $_POST["phonenum"];
$callbackURL = "https://evntr.co/callback.php";
$client->account->messages->create(array(
'To' => "XXXXXXXXXX",
'From' => "+XXXXXXXXXX",
'Body' => "Test Message",
'StatusCallback' => "https://evntr.co/callback.php",
));
?>
My understanding is that the flow should be like this:
user navigates to evntr.co/download
user submits the form with their number
form calls EvntrPHP.php and sends a text message to their number
Twilio POSTs to callback.php whenever the status of the message changes (Sent, Delivered, Canceled, etc).
However, every time I submit the form, the message is sent and the page just stays at evntr.co/EvntrPHP.php and never loads callback.php. Maybe this is a misunderstanding on my part with how Callback URLs work? Or maybe the StatusCallback parameter doesn't work with a free Twilio number?
Twilio developer evangelist here.
You are correct that the Twilio callbacks are not working as you expect. As McCann points out, the request is made asynchronously from Twilio to the URL you supply. You can use the callback to keep track of the progress of the message, but not affect the request the user has made.
So, in your example you either want to render something after you have sent the message:
<?php
require 'twiliophp/Services/Twilio.php';
// other stuff
$client->account->messages->create(array(
'To' => $phonenum,
'From' => "+14708655xxx",
'Body' => "Test Message",
'StatusCallback' => "https://evntr.co/callback.php",
));
?>
<h1>You should receive an SMS, click the link in the SMS to download the app on the platform of choice.</h1>
With some more style than a plain <h1> of course! Or, you could redirect to a page with a success message on. (PHP is not my strongest subject, but discussions of redirects are rife on this StackOverflow question)
Good luck with the app, and let me know if you have any more Twilio questions!
[edit]
As discussed in the comments, if you want to see if the API request was successful you'll want to do something like:
<?php
require 'twiliophp/Services/Twilio.php';
// other stuff
try {
$client->account->messages->create(array(
'To' => $phonenum,
'From' => "+14708655xxx",
'Body' => "Test Message",
'StatusCallback' => "https://evntr.co/callback.php",
));
// Success! Redirect to success page!
header("Location: http://evntr.co/success.php");
die();
} catch (Services_Twilio_RestException $e) {
// Something went wrong!
// Do something about it!
}
?>
So, wrapping the API call in a try/catch block and responding appropriately should catch most errors from incorrect phone numbers or other API errors. It won't guarantee that the SMS has been delivered (you'll get that from the callback webhook) but it will guarantee that you've done all you can to get the SMS sent.
The callback doesn't work like you think it does.
Call End Callback (StatusCallback) Requests
After receiving a call, requesting TwiML from your app, processing it, and finally ending the call, Twilio will make an asynchronous HTTP request to the StatusCallback URL configured for the called Twilio number (if there is one). By providing a StatusCallback URL for your Twilio number and capturing this request you can determine when a call ends and receive information about the call.
-- https://www.twilio.com/docs/api/twiml/twilio_request
I am trying to send a message by the WhatsApp API using PHP. I have the WhatsApp password and am getting by WART using the following code:
<?php
require "whatsapp.class.php";
// DEMO OF USAGE
$wa = new WhatsApp("91XXXXXXXXXX", "XXX-XXX", "Nick Name");
$wa->Connect();
$t = $wa->Login();
$wa->Message("5","91XXXXXXXXXX","Good code");
echo "Message sent";
?>
I did not change anything in the whatsapp.class.php file.
My files are:
http://vvsindia.com/stackoverflow/whatsapp.class.txt
http://vvsindia.com/stackoverflow/func.txt
http://vvsindia.com/stackoverflow/decode.txt
For your convenience to view while browsing, I just uploaded them as a txt file, but originally these are PHP files.
Using the above code I was not able to send any message. What could the issue be?
You can use the below script to send a message from WhatsApp in PHP.
https://github.com/venomous0x/WhatsAPI/tree/master/examples
Configure the source code in Apache and run examples/whatsapp.php file.
You have to change the below configurations.
// Simple password to view this script
$config['webpassword'] = 'MakeUpPassword';
and
$config['YOURNAME'] = array(
'id' => 'e807f1fcf82d132f9bb018ca6738a19f',
'fromNumber' => '441234567890',
'nick' => "YOURNICKNAME",
'waPassword' => "EsdfsawS+/ffdskjsdhwebdgxbs=",
'email' => 'testemail#gmail.com',
'emailPassword' => 'gmailpassword'
);
You should rather try this quick and easy interface API:
https://www.mashape.com/motp/whatsapp-pusher
As given in the documentation, sending a text message to a WhatsApp user would be a one-step process. Below is a sample cURL call to send a text message to a WhatsApp user.
curl -XPOST 'http://api.dial2verify.com/WHAPP/SEND/<API_KEY>/<Phone_ISD>' \
-d 'Msg=Text to image URL here'
To get the API key, you are required to drop a request to hello#dial2verify.in, and they would provide you a free API key.
Phone_ISD: should be a complete phone number including ISD code (for example, 919922003300).
I am sending emails via SendGrid with this inside the x-smtpapi header
$json_string = array(
'unique_args' => array (
'email_id' => 1
)
);
It all seems to send okay, inside SendGrid I can view the "email_id" in the Email activity under Unique Args.
However when I try to use the API to look at this email, I cannot find a way to actually get these unique arguments from the API.
I am using this to try and get the headers returned with the bounced emails.
$request = 'https://api.sendgrid.com/api/bounces.get.json&api_user=username&api_key=password'
All I get is just the email addresses that have bounced, not the header information (the unique arguments)
I want to know, is it possible to get the unique arguments from the API. I have read it multiple times to no avail.
I hope this makes sense.
Thanks
At present there's no way request to lookup specific events by unique_arg with the Web API.
However, the SendGrid Event Webhook will give you granular data on each event, such as a bounce as it happens. The Event Webhook POSTs data to your server every time an action is taken upon an email (e.g. open, click, bounce).
Once you receive it, you're responsible for storing this, although it's not a typical API it gives very specific data on events which you can then compile and rehash however you like.
To get started using the webhook, you'll do something like the following, and have SendGrid POST to the following script:
<?php
$data = file_get_contents("php://input");
$events = json_decode($data, true);
foreach ($events as $event) {
// Here, you now have each event and can process them how you like
process_event($event);
}
[Taken from the SendGrid Webhook Code Example]