How to manage sms and reply from webapp through twilio - php

I'm creating a messaging web application with laravel 5.1 and twilio and i'm using laravel-twilio package.
Settings and configuration already done in twilio dashboard
In Programmable SMS section i'hv created a messaging service with copilot which includes a single phone number ((xxx) xxx-xxx).
Given a Inbound request url.
My laravel application code
I have simple user to user messaging where both user are online where nothing to do with twilio. But suppose one of the user is offline and another one is online and the online user send a chat message to offline user then i have to notify the the offline user through an sms to this user's phone number via twilo.
public static function notifyOfflineUser($user,$reply_text){
$message = "Mysite: ".$user->user_from->firstname.", '".strip_tags($reply_text)."' Reply via mysite ".url('general-message')." or SMS";
if($user->profile->mobile) {
try {
\Twilio::message($user->profile->mobile, $message);
} catch (\Exception $e) {
}
}
}
It's sending a sms to the user from the twilio phone number.
Where the user can reply to this message form his/her phone to this number.
And if the reply succeded i have to add the message to the online user's message board.
//Inbound request url routed to this url with From number, body and other data.
public function smsReply(Request $request){
$user_id_to = Profile::where('mobile', $request->get('From'))->first(['user_id'])->user_id;
$body = $request->get('Body');
$user_id_from = Message::where('user_id_to', $user_id_to)->orderBy('id', 'desc')->first()->user_id_from;
//dd($request->all());
$message = new Message();
$message->user_id_from = $user_id_to;
$message->user_id_to = $user_id_from;
$message->message = $body;
$message->save();
return response()->json(true);
}
Now if multiple online user send chat message to this offline user, offline user will get miltiple sms from the same twilio number and those all sms are coming in a same stack, that mean it's not coming in different sms if he/she reply to the sms it will send to the same number, and all online user will get same message added in their message board.
Now here is my question..
Is there anything else need form twilio like multiple number, short codes, tollfree or i have to do the trick with my laravel code so i can send each sms to the offline user's phone as a different sms so i can distinguish them from inbound request url ? Feel free to ask me if more information needed.

Twilio developer evangelist here.
In order to be able to deal with multiple conversations like that, you're going to need multiple numbers. That way a number can represent the relationship between two users and you can tell what the user is responding to.
I recommend you check out this tutorial on building SMS conversations using multiple numbers with Laravel for more information.

Related

Notification system to alert the user

I would like to to have a notification system to alert the user via Google SMS system and register user with their phone number. I would like to send the alert when certain condition is reached in a PHP web application. Would it be possible? Could you please give some suggestion?
Yes it is possible. There are apis available for this functionality. You could implement this yourself and you can send text messages via email if you use an api to figure out the phone carrier of the user.

Back and forth SMS text messages replies in Twilio

I am working on a twilio sms API.
I have many users on my website and all of these users have a particular form on their profile page.
when a visitor goes to particular user profile and fill out the form.
then i send an automatic message from Twilio to the visitor, which basically says "Thanks for filling my form i will be in touch with you shortly"
Now here comes the challenging part where i am lost.
When the visitor tries to reply to that message. i want that message reply to be received to the particular user whose profile form was filled up. so every particular user should be able to receive their own text messages and start back and forth conversation on SMS.
It will be a dynamic reply from both users and visitor's end.
You can look into Twilio Conversations. You can find more information on conversations at the links below. Basically, you create a conversation and add participants to that conversation. Participants can be either SMS, MMS, WhatsApp, or Twilio Chat. Participants are assigned a Twilio Proxy Number with which they communicate through Twilio on.
Introducing Twilio Conversations: Now, every message becomes an invitation for a conversation
Using Twilio Conversations (Using Conversation You Tube Video - Signal 2019)
How to Use Twilio Conversations API (Twilio Chat to SMS tutorial)
Twilio Conversations Quickstart
The Conversations API Overview
Since users interact with you first through SMS and you want to continue the conversation with them through SMS.
This is the steps I would take
Receive and Reply to SMS Message. The example is in PHP but you can find an example in other languages.
You can reply with users SMS with Create a SMS Conversation using HTTP cookies with Webhooks

How To Get Chat Id of Users on telegram by there Mobile Number in PHP?

I have gone through many forums and API of telegram also.
But could not get the proper answer.
Well here is my main question.
I have list of users with their mobile numbers in my database and from all those, I want to send them messages on their telegram. So for that I will need their chat ID. How can I retrieve that by their MobileNumber? If I will have chat id, I am able to send message to them by using the following reference.
Telegram php example send message
I have visited the following link to get the chat id by manually from BOT by user itself. But I want automatic from script.
How to obtain Telegram chat_id for a specific user?
Thanks.
I'm not familiar with PHP. but there is a way to do this:
As you know you can send a contact in telegram bot using its phoneNumber and a firsName (Doesn't need to be the real first name of the contact who owns that number).
After sending the contact to a chatID(no matter what chatID you choose, can be your own personal chat ID), you can look for its UserID.
Now if the person exists in Telegram you will get a long number that
stands for his/her UserID or chatID but if not the long will be 0.
In C# I used this piece of code to see whether a phone number exists in telegram or not and it worked very well.
string s = "+44...."; //the phone number
var req2 = await bot.MakeRequestAsync(new SendContact(update.Message.Chat.Id, s, "Name"));
if(req2.Contact.UserId == 0)
{
Console.WriteLine("The contact does not exist in Telegram");
}else
{
Console.WriteLine("The contact exists in telegram with UserID:{0}",req2.Contact.UserId.ToString());
}
I believe this can be done in PHP too.
By Telegram bot you can send messages only to users that started chat with your bot. you can obtain their chat_id (not phone number) from received query.
So you can not send message to another people even if you have their phone numbers.
You can't use bots to find a user on Telegram using their phone number.

PHP API track Twilio Responses

i'm working on an web app that has a message thread within and each time a new message is sent to a person a SMS is sent to that person, so what we want to do is to include the person's response to that SMS, lets say A sends a message to B, B receives both an email and a SMS, if B responses to A via the phone we need to be able to add that response to the thread. Is there a way to add additional information when you send a message using the API?
The sending SMS code looks like this:
$client = new Services_Twilio($accountSID,$authToken);
$sms = $client->account->messages->sendMessage("TwilioNumber",$toNumber,$message);
So, is there a way in which i could add some type of information to track this SMS thread, so when i get the response to the Request URL i could actually know that this message is being sent from B as a response from the message sent from A.
Thanks in advance.
Twilio evangelist here.
Unfortunately SMS does not have the idea of "meta-data" so there is nothing you can include that would tell you a message is in "response" to another message, but there are some solutions here.
You could create a convention of commands that are included in the actual body of the text message that tell you the message is ins response to a specific thread. But thats not super user friendly.
You're best bet is probably going to using multiple phone numbers and phone number tracking.
For example, when A sends the message to B you'll need to store both of their phone numbers along with the Twilio phone number that is receiving and relaying those messages. This allows you to map the two users together in a "thread" even though they are not directly texting each other.
But what happens when A wants to have seperate thread with both B and C? With a single Twilio phone number how do you know what thread to route their message to? The simplest way to allow this is to have multiple phone numbers. To send a message to B, A sends to 555-555-5555, to send a message to C, A sends to 555-555-6666. In this scenario you might think about buying a pool of numbers that you can draw from and recycle so you don't have to continuously buy new numbers.
Hope that helps.

How can I Send message to WhatsApp Group and Sync contacts using WhatsAPI?

OK, I working on an application trying to send invitations to user contacts over WhatsApp so scenario will be as follow.
User will open invite screen.
Screen open filled with his contacts
Each contact marked if he is whatsapp user or not (Is it Applicable)
when user select X from his contacts to send them invitations
Client will send contacts details to the server
Server will send to those contacts a whatsapp message (Is it applicable)
Note: regarding password what I understand is that password different for each user so how can I get the whatsapp password for each user using my application.
Thanks
First of all, getting WhatsApp password of each of your users is not feasible I think.
Because while registering WhatsApp with YOUR app, user's official WhatsApp will fail to authenticate.
If you want to send just an invite or message you can use Intent. http://www.whatsapp.com/faq/en/android/28000012
Now back to WhatsAPI,
You can send messages to group just like an ordinary message
WhatsProt::sendMessage($groupJID,$message);
To get the list of groups, you can use the
WhatsProt::sendGetGroups();
You have to bind this to an event manager like this:
WhatsProt::eventManager()->bind("onGetGroups", "onGetGroups");
WhatsProt::sendGetGroups();
//event manager call-back function.
function onGetGroups($phone,$groups)
{
var_dump($groups);
}

Categories