OutboundCallWebHookController.php
5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace NexmoBundle\Controller;
use AppBundle\Controller\SpokeControllerTrait;
use AppBundle\Entity\User;
use AppBundle\Entity\VoiceCall;
use AppBundle\Event\VoiceCallEvent;
use AppBundle\Event\VoiceCallEvents;
use CobrandBundle\CobrandInstance;
use Doctrine\ORM\ORMException;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Post;
use NexmoBundle\VoiceCall\VoiceCallOrigins;
use NexmoBundle\VoiceCall\VoiceCallStatus;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class OutboundCallWebHookController extends NexmoWebHookController
{
/**
* @Post("/event-url", name="event_webhook")
*
* @param Request $request
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function postEventAction(Request $request)
{
$uuid = $request->get("uuid");
$voiceCall = $this->getDoctrine()->getRepository("AppBundle:VoiceCall")->findOneBy([
"uuid" => $uuid
]);
if (!$voiceCall instanceof VoiceCall) {
return $this->jsonResponse(["success" => false], Response::HTTP_OK);
}
$delegates = [];
$requestMap = [
"status" => function ($val) use ($voiceCall, &$delegates) {
$voiceCall->setStatus($val);
// add delegate for changing statuses
$delegates[] = function () use ($voiceCall) {
$event = new VoiceCallEvent($voiceCall);
switch($voiceCall->getStatus()) {
case VoiceCallStatus::STARTED:
case VoiceCallStatus::RINGING:
$this->get("event_dispatcher")->dispatch(VoiceCallEvents::STARTED_VOICE_CALL, $event);
break;
case VoiceCallStatus::ANSWERED:
$this->get("event_dispatcher")->dispatch(VoiceCallEvents::ANSWERED_VOICE_CALL, $event);
break;
case VoiceCallStatus::BUSY:
case VoiceCallStatus::CANCELLED:
case VoiceCallStatus::REJECTED:
case VoiceCallStatus::FAILED:
$this->get("event_dispatcher")->dispatch(VoiceCallEvents::FAILED_VOICE_CALL, $event);
break;
case VoiceCallStatus::COMPLETED:
$this->get("event_dispatcher")->dispatch(VoiceCallEvents::COMPLETED_VOICE_CALL, $event);
break;
}
}; // end delegate func
},
"timestamp" => function ($val) use ($voiceCall) {
$dt = new \DateTime($val);
$voiceCall->setUpdatedAt($dt);
},
"rate" => function ($val) use ($voiceCall) {
$voiceCall->setRate($val);
},
"price" => function ($val) use ($voiceCall) {
$voiceCall->setPrice($val);
},
"duration" => function ($val) use ($voiceCall) {
$voiceCall->setDuration($val);
},
];
foreach ($request->request->all() as $key => $val) {
if (array_key_exists($key, $requestMap)) {
$requestMap[$key]($val);
}
}
$em = $this->getEntityManager();
$em->persist($voiceCall);
try {
$em->flush();
if (!empty($delegates)) {
foreach ($delegates as $key => $eachDelegate) {
$eachDelegate();
}
}
return $this->jsonResponse(["success" => true], Response::HTTP_OK);
} catch (\Exception $exception) {
// webhooks need 200 response
return $this->jsonResponse(["success" => false], Response::HTTP_OK);
}
}
/**
* @Get("/answer-url", name="answer_webhook")
*
* @param Request $request
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function getAnswerWebHookAction(Request $request)
{
$callerUser = $this->getDoctrine()->getRepository("AppBundle:User")
->find($request->get("caller_uid", 0));
$eventUrl = base64_decode($request->get('event_url'));
if ($callerUser instanceof User) {
$proxyNumber = (string) $callerUser->getProfile()->getPrimaryContactNumber();
}
$appName = "Spoke Health";
$currentCobrand = $this->get("cobrand.factory")->current();
if ($currentCobrand instanceof CobrandInstance) {
$appName = $currentCobrand->getApp()->getName();
}
$ncoo = [
[
"action" => "talk",
"text" => "Hi! Please stay on the line while we connect you to your assigned employee on {$appName}."
],
[
"action" => "connect",
"timeout" => 45,
"from" => $this->getParameter("nexmo_provisioned_number"),
"eventUrl" => [$eventUrl],
"endpoint" => [[
"type" => "phone",
"number" => $proxyNumber
]]
]
];
return $this->jsonResponse($ncoo, Response::HTTP_OK);
}
}