77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
package tg
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"91porn-server/common/httputil"
|
|
)
|
|
|
|
const (
|
|
botApi = "https://api.telegram.org"
|
|
|
|
TianTian_Bot = "1653674118:AAFXJVpLD2Vc0jHL59ZrqKAUbGRRr1vhUt8" //yinse_zabbix
|
|
|
|
ChatIdServerTeam = -500730570 //服务器小组
|
|
ChatIdRechargeTeam = -1001467392117 //业务监控告警群
|
|
ChatIdOPSTeam = -392146367 //运维群
|
|
ChatIdBILLTeam = -354751425 //支付报警群
|
|
|
|
getMe botMethod = "getMe"
|
|
sendMessage botMethod = "sendMessage"
|
|
)
|
|
|
|
type botMethod = string
|
|
|
|
type Bot struct {
|
|
token string
|
|
msgPayload string
|
|
sendRetry int
|
|
}
|
|
|
|
type Msg struct {
|
|
ChatId int64
|
|
Text string
|
|
}
|
|
|
|
type Panic struct {
|
|
ChatId int64
|
|
Err interface{}
|
|
}
|
|
|
|
func New(token string, msgPayload string) *Bot {
|
|
return &Bot{token: token, msgPayload: msgPayload, sendRetry: 3}
|
|
}
|
|
|
|
func (c *Bot) makeUrl(m botMethod) string {
|
|
return fmt.Sprintf("%s/bot%s/%s", botApi, c.token, m)
|
|
}
|
|
|
|
func (c *Bot) Send(t Msg, opts ...*SendOpt) error {
|
|
req := map[string]interface{}{
|
|
"chat_id": t.ChatId,
|
|
"text": c.msgPayload + t.Text,
|
|
}
|
|
opt := mergeSendOpt(opts)
|
|
if opt.ParseMode != nil {
|
|
if *opt.ParseMode == MARKDOWN {
|
|
req["text"] = "```" + c.msgPayload + "```" + t.Text
|
|
}
|
|
req["parse_mode"] = *opt.ParseMode
|
|
}
|
|
retry := c.sendRetry
|
|
url := c.makeUrl(sendMessage)
|
|
var err error
|
|
for retry != 0 {
|
|
retry--
|
|
resp := struct {
|
|
Description string `json:"description"`
|
|
OK bool `json:"ok"`
|
|
ErrorCode int `json:"error_code"`
|
|
}{}
|
|
if _, err = httputil.DefaultClientPostJsonWithResp(&resp, url, nil, req); err == nil {
|
|
return nil
|
|
}
|
|
}
|
|
return err
|
|
}
|