From a0c6e9a3d1bc79c44ad121412a6694c1bb539a6a Mon Sep 17 00:00:00 2001 From: tantra35 Date: Fri, 15 Jan 2016 19:38:52 +0300 Subject: [PATCH 1/3] 1. Fix voice call 2. Rename configuration parameters 3. Add new configuration parameter to twilio voice: voiceUrl --- twilio/twilio.go | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/twilio/twilio.go b/twilio/twilio.go index 1f8ea0f..371ad19 100644 --- a/twilio/twilio.go +++ b/twilio/twilio.go @@ -29,6 +29,7 @@ type twilioSenderSms struct { type twilioSenderVoice struct { twilioSender + voiceUrl string } func (smsSender *twilioSenderSms) SendEvents(events notifier.EventsData, contact notifier.ContactData, trigger notifier.TriggerData, throttled bool) error { @@ -65,7 +66,14 @@ func (smsSender *twilioSenderSms) SendEvents(events notifier.EventsData, contact } func (voiceSender *twilioSenderVoice) SendEvents(events notifier.EventsData, contact notifier.ContactData, trigger notifier.TriggerData, throttled bool) error { - twilio.NewCall(voiceSender.client, voiceSender.APIFromPhone, contact.Value, nil) + twilioCall, err := twilio.NewCall(voiceSender.client, voiceSender.APIFromPhone, contact.Value, twilio.Callback(voiceSender.voiceUrl)) + + if err != nil { + return fmt.Errorf("Failed to make call to contact %s: %s", contact.Value, err.Error()) + } + + voiceSender.log.Debug(fmt.Sprintf("call queued to twilio with status: %s", twilioCall.Status)) + return nil } @@ -76,30 +84,36 @@ type Sender struct { //Init read yaml config func (sender *Sender) Init(senderSettings map[string]string, logger *logging.Logger) error { - apiSID := senderSettings["api_sid"] - if apiSID == "" { - return fmt.Errorf("Can not read twilio api_sid from config") + apiType := senderSettings["type"] + + apiASID := senderSettings["api_asid"] + if apiASID == "" { + return fmt.Errorf("Can not read [" + apiType + "] api_sid param from config") } - apiSecret := senderSettings["api_secret"] - if apiSecret == "" { - return fmt.Errorf("Can not read twilio api_secret from config") + apiAuthToken := senderSettings["api_authtoken"] + if apiAuthToken == "" { + return fmt.Errorf("Can not read [" + apiType + "] api_authtoken param from config") } apiFromPhone := senderSettings["api_fromphone"] if apiFromPhone == "" { - return fmt.Errorf("Can not read twilio from phone") + return fmt.Errorf("Can not read [" + apiType + "] api_fromphone param from config") } - apiType := senderSettings["type"] - twilioClient := twilio.NewClient(apiSID, apiSecret) + twilioClient := twilio.NewClient(apiASID, apiAuthToken) switch apiType { case "twilio sms": sender.sender = &twilioSenderSms{twilioSender{twilioClient, apiFromPhone, logger}} case "twilio voice": - sender.sender = &twilioSenderVoice{twilioSender{twilioClient, apiFromPhone, logger}} + voiceUrl := senderSettings["voiceurl"] + if voiceUrl == "" { + return fmt.Errorf("Can not read [" + apiType + "] voiceurl param from config") + } + + sender.sender = &twilioSenderVoice{twilioSender{twilioClient, apiFromPhone, logger}, voiceUrl} default: return fmt.Errorf("Wrong twilio type: %s", apiType) From d1974901a764e12b72601910d9bcf066508cdc97 Mon Sep 17 00:00:00 2001 From: tantra35 Date: Fri, 15 Jan 2016 19:41:04 +0300 Subject: [PATCH 2/3] fix formating --- twilio/twilio.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twilio/twilio.go b/twilio/twilio.go index 371ad19..368fee8 100644 --- a/twilio/twilio.go +++ b/twilio/twilio.go @@ -108,7 +108,7 @@ func (sender *Sender) Init(senderSettings map[string]string, logger *logging.Log sender.sender = &twilioSenderSms{twilioSender{twilioClient, apiFromPhone, logger}} case "twilio voice": - voiceUrl := senderSettings["voiceurl"] + voiceUrl := senderSettings["voiceurl"] if voiceUrl == "" { return fmt.Errorf("Can not read [" + apiType + "] voiceurl param from config") } From 71343dcabf99f7c232040251b260235039063777 Mon Sep 17 00:00:00 2001 From: tantra35 Date: Sun, 17 Jan 2016 21:33:58 +0300 Subject: [PATCH 3/3] fix code formatting, and variable names to conform go code-style --- twilio/twilio.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/twilio/twilio.go b/twilio/twilio.go index 368fee8..8f4ba36 100644 --- a/twilio/twilio.go +++ b/twilio/twilio.go @@ -29,7 +29,7 @@ type twilioSenderSms struct { type twilioSenderVoice struct { twilioSender - voiceUrl string + voiceURL string } func (smsSender *twilioSenderSms) SendEvents(events notifier.EventsData, contact notifier.ContactData, trigger notifier.TriggerData, throttled bool) error { @@ -57,7 +57,7 @@ func (smsSender *twilioSenderSms) SendEvents(events notifier.EventsData, contact twilioMessage, err := twilio.NewMessage(smsSender.client, smsSender.APIFromPhone, contact.Value, twilio.Body(message.String())) if err != nil { - return fmt.Errorf("Failed to send message to contact %s: %s", contact.Value, err.Error()) + return fmt.Errorf("Failed to send message to contact %s: %s", contact.Value, err) } smsSender.log.Debug(fmt.Sprintf("message send to twilio with status: %s", twilioMessage.Status)) @@ -66,10 +66,10 @@ func (smsSender *twilioSenderSms) SendEvents(events notifier.EventsData, contact } func (voiceSender *twilioSenderVoice) SendEvents(events notifier.EventsData, contact notifier.ContactData, trigger notifier.TriggerData, throttled bool) error { - twilioCall, err := twilio.NewCall(voiceSender.client, voiceSender.APIFromPhone, contact.Value, twilio.Callback(voiceSender.voiceUrl)) + twilioCall, err := twilio.NewCall(voiceSender.client, voiceSender.APIFromPhone, contact.Value, twilio.Callback(voiceSender.voiceURL)) if err != nil { - return fmt.Errorf("Failed to make call to contact %s: %s", contact.Value, err.Error()) + return fmt.Errorf("Failed to make call to contact %s: %s", contact.Value, err) } voiceSender.log.Debug(fmt.Sprintf("call queued to twilio with status: %s", twilioCall.Status)) @@ -88,17 +88,17 @@ func (sender *Sender) Init(senderSettings map[string]string, logger *logging.Log apiASID := senderSettings["api_asid"] if apiASID == "" { - return fmt.Errorf("Can not read [" + apiType + "] api_sid param from config") + return fmt.Errorf("Can not read [%s] api_sid param from config", apiType) } apiAuthToken := senderSettings["api_authtoken"] if apiAuthToken == "" { - return fmt.Errorf("Can not read [" + apiType + "] api_authtoken param from config") + return fmt.Errorf("Can not read [%s] api_authtoken param from config", apiType) } apiFromPhone := senderSettings["api_fromphone"] if apiFromPhone == "" { - return fmt.Errorf("Can not read [" + apiType + "] api_fromphone param from config") + return fmt.Errorf("Can not read [%s] api_fromphone param from config", apiType) } twilioClient := twilio.NewClient(apiASID, apiAuthToken) @@ -108,12 +108,12 @@ func (sender *Sender) Init(senderSettings map[string]string, logger *logging.Log sender.sender = &twilioSenderSms{twilioSender{twilioClient, apiFromPhone, logger}} case "twilio voice": - voiceUrl := senderSettings["voiceurl"] - if voiceUrl == "" { - return fmt.Errorf("Can not read [" + apiType + "] voiceurl param from config") + voiceURL := senderSettings["voiceurl"] + if voiceURL == "" { + return fmt.Errorf("Can not read [%s] voiceurl param from config", apiType) } - sender.sender = &twilioSenderVoice{twilioSender{twilioClient, apiFromPhone, logger}, voiceUrl} + sender.sender = &twilioSenderVoice{twilioSender{twilioClient, apiFromPhone, logger}, voiceURL} default: return fmt.Errorf("Wrong twilio type: %s", apiType)