PHP code example of samuelmwangiw / africastalking-laravel
1. Go to this page and download the library: Download samuelmwangiw/africastalking-laravel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
samuelmwangiw / africastalking-laravel example snippets
use SamuelMwangiW\Africastalking\Facades\Africastalking;
/** @var \SamuelMwangiW\Africastalking\ValueObjects\Balance $account */
$account = Africastalking::application()->balance();
// Or using the global helper function
$account = africastalking()->application()->balance();
use SamuelMwangiW\Africastalking\Facades\Africastalking;
$response = Africastalking::sms('Hello mom!')
->to('+254712345678')
->send();
// Or using the global helper function
$response = africastalking()->sms("Hello Mom")
->to('+254712345678')
->send();
use SamuelMwangiW\Africastalking\Facades\Africastalking;
$response = Africastalking::sms('It is quality rather than quantity that matters. - Lucius Annaeus Seneca')
->message("We made it!") //overwrites any text previously set
->text("Look, am on the internet") //alias to message()
->as('MyBIZ') // optional: When the senderId is different from `config('africastalking.sms.from')`
->to(['+254712345678','+256706123567'])
->bulk() // optional: Messages are bulk by default
->enqueue() //used for Bulk SMS clients that would like to deliver as many messages to the API before waiting for an acknowledgement from the Telcos
->send()
// Or using the global helper function
$response = africastalking()->sms()
->message("Hello Mom") //overwrites any text previously set
->to('+254712345678')
->send();
use SamuelMwangiW\Africastalking\Facades\Africastalking;
$response = Africastalking::sms('It is quality rather than quantity that matters. - Lucius Annaeus Seneca')
->as('90012') // optional: When the senderId is different from `config('africastalking.sms.from')`
->to(['+254712345678','+256706123567'])
->premium() // Required to designate messages as bulk
->bulkMode(false) // True to send premium messages in bulkMode and false to send as premium
->retry(2) //specifies the number of hours your subscription message should be retried in case it’s not delivered to the subscriber.
->keyword('keyword') // optional:
->linkId('message-link-id') // optional:
->send()
use SamuelMwangiW\Africastalking\Facades\Africastalking;
$response = Africastalking::airtime()
->to('+254712345678','KES',100)
->send();
// Or using the global helper function
$response = africastalking()->airtime()
->to('+256706345678','UGX',100)
->send();
use SamuelMwangiW\Africastalking\Facades\Africastalking;
use SamuelMwangiW\Africastalking\ValueObjects\AirtimeTransaction;
use SamuelMwangiW\Africastalking\ValueObjects\PhoneNumber;
use SamuelMwangiW\Africastalking\Enum\Currency;
$transaction = new AirtimeTransaction(PhoneNumber::make('+256769000000'),Currency::UGANDA,1000)
$response = Africastalking::airtime()
->to($transaction)
->send();
use App\Models\Clients;
$airtime = africastalking()->airtime();
Clients::query()->chunk(1000, function ($clients) use($airtime) {
foreach ($clients as $client) {
$airtime->add($client->phone,'TZS',3000);
}
});
$results = $airtime->send();
namespace App\Http\Controllers;
use App\Models\Client;
use Illuminate\Support\Str;
use SamuelMwangiW\Africastalking\Http\Requests\UssdEventRequest;
use SamuelMwangiW\Africastalking\Http\Requests\UssdSessionRequest;
class UssdController extends Controller
{
public function session(UssdSessionRequest $request)
{
if (!$request->userInput()) {
return africastalking()->ussd('Kindly enter your account number');
}
$account = Client::query()
->whereAccountNo(
Str::of($request->userInput())->afterLast('*')->toString()
)->first();
if ($account?->exists()) {
return africastalking()
->ussd("Your account Balance is KES {$account->balance}")->end();
}
return africastalking()
->ussd("The account you entered does not exists.\n\nKindly enter a valid account number");
}
public function event(UssdEventRequest $request)
{
//Handle the USSD event here
return response('OK');
}
}
africastalking()
->stash()
->topup('TZS',100000);
//or fluently
africastalking()
->stash()
->amount(10000)
->currency('USD')
->product('My Product') //To override the product name in config
->topup(); //topup() has an alias named send() if you fancy
return africastalking()->voice()
->say(message: '', playBeep: "false", voice: "en-US-Standard-C")
->play(url: 'https://example.com/audio.wav')
->getDigits(
say: 'Please enter your account number followed by the hash sign',
finishOnKey: '#',
timeout: '30',
callbackUrl: 'http://mycallbackURL.com',
)->dial(
phoneNumbers: ['+254711XXXYYY', '+25631XYYZZZZ', '[email protected]'],
record: true,
ringBackTone: 'http://mymediafile.com/playme.mp3',
maxDuration: 5,
sequential: false,
callerId: '+254711XXXYYY',
)->record(
say: 'Please enter your account number followed by the hash sign',
finishOnKey: '#',
timeout: '30',
maxLength: 10,
playBeep: true,
trimSilence: true
)->redirect(
url: 'http://www.myotherhandler.com/process.php',
)->reject();
namespace App\Http\Controllers\CallCenter;
use SamuelMwangiW\Africastalking\Http\Requests\VoiceCallRequest;
class HandleCallsController
{
public function __invoke(VoiceCallRequest $request)
{
if ($request->input('isActive')){
return africastalking()->voice()
->say('Welcome to Unicorn bank.')
->getDigits(
say:'Please enter your account Number followed by the # key',
finishOnKey: '#'
)
}
return response('OK');
}
}
namespace App\Http\Controllers\CallCenter;
use SamuelMwangiW\Africastalking\Http\Requests\VoiceCallRequest;
class RecordCallsController
{
public function __invoke(VoiceCallRequest $request)
{
if ($request->input('isActive')){
return africastalking()->voice()
->say('Our working hours are 9AM - 7PM, Monday to to Friday')
->record(
say: 'Please leave a message after the tone.',
finishOnKey: '#',
playBeep: true,
maxLength: 10,
trimSilence: true,
);
}
return response('OK');
}
}
namespace App\Http\Controllers\CallCenter;
use SamuelMwangiW\Africastalking\Http\Requests\VoiceCallRequest;
class RejectCallsController
{
public function __invoke(VoiceCallRequest $request)
{
if ($request->input('isActive')){
return africastalking()->voice()
->say('Our working hours are 9AM - 7PM, Monday to to Friday')
->reject();
}
return response('OK');
}
}
namespace App\Http\Controllers\CallCenter;
use App\Models\Doctor;
use SamuelMwangiW\Africastalking\Http\Requests\VoiceCallRequest;
class ForwardCallsController
{
public function __invoke(VoiceCallRequest $request)
{
if ($request->input('isActive')){
$doctor = Doctor::query()->onDuty()->first();
return africastalking()->voice()
->dial(
phoneNumbers: [$doctor->number],
record: true,
ringBackTone: "https://example.com/marketing-tone.wave"
);
}
return response('OK');
}
}
namespace App\Http\Controllers\CallCenter;
use SamuelMwangiW\Africastalking\Http\Requests\VoiceCallRequest;
use SamuelMwangiW\Africastalking\ValueObjects\Voice\SynthesisedSpeech;
class HandleCallsController
{
public function __invoke(VoiceCallRequest $request)
{
if ($request->input('isActive')){
return africastalking()->voice()
->say(
fn(SynthesisedSpeech $speech) => $speech
->say('Welcome to Wasafi. Your airtime balance is')
->sayAsCurrency('$1000','en-US')
->say(' and expires on ')
->sayAsDate('20420420')
->say(' at ')
->sayAsTime('04:42PM')
->pause('69ms')
->say('For assistance Call our customer service team on ')
->sayAsTelephone('0800694269')
->bleep('Thank you for choosing us 😜')
)
}
return response('OK');
}
}
// Most basic call
africastalking()->voice()
->call('+254720123123')
->done();
// Call a list of users
africastalking()->voice()
->call(['+254720123123','+254731234567'])
->done();
// override the callerId
africastalking()->voice()
->call('+254720123123')
->as('+254711082999')
->done();
// Set the call Request Id
africastalking()->voice()
->call('+254720123123')
->requestId('id_12345')
->done();
// You can also use send if like that better
africastalking()->voice()
->call('+254720123123')
->send();
namespace App\Http\Controllers\Messaging;
use App\Models\Message;
use SamuelMwangiW\Africastalking\Http\Requests\MessageDeliveryRequest;
class MessageDeliveredController{
public function __invoke(MessageDeliveryRequest $request)
{
$message = Message::query()
->where(['transaction_id'=>$request->id()])
->firstOrFail();
$message->update([
'delivered_at'=>now(),
'status'=>$request->status(),
]);
return response('OK');
}
}
namespace App\Notifications;
use Illuminate\Notifications\Notification;
use SamuelMwangiW\Africastalking\Facades\Africastalking;
use SamuelMwangiW\Africastalking\Notifications\AfricastalkingChannel;
class WelcomeNotification extends Notification
{
public function via($notifiable)
{
return [AfricastalkingChannel::class];
}
public function toAfricastalking($notifiable)
{
return "Hi {$notifiable->name}. Your account at Unicorn Bank has been created. Hope you enjoy the service";
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use SamuelMwangiW\Africastalking\Contracts\ReceivesSmsMessages;
class User extends Model implements ReceivesSmsMessages
{
protected $fillable = ['email','name','phone'];
public function routeNotificationForAfricastalking(Notification $notification): string
{
return $this->phone;
}
}