Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Msg91 Laravel Package

robincsamuel/laravel-msg91

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require robincsamuel/laravel-msg91
    

    Add to config/app.php:

    'providers' => [
        // ...
        RobinCSamuel\LaravelMsg91\LaravelMsg91ServiceProvider::class,
    ],
    'aliases' => [
        // ...
        'LaravelMsg91' => RobinCSamuel\LaravelMsg91\Facades\LaravelMsg91::class,
    ]
    
  2. Environment Configuration: Add these to .env:

    MSG91_KEY=your_auth_key_from_msg91
    MSG91_SENDER_ID=YourSenderID
    MSG91_ROUTE=4  # (e.g., 4 for transactional, 5 for promotional)
    MSG91_COUNTRY=91  # Default country code (e.g., 91 for India)
    
  3. First Use Case: Send a text SMS:

    use LaravelMsg91\Facades\LaravelMsg91;
    
    LaravelMsg91::send('+919876543210', 'Hello from Laravel!');
    

Implementation Patterns

Core Workflows

  1. Sending SMS:

    • Basic Text SMS:
      LaravelMsg91::send($mobileNumber, $message);
      
    • OTP SMS:
      $otp = LaravelMsg91::sendOTP($mobileNumber, $templateId);
      
  2. Batch Processing: Use Laravel’s queues to avoid timeouts for bulk SMS:

    LaravelMsg91::dispatch($mobileNumber, $message)->delay(now()->addMinutes(5));
    
  3. Dynamic Sender ID: Override sender ID per request:

    LaravelMsg91::setSenderId('CUSTOMID')->send($mobileNumber, $message);
    
  4. Error Handling: Wrap calls in try-catch:

    try {
        LaravelMsg91::send($mobileNumber, $message);
    } catch (\Exception $e) {
        Log::error('MSG91 Error: ' . $e->getMessage());
    }
    
  5. Integration with Events: Trigger SMS on model events (e.g., created):

    class User extends Model {
        protected static function booted() {
            static::created(function ($user) {
                LaravelMsg91::send($user->phone, 'Welcome!');
            });
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Deprecated Package:

    • Last release in 2021—verify compatibility with Laravel 10+ (may require manual fixes).
    • Check for undocumented breaking changes in newer MSG91 API versions.
  2. Environment Variables:

    • MSG91_KEY is mandatory; missing it throws silent failures.
    • MSG91_ROUTE defaults to 4 (transactional). Use 5 for promotional SMS (rate limits apply).
  3. Mobile Number Format:

    • Always include country code (e.g., +919876543210), or SMS may fail.
    • Test with MSG91_COUNTRY=91 for India; adjust for other regions.
  4. Rate Limits:

    • MSG91 enforces 1 SMS/second for free tier. Queue requests to avoid throttling:
      LaravelMsg91::dispatch($mobileNumber, $message)->delay(now()->addSecond());
      
  5. OTP Template IDs:

    • Requires pre-configured templates in MSG91 dashboard. Hardcoding IDs is risky—use .env or database.

Debugging

  1. Enable Logging: Add to .env:

    MSG91_DEBUG=true
    

    Logs API responses to storage/logs/laravel.log.

  2. API Response Parsing: MSG91 returns JSON like:

    {"type":"error", "message":"Invalid Auth Key"}
    

    Use LaravelMsg91::getLastResponse() to inspect raw output.

  3. Common Errors:

    • Invalid Auth Key: Verify MSG91_KEY in .env.
    • Invalid Route: Ensure MSG91_ROUTE is 4 or 5.
    • Invalid Mobile Number: Validate format (e.g., +919876543210).

Extension Points

  1. Custom Base URI: Override MSG91 API endpoint (useful for resellers):

    MSG91_BASE_URI=https://api.msg91.com/api/sendotp.php
    
  2. Mocking for Tests: Extend the facade for testing:

    // In tests/CreatesApplication.php
    $this->app->singleton(\RobinCSamuel\LaravelMsg91\Msg91::class, function () {
        return Mockery::mock(\RobinCSamuel\LaravelMsg91\Msg91::class);
    });
    
  3. Adding New Features:

    • The package lacks scheduling or template management. Extend the Msg91 class:
      // app/Extensions/Msg91Extension.php
      namespace App\Extensions;
      
      use RobinCSamuel\LaravelMsg91\Msg91;
      
      class Msg91Extension extends Msg91 {
          public function sendScheduled($mobile, $message, \Carbon\Carbon $schedule) {
              // Implement logic to queue SMS
          }
      }
      
    • Bind the extension in AppServiceProvider:
      $this->app->bind(Msg91::class, function () {
          return new Msg91Extension(config('msg91'));
      });
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope