• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /usr/bin/env python3
2# Copyright (C) 2016 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5# use this file except in compliance with the License. You may obtain a copy of
6# the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations under
14# the License.
15
16"""The twilio client that initiates the call."""
17
18# TODO(danielvernon):Generalize client to use any service including phone.
19
20from twilio.rest import Client
21import yaml
22
23ACCOUNT_SID_KEY = 'account_sid'
24AUTH_TOKEN_KEY = 'auth_token'
25PHONE_NUMBER_KEY = 'phone_number'
26MUSIC_URL = 'http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient'
27URLS_KEY = 'urls'
28
29class TwilioClient:
30    """A class that wraps the Twilio Client class and can make calls.
31
32    Attributes:
33         __account_sid: The account id
34         __auth_token: The authentication token
35         __phone_number: The phoone number
36         urls: urls that will be played during call
37    """
38
39    def __init__(self, cfg_path):
40        self.__account_sid = None
41        self.__auth_token = None
42        self.__phone_number = None
43        self.urls = None
44
45        self.load_config(cfg_path)
46        self.client = Client(self.__account_sid, self.__auth_token)
47        self.call_handle = self.client.api.account.calls
48
49    def load_config(self, cfg_path):
50        """Loads the config for twilio.
51
52        Args:
53            cfg_path: A string, which is the path to the config file.
54        """
55        with open(cfg_path) as cfg_file:
56            cfg = yaml.load(cfg_file)
57            self.__account_sid = cfg[ACCOUNT_SID_KEY]
58            self.__auth_token = cfg[AUTH_TOKEN_KEY]
59            self.__phone_number = cfg[PHONE_NUMBER_KEY]
60            self.urls = cfg[URLS_KEY]
61
62    def call(self, to):
63        """Makes request to Twilio API to call number and play music.
64
65        Must be registered with Twilio account in order to use this client.
66        Arguments:
67            to: the number to call (str). example -- '+12345678910'
68
69        Returns:
70            call.sid: the sid of the call request.
71        """
72
73        call = self.call_handle.create(to=to, from_=self.__phone_number,
74                                       url=MUSIC_URL, status_callback=MUSIC_URL)
75        return call.sid
76