text4u
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TwilioController.php 1.4KB

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