59 lines
1.0 KiB
Go
59 lines
1.0 KiB
Go
package telegram
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"log"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
type TelegramChallenger interface {
|
|
Send(message string)
|
|
}
|
|
|
|
type TelegramBotService struct {
|
|
Key string
|
|
ChatId string
|
|
}
|
|
|
|
func SpawnTelegramChallenger() TelegramChallenger {
|
|
return &TelegramBotService{
|
|
Key: os.Getenv("TELEGRAM_TOKEN"),
|
|
ChatId: os.Getenv("TELEGRAM_CHATID"),
|
|
}
|
|
}
|
|
|
|
func (bot *TelegramBotService) Send(message string) {
|
|
|
|
url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", bot.Key)
|
|
method := "GET"
|
|
|
|
payload := &bytes.Buffer{}
|
|
writer := multipart.NewWriter(payload)
|
|
_ = writer.WriteField("text", message)
|
|
_ = writer.WriteField("chat_id", bot.ChatId)
|
|
err := writer.Close()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
client := &http.Client{}
|
|
req, err := http.NewRequest(method, url, payload)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
|
res, err := client.Do(req)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
defer res.Body.Close()
|
|
log.Println(res)
|
|
}
|