gr8shivam / laravel-sms-api

Laravel package to provide SMS API integration.
MIT License
99 stars 30 forks source link

Improvement: support for multiple phone numbers in notification channel ... #25

Closed scramatte closed 2 years ago

scramatte commented 3 years ago

Hello,

Can you add support for multiple phone numbers in notification channel and send message ? I've made a dirty patch into SmsApiChannel.php but requires loop to send different messages.

In the api I use, it possible to send various SMS in one call https://panel.smspubli.com/api/3.0/docs/sms/send

For sur it's possible to improve it on your side.

SmsApiChannel.php

public function send($notifiable, Notification $notification)
    {
        if (! $mobile = $notifiable->routeNotificationFor('sms_api')) {
            return;
        }

        $message = $notification->toSmsApi($notifiable);

        if (is_string($message)) {
            $message = new SmsApiMessage($message);
        }

        $mobiles = is_string($mobile)?[$mobile]:$mobile;

        foreach ($mobiles as $mobile) {
                $this->client->sendMessage($mobile,$message->content,$message->params,$message->headers);
        }

    }
gr8shivam commented 3 years ago

Hi @scramatte , I couldn't exactly get your point.

You can send a SMS to multiple users by following steps:

A) Fetch users mobile in an Array like:

$mobiles = App\User::where('id','>=',100)
            ->get()
            ->pluck('mobile')
            ->toArray();

B) Send SMS using: smsapi()->sendMessage($mobiles, $message);

gr8shivam commented 3 years ago

I had a look at your Gateway API docs. You can use the wrapper functionality to send the SMS.

But I don't think your gateway accepts multiple numbers in one request. They want a different sub-request for each destination number like:

        {
            "from":"GOOD PIZZA",
            "to":"34666666111",
            "text":"Hi John, today 2x1 in pizzas, watch the game like a boss with our new pepperoni pizza!",
            "send_at":"2018-02-18 17:30:00"
        },
        {
            "from":"GOOD PIZZA",
            "to":"34666666112",
            "text":"Hi Maria, , today 2x1 in pizzas, watch the game like a boss with our new pepperoni pizza!",
            "custom":"MyMsgID-12345",
            "send_at":"2018-02-18 17:30:00"
        }
scramatte commented 3 years ago

Hi,

Sorry for very very late reply ...

I'm back on my Laravel project and what I need is just to be able to pass and array or a string into routeNotificationForSmsApi() because my customers has various mobiles, emails and my app use many many Notifications ...

The way you comment of course works but it's not usable in the context of my app. Can you make the patch ?

gajosadrian commented 2 years ago

Just construct the method in your Model like so:

public function routeNotificationForMail($notification)
{
    return [$this->phone, $this->phone2];
}
scramatte commented 2 years ago

Hello,

Yes , I've solved it in similar way.