File size: 1,183 Bytes
9b76a89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re

import twilio.twiml.voice_response

import vbx
import vbx.config


number = re.compile(r'^[\d\+\-\(\) ]+$')


class Call(vbx.Event):
    def __init__(self, request):
        super().__init__(request)

        self.sid = request['CallSid']
        self.status = request['CallStatus']
        self.direction = request['Direction']

        try:
            self.forwarded_from = request['ForwardedFrom']
        except KeyError:
            self.forwarded_from = None

        try:
            self.caller_name = request['CallerName']
        except KeyError:
            self.caller_name = None

    def handle(self, flows):
        response = twilio.twiml.voice_response.VoiceResponse()

        if self.to == vbx.config.number:
            for flow in flows:
                flow.dial(self, response)

                if not flow.completed and flow.next:
                    response.redirect(flow.next)
                    break
        else:
            dial = response.dial(self.to, caller_id=vbx.config.number)

            if number.match(self.to):
                dial.number(self.to)
            else:
                dial.client(self.to)

        return response