2020-03-12 08:09:25 +00:00
package notifiers
2018-12-10 05:43:15 +00:00
import (
"encoding/json"
"errors"
"fmt"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/failures"
2020-03-14 03:13:20 +00:00
"github.com/statping/statping/types/notifications"
"github.com/statping/statping/types/notifier"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/services"
"github.com/statping/statping/utils"
2018-12-10 05:43:15 +00:00
"net/url"
"strings"
"time"
)
2020-03-14 03:13:20 +00:00
var _ notifier . Notifier = ( * telegram ) ( nil )
2020-03-09 15:15:15 +00:00
2018-12-10 05:43:15 +00:00
type telegram struct {
2020-03-14 03:13:20 +00:00
* notifications . Notification
2018-12-10 05:43:15 +00:00
}
2020-03-14 03:13:20 +00:00
func ( t * telegram ) Select ( ) * notifications . Notification {
return t . Notification
}
var Telegram = & telegram { & notifications . Notification {
2018-12-10 05:43:15 +00:00
Method : "telegram" ,
Title : "Telegram" ,
Description : "Receive notifications on your Telegram channel when a service has an issue. You must get a Telegram API token from the /botfather. Review the <a target=\"_blank\" href=\"http://techthoughts.info/how-to-create-a-telegram-bot-and-send-messages-via-api\">Telegram API Tutorial</a> to learn how to generate a new API Token." ,
Author : "Hunter Long" ,
AuthorUrl : "https://github.com/hunterlong" ,
Icon : "fab fa-telegram-plane" ,
Delay : time . Duration ( 5 * time . Second ) ,
2020-06-15 04:46:28 +00:00
SuccessData : "Your service '{{.Service.Name}}' is currently online!" ,
FailureData : "Your service '{{.Service.Name}}' is currently offline!" ,
DataType : "text" ,
2020-03-17 03:13:07 +00:00
Limits : 60 ,
2020-03-14 03:13:20 +00:00
Form : [ ] notifications . NotificationForm { {
2018-12-10 05:43:15 +00:00
Type : "text" ,
Title : "Telegram API Token" ,
Placeholder : "383810182:EEx829dtCeufeQYXG7CUdiQopqdmmxBPO7-s" ,
SmallText : "Enter the API Token given to you from the /botfather chat." ,
DbField : "api_secret" ,
Required : true ,
} , {
Type : "text" ,
2020-04-08 07:30:08 +00:00
Title : "Channel or User" ,
Placeholder : "@statping_channel" ,
SmallText : "Insert your Telegram Channel or User here." ,
DbField : "var1" ,
2018-12-10 05:43:15 +00:00
Required : true ,
} } } ,
}
// Send will send a HTTP Post to the Telegram API. It accepts type: string
2020-04-11 05:59:51 +00:00
func ( t * telegram ) sendMessage ( message string ) ( string , error ) {
2020-03-23 02:50:30 +00:00
apiEndpoint := fmt . Sprintf ( "https://api.telegram.org/bot%v/sendMessage" , t . ApiSecret )
2018-12-10 05:43:15 +00:00
v := url . Values { }
2020-03-23 02:50:30 +00:00
v . Set ( "chat_id" , t . Var1 )
2018-12-10 05:43:15 +00:00
v . Set ( "text" , message )
rb := * strings . NewReader ( v . Encode ( ) )
2020-05-20 06:41:50 +00:00
contents , _ , err := utils . HttpRequest ( apiEndpoint , "GET" , "application/x-www-form-urlencoded" , nil , & rb , time . Duration ( 10 * time . Second ) , true , nil )
2018-12-10 05:43:15 +00:00
success , _ := telegramSuccess ( contents )
if ! success {
errorOut := telegramError ( contents )
out := fmt . Sprintf ( "Error code %v - %v" , errorOut . ErrorCode , errorOut . Description )
2020-04-11 05:59:51 +00:00
return string ( contents ) , errors . New ( out )
2018-12-10 05:43:15 +00:00
}
2020-04-11 05:59:51 +00:00
return string ( contents ) , err
2018-12-10 05:43:15 +00:00
}
// OnFailure will trigger failing service
2020-06-26 04:08:12 +00:00
func ( t * telegram ) OnFailure ( s services . Service , f failures . Failure ) ( string , error ) {
2020-06-15 04:46:28 +00:00
msg := ReplaceVars ( t . FailureData , s , f )
2020-06-15 07:46:27 +00:00
out , err := t . sendMessage ( msg )
return out , err
2018-12-10 05:43:15 +00:00
}
// OnSuccess will trigger successful service
2020-06-26 04:08:12 +00:00
func ( t * telegram ) OnSuccess ( s services . Service ) ( string , error ) {
msg := ReplaceVars ( t . SuccessData , s , failures . Failure { } )
2020-06-15 07:46:27 +00:00
out , err := t . sendMessage ( msg )
return out , err
2018-12-10 05:43:15 +00:00
}
// OnTest will test the Twilio SMS messaging
2020-04-11 05:59:51 +00:00
func ( t * telegram ) OnTest ( ) ( string , error ) {
2019-01-09 04:20:43 +00:00
msg := fmt . Sprintf ( "Testing the Twilio SMS Notifier on your Statping server" )
2020-04-11 05:59:51 +00:00
content , err := t . sendMessage ( msg )
return content , err
2018-12-10 05:43:15 +00:00
}
2020-06-21 06:52:07 +00:00
// OnSave will trigger when this notifier is saved
func ( t * telegram ) OnSave ( ) ( string , error ) {
return "" , nil
}
2018-12-10 05:43:15 +00:00
func telegramSuccess ( res [ ] byte ) ( bool , telegramResponse ) {
var obj telegramResponse
json . Unmarshal ( res , & obj )
if obj . Ok {
return true , obj
}
return false , obj
}
func telegramError ( res [ ] byte ) telegramErrorObj {
var obj telegramErrorObj
json . Unmarshal ( res , & obj )
return obj
}
type telegramErrorObj struct {
Ok bool ` json:"ok" `
ErrorCode int ` json:"error_code" `
Description string ` json:"description" `
}
type telegramResponse struct {
Ok bool ` json:"ok" `
Result struct {
MessageID int ` json:"message_id" `
From struct {
ID int ` json:"id" `
IsBot bool ` json:"is_bot" `
FirstName string ` json:"first_name" `
Username string ` json:"username" `
} ` json:"from" `
Chat struct {
ID int ` json:"id" `
FirstName string ` json:"first_name" `
LastName string ` json:"last_name" `
Username string ` json:"username" `
Type string ` json:"type" `
} ` json:"chat" `
Date int ` json:"date" `
Text string ` json:"text" `
} ` json:"result" `
}