-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.go
51 lines (44 loc) · 1.02 KB
/
telegram.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
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"io"
"log"
"net/http"
"net/url"
"os"
"strconv"
)
var TelegramTOKEN string = os.Getenv("TOKEN")
type Update struct {
UpdateId int `json:"update_id"`
Message Message `json:"message"`
}
type Message struct {
Text string `json:"text"`
Chat Chat `json:"chat"`
}
type Chat struct {
Id int `json:"id"`
}
func SendTextToTelegramChat(chatId int, text string) (string, error) {
var telegramApi string = "https://api.telegram.org/bot" + TelegramTOKEN + "/sendMessage"
parseMode := "Markdown"
response, err := http.PostForm(
telegramApi,
url.Values{
"chatID": {strconv.Itoa(chatId)},
"text": {text},
"parseMode": {parseMode},
})
if err != nil {
log.Printf("error when posting text to the chat: %s", err.Error())
return "", err
}
defer response.Body.Close()
var bodyBytes, errRead = io.ReadAll(response.Body)
if errRead != nil {
log.Printf("error in parsing telegram answer %s", errRead.Error())
return "", err
}
bodyString := string(bodyBytes)
return bodyString, nil
}