Selaa lähdekoodia

add Twilio integration for sending WhatsApp messages and handling callbacks

master
Amirul Anwar 4 päivää sitten
vanhempi
commit
eb360958e7
6 muutettua tiedostoa jossa 119 lisäystä ja 2 poistoa
  1. 4
    0
      .env.example
  2. 48
    0
      app/Http/Controllers/TwilioController.php
  3. 2
    1
      composer.json
  4. 54
    1
      composer.lock
  5. 7
    0
      config/twilio.php
  6. 4
    0
      routes/api.php

+ 4
- 0
.env.example Näytä tiedosto

@@ -67,3 +67,7 @@ VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
67 67
 VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
68 68
 
69 69
 DEBUGBAR_ENABLED=false
70
+
71
+TWILIO_SID=your_twilio_account_sid
72
+TWILIO_AUTH_TOKEN=your_twilio_auth_token
73
+TWILIO_PHONE_NUMBER=your_twilio_phone_number

+ 48
- 0
app/Http/Controllers/TwilioController.php Näytä tiedosto

@@ -0,0 +1,48 @@
1
+<?php
2
+
3
+namespace App\Http\Controllers;
4
+
5
+use Illuminate\Http\Request;
6
+use Illuminate\Support\Facades\Log;
7
+use Twilio\Rest\Client;
8
+
9
+class TwilioController extends Controller
10
+{
11
+    public function sendWhatsAppMessage(Request $request)
12
+    {
13
+        // $request->validate([
14
+        //     'to' => 'required',
15
+        //     'message' => 'required',
16
+        // ]);
17
+
18
+        $sid = config('twilio.sid');
19
+        $token = config('twilio.auth_token');
20
+        $from = config('twilio.from');
21
+        $twilio = new Client($sid, $token);
22
+
23
+        try {
24
+            $message = $twilio->messages
25
+            ->create("whatsapp:+60126194469", // to
26
+                array(
27
+                "from" => $from,
28
+                "contentSid" => "HXb5b62575e6e4ff6129ad7c8efe1f983e",
29
+                "contentVariables" => '{"1":"12/1","2":"3pm"}',
30
+                "body" => "Your Message"
31
+                )
32
+            );
33
+
34
+            return response()->json(['status' => 'Message sent successfully', 'message_sid' => $message->sid]);
35
+        } catch (\Exception $e) {
36
+            return response()->json(['status' => 'Failed to send message', 'error' => $e->getMessage()], 500);
37
+        }
38
+    }
39
+
40
+    public function handleWhatsAppCallback(Request $request)
41
+    {
42
+        $data = $request->all();
43
+        // Log the callback data
44
+        Log::info('WhatsApp Callback Data:', $data);
45
+
46
+        return response()->json(['status' => 'Callback received', 'data' => $data]);
47
+    }
48
+}

+ 2
- 1
composer.json Näytä tiedosto

@@ -14,7 +14,8 @@
14 14
         "laravel/framework": "^10.8",
15 15
         "laravel/sanctum": "^3.2",
16 16
         "laravel/tinker": "^2.8",
17
-        "statamic/cms": "^4.0"
17
+        "statamic/cms": "^4.0",
18
+        "twilio/sdk": "*"
18 19
     },
19 20
     "require-dev": {
20 21
         "barryvdh/laravel-debugbar": "^3.8.1",

+ 54
- 1
composer.lock Näytä tiedosto

@@ -4,7 +4,7 @@
4 4
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
5 5
         "This file is @generated automatically"
6 6
     ],
7
-    "content-hash": "88337a197bd9098ea3816ea068857e9c",
7
+    "content-hash": "a67397f21c534eeb9c04ce82e2587d71",
8 8
     "packages": [
9 9
         {
10 10
             "name": "ajthinking/archetype",
@@ -7138,6 +7138,59 @@
7138 7138
             },
7139 7139
             "time": "2023-12-08T13:03:43+00:00"
7140 7140
         },
7141
+        {
7142
+            "name": "twilio/sdk",
7143
+            "version": "8.3.9",
7144
+            "source": {
7145
+                "type": "git",
7146
+                "url": "https://github.com/twilio/twilio-php.git",
7147
+                "reference": "b8a79247ad00daed6f823328a052293d498fac36"
7148
+            },
7149
+            "dist": {
7150
+                "type": "zip",
7151
+                "url": "https://api.github.com/repos/twilio/twilio-php/zipball/b8a79247ad00daed6f823328a052293d498fac36",
7152
+                "reference": "b8a79247ad00daed6f823328a052293d498fac36",
7153
+                "shasum": ""
7154
+            },
7155
+            "require": {
7156
+                "php": ">=7.1.0"
7157
+            },
7158
+            "require-dev": {
7159
+                "guzzlehttp/guzzle": "^6.3 || ^7.0",
7160
+                "phpunit/phpunit": ">=7.0 < 10"
7161
+            },
7162
+            "suggest": {
7163
+                "guzzlehttp/guzzle": "An HTTP client to execute the API requests"
7164
+            },
7165
+            "type": "library",
7166
+            "autoload": {
7167
+                "psr-4": {
7168
+                    "Twilio\\": "src/Twilio/"
7169
+                }
7170
+            },
7171
+            "notification-url": "https://packagist.org/downloads/",
7172
+            "license": [
7173
+                "MIT"
7174
+            ],
7175
+            "authors": [
7176
+                {
7177
+                    "name": "Twilio API Team",
7178
+                    "email": "api@twilio.com"
7179
+                }
7180
+            ],
7181
+            "description": "A PHP wrapper for Twilio's API",
7182
+            "homepage": "https://github.com/twilio/twilio-php",
7183
+            "keywords": [
7184
+                "api",
7185
+                "sms",
7186
+                "twilio"
7187
+            ],
7188
+            "support": {
7189
+                "issues": "https://github.com/twilio/twilio-php/issues",
7190
+                "source": "https://github.com/twilio/twilio-php/tree/8.3.9"
7191
+            },
7192
+            "time": "2024-12-05T10:18:30+00:00"
7193
+        },
7141 7194
         {
7142 7195
             "name": "ueberdosis/tiptap-php",
7143 7196
             "version": "1.4.0",

+ 7
- 0
config/twilio.php Näytä tiedosto

@@ -0,0 +1,7 @@
1
+<?php
2
+
3
+return [
4
+    'sid' => env('TWILIO_SID'),
5
+    'auth_token' => env('TWILIO_AUTH_TOKEN'),
6
+    'from' => env('TWILIO_WHATSAPP_FROM', 'whatsapp:+14155238886'),
7
+];

+ 4
- 0
routes/api.php Näytä tiedosto

@@ -3,6 +3,7 @@
3 3
 use App\Http\Controllers\EnquiryController;
4 4
 use Illuminate\Http\Request;
5 5
 use Illuminate\Support\Facades\Route;
6
+use App\Http\Controllers\TwilioController;
6 7
 
7 8
 /*
8 9
 |--------------------------------------------------------------------------
@@ -20,3 +21,6 @@ use Illuminate\Support\Facades\Route;
20 21
 // });
21 22
 
22 23
 Route::resource('/enquiry', EnquiryController::class);
24
+
25
+Route::post('/send-whatsapp', [TwilioController::class, 'sendWhatsAppMessage'])->withoutMiddleware([\App\Http\Middleware\VerifyCsrfToken::class]);
26
+Route::post('/callback-whatsapp', [TwilioController::class, 'handleWhatsAppCallback'])->withoutMiddleware([\App\Http\Middleware\VerifyCsrfToken::class]);

Loading…
Peruuta
Tallenna