2018-08-16 06:22:20 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2018-12-04 04:17:29 +00:00
// https://github.com/hunterlong/statping
2018-08-16 06:22:20 +00:00
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-07-19 03:20:43 +00:00
package notifiers
import (
2018-09-15 22:21:58 +00:00
"encoding/json"
"errors"
2018-07-19 03:20:43 +00:00
"fmt"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/core/notifier"
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
2018-07-19 03:20:43 +00:00
"net/url"
"strings"
"time"
)
2018-09-15 22:39:17 +00:00
type twilio struct {
2018-09-12 04:14:22 +00:00
* notifier . Notification
2018-07-19 03:20:43 +00:00
}
2018-09-15 22:39:17 +00:00
var twilioNotifier = & twilio { & notifier . Notification {
2018-09-16 07:48:34 +00:00
Method : "twilio" ,
Title : "Twilio" ,
Description : "Receive SMS text messages directly to your cellphone when a service is offline. You can use a Twilio test account with limits. This notifier uses the <a href=\"https://www.twilio.com/docs/usage/api\">Twilio API</a>." ,
2018-09-15 01:18:21 +00:00
Author : "Hunter Long" ,
AuthorUrl : "https://github.com/hunterlong" ,
2018-11-01 14:37:20 +00:00
Icon : "far fa-comment-alt" ,
2018-09-15 01:18:21 +00:00
Delay : time . Duration ( 10 * time . Second ) ,
2018-09-12 04:14:22 +00:00
Form : [ ] notifier . NotificationForm { {
Type : "text" ,
Title : "Account Sid" ,
2018-09-16 07:48:34 +00:00
Placeholder : "Insert your Twilio Account Sid" ,
2018-09-12 04:14:22 +00:00
DbField : "api_key" ,
2018-09-27 01:49:21 +00:00
Required : true ,
2018-09-12 04:14:22 +00:00
} , {
Type : "text" ,
Title : "Account Token" ,
2018-09-16 07:48:34 +00:00
Placeholder : "Insert your Twilio Account Token" ,
2018-09-12 04:14:22 +00:00
DbField : "api_secret" ,
2018-09-27 01:49:21 +00:00
Required : true ,
2018-09-12 04:14:22 +00:00
} , {
Type : "text" ,
Title : "SMS to Phone Number" ,
2018-09-15 22:21:58 +00:00
Placeholder : "18555555555" ,
2018-09-12 04:14:22 +00:00
DbField : "Var1" ,
2018-09-27 01:49:21 +00:00
Required : true ,
2018-09-12 04:14:22 +00:00
} , {
Type : "text" ,
Title : "From Phone Number" ,
2018-09-15 22:21:58 +00:00
Placeholder : "18555555555" ,
2018-09-12 04:14:22 +00:00
DbField : "Var2" ,
2018-09-27 01:49:21 +00:00
Required : true ,
2018-09-12 04:14:22 +00:00
} } } ,
2018-07-19 03:20:43 +00:00
}
// DEFINE YOUR NOTIFICATION HERE.
func init ( ) {
2018-09-15 22:39:17 +00:00
err := notifier . AddNotifier ( twilioNotifier )
2018-09-10 09:01:04 +00:00
if err != nil {
2018-09-12 04:14:22 +00:00
panic ( err )
2018-09-10 09:01:04 +00:00
}
2018-07-19 03:20:43 +00:00
}
2018-09-15 22:39:17 +00:00
func ( u * twilio ) Select ( ) * notifier . Notification {
2018-09-15 01:18:21 +00:00
return u . Notification
2018-07-19 03:20:43 +00:00
}
2018-09-15 22:39:17 +00:00
// Send will send a HTTP Post to the Twilio SMS API. It accepts type: string
func ( u * twilio ) Send ( msg interface { } ) error {
2018-09-15 01:18:21 +00:00
message := msg . ( string )
2018-09-16 07:48:34 +00:00
twilioUrl := fmt . Sprintf ( "https://api.twilio.com/2010-04-01/Accounts/%v/Messages.json" , u . GetValue ( "api_key" ) )
2018-11-25 03:56:09 +00:00
2018-09-15 01:18:21 +00:00
v := url . Values { }
2018-09-15 22:21:58 +00:00
v . Set ( "To" , "+" + u . Var1 )
v . Set ( "From" , "+" + u . Var2 )
2018-09-15 01:18:21 +00:00
v . Set ( "Body" , message )
rb := * strings . NewReader ( v . Encode ( ) )
2018-11-25 03:56:09 +00:00
2018-11-25 10:18:21 +00:00
contents , _ , err := utils . HttpRequest ( twilioUrl , "POST" , "application/x-www-form-urlencoded" , nil , & rb , time . Duration ( 10 * time . Second ) )
2018-09-27 01:49:21 +00:00
success , _ := twilioSuccess ( contents )
2018-09-15 22:21:58 +00:00
if ! success {
2018-09-27 01:49:21 +00:00
errorOut := twilioError ( contents )
out := fmt . Sprintf ( "Error code %v - %v" , errorOut . Code , errorOut . Message )
return errors . New ( out )
2018-09-15 22:21:58 +00:00
}
2018-11-25 03:56:09 +00:00
return err
2018-07-19 03:20:43 +00:00
}
2018-09-15 22:39:17 +00:00
// OnFailure will trigger failing service
func ( u * twilio ) OnFailure ( s * types . Service , f * types . Failure ) {
2018-09-15 01:18:21 +00:00
msg := fmt . Sprintf ( "Your service '%v' is currently offline!" , s . Name )
2018-11-01 14:37:20 +00:00
u . AddQueue ( s . Id , msg )
2018-09-21 04:17:24 +00:00
u . Online = false
2018-07-19 03:20:43 +00:00
}
2018-09-15 22:39:17 +00:00
// OnSuccess will trigger successful service
func ( u * twilio ) OnSuccess ( s * types . Service ) {
2018-09-21 04:17:24 +00:00
if ! u . Online {
2018-11-01 14:37:20 +00:00
u . ResetUniqueQueue ( s . Id )
2018-09-21 04:17:24 +00:00
msg := fmt . Sprintf ( "Your service '%v' is back online!" , s . Name )
2018-11-01 14:37:20 +00:00
u . AddQueue ( s . Id , msg )
2018-09-21 04:17:24 +00:00
}
u . Online = true
2018-07-19 03:20:43 +00:00
}
2018-09-15 22:39:17 +00:00
// OnSave triggers when this notifier has been saved
func ( u * twilio ) OnSave ( ) error {
2018-07-19 03:20:43 +00:00
utils . Log ( 1 , fmt . Sprintf ( "Notification %v is receiving updated information." , u . Method ) )
// Do updating stuff here
return nil
}
2018-09-15 22:21:58 +00:00
2018-09-27 01:49:21 +00:00
// OnTest will test the Twilio SMS messaging
func ( u * twilio ) OnTest ( ) error {
msg := fmt . Sprintf ( "Testing the Twilio SMS Notifier" )
return u . Send ( msg )
}
2018-10-06 05:03:10 +00:00
func twilioSuccess ( res [ ] byte ) ( bool , twilioResponse ) {
var obj twilioResponse
2018-09-15 22:21:58 +00:00
json . Unmarshal ( res , & obj )
if obj . Status == "queued" {
return true , obj
}
return false , obj
}
2018-10-06 05:03:10 +00:00
func twilioError ( res [ ] byte ) twilioErrorObj {
var obj twilioErrorObj
2018-09-27 01:49:21 +00:00
json . Unmarshal ( res , & obj )
return obj
}
2018-10-06 05:03:10 +00:00
type twilioErrorObj struct {
2018-09-27 01:49:21 +00:00
Code int ` json:"code" `
Message string ` json:"message" `
MoreInfo string ` json:"more_info" `
Status int ` json:"status" `
}
2018-10-06 05:03:10 +00:00
type twilioResponse struct {
2018-09-15 22:21:58 +00:00
Sid string ` json:"sid" `
DateCreated string ` json:"date_created" `
DateUpdated string ` json:"date_updated" `
DateSent interface { } ` json:"date_sent" `
AccountSid string ` json:"account_sid" `
To string ` json:"to" `
From string ` json:"from" `
MessagingServiceSid interface { } ` json:"messaging_service_sid" `
Body string ` json:"body" `
Status string ` json:"status" `
NumSegments string ` json:"num_segments" `
NumMedia string ` json:"num_media" `
Direction string ` json:"direction" `
APIVersion string ` json:"api_version" `
Price interface { } ` json:"price" `
PriceUnit string ` json:"price_unit" `
ErrorCode interface { } ` json:"error_code" `
ErrorMessage interface { } ` json:"error_message" `
URI string ` json:"uri" `
SubresourceUris struct {
Media string ` json:"media" `
} ` json:"subresource_uris" `
}