I am triggering a call from postman to mobile phone using twilio. I am able to call successfully. It rings on my phone. When I click accept, a message is prompted on the call. When I press 1, it says something error and does not redirect or open a website link.
How should I open a browser and display the url as passed?
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use TwilioRestClient;
class TwilioController extends Controller
{
public function makeCall(Request $request)
{
$sid = env('TWILIO_ACCOUNT_SID');
$token = env('TWILIO_AUTH_TOKEN');
$twilio = new Client($sid, $token);
$call = $twilio->calls->create(
$request->input('to'), // User's phone number in E.164 format
env('TWILIO_PHONE_NUMBER'), // Your Twilio number
[
'url' => 'http://testing.domain.tech/twilio/voice-prompt'
]
);
return response()->json(['status' => 'Call initiated', 'sid' => $call->sid]);
}
public function voicePrompt()
{
$response = new TwilioTwiMLVoiceResponse();
$gather = $response->gather([
'numDigits' => 1,
'action' => url('/twilio/handle-key-press'),
'method' => 'POST'
]);
$gather->say('Press 1 to receive a link via SMS.');
// If no input, Twilio will call this after gather timeout
$response->say('No input received. Goodbye!');
return response($response)->header('Content-Type', 'text/xml');
}
public function handleKeyPress(Request $request)
{
$digits = $request->input('Digits');
$from = $request->input('From');
$response = new TwilioTwiMLVoiceResponse();
if ($digits == '1') {
try {
$sid = env('TWILIO_ACCOUNT_SID');
$token = env('TWILIO_AUTH_TOKEN');
$twilio = new Client($sid, $token);
$twilio->messages->create(
$from,
[
'from' => env('TWILIO_PHONE_NUMBER'),
'body' => 'Here is your link: ' . url('/')
]
);
$response->say('A link has been sent to your phone via SMS. Thank you!');
} catch (Exception $e) {
Log::error('Twilio SMS error: ' . $e->getMessage());
$response->say('Sorry, there was an error. Please try again later.');
}
} else {
$response->say('Invalid option. Goodbye!');
}
return response($response)->header('Content-Type', 'text/xml');
}
}
// web.php
Route::post('/twilio/voice-prompt', [TwilioController::class, 'voicePrompt'])->name('twilio.voicePrompt');
Route::post('/twilio/handle-key-press', [TwilioController::class, 'handleKeyPress'])->name('twilio.handleKeyPress');
// api.php
Route::post('/twilio/make-call', [TwilioController::class, 'makeCall']);