Skip to content

Commit 233d46c

Browse files
basic fns done
1 parent 61ed9f4 commit 233d46c

File tree

4 files changed

+112
-6
lines changed

4 files changed

+112
-6
lines changed

app/Http/Controllers/FunctionsController.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,71 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Services\FunctionToolService;
56
use Illuminate\Http\Request;
67

78
class FunctionsController extends Controller
89
{
10+
11+
private $functionToolService;
12+
13+
public function __construct()
14+
{
15+
$this->functionToolService = new FunctionToolService();
16+
}
917
public function create_basic(Request $request)
1018
{
11-
return 'functions_basic';
19+
$payload = $request->input('message');
20+
$type = $payload['type'];
21+
22+
switch ($type) {
23+
case 'function-call':
24+
$functionCall = $payload['functionCall'];
25+
26+
if (!$functionCall) {
27+
throw new \Exception("Invalid Request.");
28+
}
29+
30+
$name = $functionCall['name'];
31+
$parameters = $functionCall['parameters'];
32+
33+
if ($name == "getRandomName") {
34+
return $this->functionToolService->getRandomName($parameters);
35+
} else if ($name == "getCharacterInspiration") {
36+
return $this->functionToolService->getCharacterInspiration($parameters);
37+
}
38+
39+
return;
40+
default:
41+
// ignore other types of messages
42+
return;
43+
}
1244
}
1345

1446
public function create_rag(Request $request)
1547
{
16-
return 'functions_rag';
48+
$payload = $request->input('message');
49+
$type = $payload['type'];
50+
51+
switch ($type) {
52+
case 'function-call':
53+
$functionCall = $payload['functionCall'];
54+
55+
if (!$functionCall) {
56+
throw new \Exception("Invalid Request.");
57+
}
58+
59+
$name = $functionCall['name'];
60+
$parameters = $functionCall['parameters'];
61+
62+
if ($name == "getCharacterInspiration") {
63+
return $this->functionToolService->getCharacterInspiration($parameters);
64+
}
65+
66+
return;
67+
default:
68+
// ignore other types of messages
69+
return;
70+
}
1771
}
1872
}

app/Http/Controllers/OutboundController.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,57 @@ class OutboundController extends Controller
88
{
99
public function create(Request $request)
1010
{
11-
return 'outbound';
11+
// Extract phoneNumberId, assistantId, and customerNumber from the request body
12+
$data = json_decode($request->getContent(), true);
13+
$phoneNumberId = $data['phoneNumberId'];
14+
$assistantId = $data['assistantId'];
15+
$customerNumber = $data['customerNumber'];
16+
17+
try {
18+
/**!SECTION
19+
* Handle Outbound Call logic here.
20+
* This can initiate an outbound call to a customer's phonenumber using Vapi.
21+
*/
22+
23+
$curl = curl_init();
24+
25+
curl_setopt_array($curl, array(
26+
CURLOPT_URL => env('VAPI_BASE_URL') . '/call/phone',
27+
CURLOPT_RETURNTRANSFER => true,
28+
CURLOPT_ENCODING => '',
29+
CURLOPT_MAXREDIRS => 10,
30+
CURLOPT_TIMEOUT => 0,
31+
CURLOPT_FOLLOWLOCATION => true,
32+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
33+
CURLOPT_CUSTOMREQUEST => 'POST',
34+
CURLOPT_POSTFIELDS => json_encode(array(
35+
'phoneNumberId' => $phoneNumberId,
36+
'assistantId' => $assistantId,
37+
'customer' => array(
38+
'number' => $customerNumber
39+
)
40+
)),
41+
CURLOPT_HTTPHEADER => array(
42+
'Content-Type: application/json',
43+
'Authorization: Bearer ' . env('VAPI_API_KEY')
44+
),
45+
));
46+
47+
$response = curl_exec($curl);
48+
49+
if (curl_errno($curl)) {
50+
throw new \Exception('Curl error: ' . curl_error($curl));
51+
}
52+
53+
curl_close($curl);
54+
55+
$data = json_decode($response, true);
56+
return response()->json($data, 200);
57+
} catch (\Exception $error) {
58+
return response()->json([
59+
'message' => 'Failed to place outbound call',
60+
'error' => $error->getMessage()
61+
], 500);
62+
}
1263
}
1364
}

app/Services/WebhookService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public function handleWebhook(Request $request)
3535
case 'hang':
3636
return $this->handleHang($payload);
3737
default:
38-
throw new \Exception('Unhandled message type');
38+
// ignore other types of messages
39+
3940
}
4041
}
4142

.env.example renamed to example.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ VITE_PUSHER_PORT="${PUSHER_PORT}"
5858
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
5959
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
6060

61-
61+
WEATHER_BASE_URL=https://api.openweathermap.org/data/2.5
6262
WEATHER_API_KEY=
6363
OPENAI_API_KEY=
64-
64+
VAPI_BASE_URL=https://api.vapi.ai
6565
VAPI_API_KEY=

0 commit comments

Comments
 (0)