forked from Rishats/notifyer-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.go
39 lines (33 loc) · 1.01 KB
/
sender.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
)
// sendTextToTelegramChat sends a text message to the Telegram chat identified by its chat Id
func sendTextToTelegramChat(chatId int, text string, telegramBotToken string, telegramParseMode string) (string, error) {
log.Printf("Sending %s to chat_id: %d", text, chatId)
var telegramApi string = "https://api.telegram.org/bot" + telegramBotToken + "/sendMessage"
response, err := http.PostForm(
telegramApi,
url.Values{
"chat_id": {strconv.Itoa(chatId)},
"text": {text},
"parse_mode": {telegramParseMode},
})
if err != nil {
log.Printf("error when posting text to the chat: %s", err.Error())
return "", err
}
defer response.Body.Close()
var bodyBytes, errRead = ioutil.ReadAll(response.Body)
if errRead != nil {
log.Printf("error in parsing telegram answer %s", errRead.Error())
return "", err
}
bodyString := string(bodyBytes)
log.Printf("Body of Telegram Response: %s", bodyString)
return bodyString, nil
}