Sending a message by the WhatsApp API using PHP - php

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).

Related

Graph API: Send mail with attachment by drive item ID?

I'd like to send mail via the Graph API and attach a file by its drive item ID.
At the moment I can successfully send email via the Graph API and attach a file that is on my local server.
However the file originates on OneDrive so the current situation is I have to download the file to my server then re-upload it via the sendMail endpoint as an attachment and then delete it from my server.
This seems like an unneeded step if it's possible to just provide the file ID and let office 365 resolve it all locally.
$mailBody = ...
'attachments' => [
[
'#odata.type' => '#microsoft.graph.fileAttachment',
'Name' => 'file.docx',
'ContentBytes' => $localFile
// 'DriveID' => 'possibly this instead of Content Bytes?'
]
]
...
$response = $this->getGraph()->createRequest("POST", "/users/{primary-user}/sendMail")
->attachBody($mailBody)
->execute();
You can attach a file by its drive item ID, it is called referenceAttachment but not the fileAttachment in your code. The v1.0 edition has very limited support for referenceAttachment. And by that I mean, there isn't much you can do with them beyond acknowledging one exists.
Reference from egorbunov's answer: Send reference attachment to email via Graph API
Create the message draft using POST request to https://graph.microsoft.com/beta/me/messages with payload:
{
"subject": "TestMessage",
"toRecipients": [
{
"emailAddress":{
"address":"egor-mailbox#ya.ru"
}
}
],
"body": {
"contentType": "html",
"content": "<b>Hello!</b>"
}
},
As a response you will get the whole message structure with id set to something like
AQMkADAwATMwMAItMTJkYi03YjFjLTAwAi0wMAoARgAAA_hRKmxc6QpJks9QJkO5R50HAP6mz4np5UJHkvaxWZjGproAAAIBDwAAAP6mz4np5UJHkvaxWZjGproAAAAUZT2jAAAA.
Lets refer to it as {messageID}. NOTE: as you can see I have passed
html-typed body. This is needed because (at least in GraphAPI
Explorer) graph api returns error in case you are trying to add
reference attachment to message with non-html body content-type.
After that you can create an attachment using POST request to https://graph.microsoft.com/beta/me/messages/{messageID}/attachments
{
"#odata.type": "#microsoft.graph.referenceAttachment",
"name": "AttachmentName",
"sourceUrl": "https://1drv.ms/u/s!ASDLKASDLASHDLASKDLJAXCXZ_DASD",
"providerType": "oneDriveConsumer",
"isFolder": false
}
After step 2 you will see created message in your mailbox Drafts folder. To send it use
https://graph.microsoft.com/beta/me/messages/{messageID}/send (=(
turns out it does not work too)
An alternative solution, not add attachment but add the file link in the mail body directly(Let the recipient to download it as needed).

ASANA API display all tasks in PHP script

I am using a simple php script which generates a task's name (it is quite long, that's why I build a generator for generating it).
Right now I need to copy generated name to ASANA, while I am creating a new task but I know that using ASANA API it can be integrated with my generator (when I click on GENERATE button the project's name is generated and a new ASANA task has been created with the generated name).
I created in my dashboard a Personal access token.
I downloaded this library https://github.com/ajimix/asana-api-php-class and filled up file examples/task-creation.php (all 3 fields with capital letters are correct filled):
<?php
require_once('../asana.php');
// See class comments and Asana API for full info
$asana = new Asana(array('personalAccessToken' => 'MY PERSONAL ACCESS TOKEN')); // Create a personal access token in Asana or use OAuth
$workspaceId = 'MY WORKSPACE ID'; // The workspace where we want to create our task, take a look at getWorkspaces() method.
// First we create the task
$asana->createTask(array(
'workspace' => $workspaceId, // Workspace ID
'name' => 'Hello World!', // Name of task
'assignee' => 'HERE MY EMAIL' // Assign task to...
));
// As Asana API documentation says, when a task is created, 201 response code is sent back so...
if ($asana->hasError()) {
echo 'Error while trying to connect to Asana, response code: ' . $asana->responseCode;
return;
}
$result = $asana->getData();
if (isset($result->id)) {
echo $result->id; // Here we have the id of the task that have been created
}
?>
After running the script from my localhost directory I get following error:
Error while trying to connect to Asana, response code: 0
Any help regarding this problem will be appreciated.
Please move your file to an online host instead of localhost. It should work.

Twilio Callback URL not Called - PHP & Wordpress

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

How to send message from WhatsApp in PHP with WhatsAPI Official?

I'm trying to use the WhatsApi Official library to send a message via WhatsApp from a php file. I've moved in my Apache web server the library, in a folder call test, like this:
The file whatsapp.php is this one:
<?php
require_once './src/whatsprot.class.php';
$username = "1XXXXXXXXX";
$password = "password";
$w = new WhatsProt($username, "0", "My Nickname", true); //Name your application by replacing “WhatsApp Messaging”
$w->connect();
$w->loginWithPassword($password);
$target = '1xxxxxxxxx'; //Target Phone,reciever phone
$message = 'This is my messagge';
$w->SendPresenceSubscription($target); //Let us first send presence to user
$w->sendMessage($target,$message ); // Send Message
echo "Message Sent Successfully";
?>
I'm facing some problem with the library new WhatsProt(), which blocks all the code (may be sockets ?).
So my question is, how can I fix this problem ? If no, are there any other solution to send message from a pho script ?
You can use below script to send 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 change 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'
);
It's working for me..
afaik you are probably better off currently writing an interface to a python project. E.g. have a microservice that does sending of messages for you in python, and you call them via some json request or similar
see this project, looks promising: https://github.com/tgalal/yowsup
it seems like the only viable option so far, as everything else was shut down or has a high probability to get your account banned
see discussion here:
https://stackoverflow.com/a/46635985/533426

How to send a picture message (MMS) with PHP

I am currently trying to send a MMS message to a cell phone (via a MMS gateway address) with a PHP script using the mail() function. I want to include a picture in the message.
I haven't found any information specific to sending MMS messages with PHP, and the information that I have found that dealt with sending email attachments was split, with some advising that I use an external package such as Mail_Mime. What would be the best way to send a picture message to a cell phone with a PHP script?
Any help would be greatly appreciated.
If your carrier supports receiving email messages and forward them to the handset as MMS, then simply sending an email with MIME image attachment is sufficient.
However, if you're dealing with an MMS centre (MMSC), then industry standard protocol for application<->MMSC is MM7
Ricky from Twilio here.
I know this is an old question, but for anyone landing here that's looking to accomplish this you can send MMS using our PHP helper library:
<?php
// this line loads the library
require('/path/to/twilio-php/Services/Twilio.php');
$account_sid = '[AccountSid]';
$auth_token = '[AuthToken]';
$client = new Services_Twilio($account_sid, $auth_token);
$client->account->messages->create(array(
'To' => "+16518675309",
'From' => "+14158141829",
'Body' => "Hey Jenny! Good luck on the bar exam!",
'MediaUrl' => "http://farm2.static.flickr.com/1075/1404618563_3ed9a44a3a.jpg",
));
You can find a few more examples in our API docs. Hope that helps!

Categories