-
Notifications
You must be signed in to change notification settings - Fork 28
/
TwilioClick2Dial.vf
58 lines (45 loc) · 1.9 KB
/
TwilioClick2Dial.vf
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
<apex:page controller="TwilioClientController" showHeader="false" >
<script type="text/javascript" src="//static.twilio.com/libs/twiliojs/1.2/twilio.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://na15.salesforce.com/support/api/33.0/interaction.js"></script>
<script type="text/javascript">
Twilio.Device.setup("{! token }");
//OpenCTI!!
sforce.interaction.cti.enableClickToDial();
sforce.interaction.cti.onClickToDial(startCall);
function call() {
params = {"PhoneNumber": $("#number").val()};
Twilio.Device.connect(params);
}
function hangup() {
Twilio.Device.disconnectAll();
}
// Handle incoming calls from Twilio
Twilio.Device.incoming(function (conn) {
if (confirm('Accept incoming call from ' + conn.parameters.From + '?')){
conn.accept();
}
});
function startCall(response) {
sforce.interaction.setVisible(true); //pop up CTI console
var result = JSON.parse(response.result);
var cleanednumber = cleanFormatting(result.number);
params = {"PhoneNumber": cleanednumber};
Twilio.Device.connect(params);
}
function cleanFormatting(number) {
//changes a SFDC formatted US number, which would be 415-555-1212 into a twilio understanble number 4155551212
return number.replace(' ','').replace('-','').replace('(','').replace(')','').replace('+','');
}
</script>
<div>
<input type="text" id="number" name="number"
placeholder="Enter a phone number to call"/>
</div>
<button class="call" onclick="call();">
Call
</button>
<button class="hangup" onclick="hangup();">
Hangup
</button>
</apex:page>