mercredi 1 juin 2016

How to manage sms and reply from webapp through twilio

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

  1. In Programmable SMS section i'hv created a messaging service with copilot which includes a single phone number ((xxx) xxx-xxx).

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire