-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package telegram | ||
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
const ( | ||
// APISendMessageURL api url to send telegram message | ||
APISendMessageURL = "https://api.telegram.org/bot%s/sendMessage" | ||
// bodySendMessageFormat is format of send message | ||
bodySendMessageFormat = `{"chat_id":"%s","text":"%s","disable_notification":"%b"}` | ||
) | ||
|
||
type Bot struct { | ||
Token string | ||
Prefix string | ||
} | ||
|
||
func NewBot(token, prefix string) *Bot { | ||
return &Bot{Token: token, Prefix: prefix} | ||
} | ||
|
||
// SendMessage sending message to client or chat | ||
func (b Bot) SendMessage(chatID, text string) (bool, error) { | ||
var err error | ||
var success bool | ||
defer func() { | ||
if x := recover(); x != nil { | ||
log.Printf("%s run time panic: %v\n", time.Now().Format("2006-01-02 15:04:05"), x) | ||
success = false | ||
err = errors.New(fmt.Sprintf("%v", x)) | ||
} | ||
}() | ||
tr := &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
} | ||
client := &http.Client{Transport: tr} | ||
URL := fmt.Sprintf(APISendMessageURL, b.Token) | ||
body := fmt.Sprintf(bodySendMessageFormat, chatID, b.Prefix+text, 0) | ||
var jsonStr = []byte(body) | ||
req, err := http.NewRequest("POST", URL, bytes.NewBuffer(jsonStr)) | ||
req.Header.Set("Content-Type", "application/json") | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.Println(time.Now().Format("2006-01-02 15:04:05"), err.Error()) | ||
log.Println(time.Now().Format("2006-01-02 15:04:05"), "response Status:", resp.Status) | ||
responseBody, _ := ioutil.ReadAll(resp.Body) | ||
log.Println(time.Now().Format("2006-01-02 15:04:05"), "response Body:", string(responseBody)) | ||
success = false | ||
} else { | ||
success = true | ||
} | ||
defer resp.Body.Close() | ||
return success, err | ||
} |