74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
package ummessage
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
var telegramToken string
|
|
var telegramChatId string
|
|
|
|
func InitTelegramBot(token string, chatId string) {
|
|
telegramToken = token
|
|
telegramChatId = chatId
|
|
}
|
|
|
|
type SendMessageModel struct {
|
|
Parse_mode string
|
|
Chat_id string
|
|
Disable_web_page_preview bool
|
|
Text string
|
|
}
|
|
|
|
func SendMessage(title string, body string) {
|
|
message := `<b>` + title + `</b>
|
|
<b>발생시간(UTC)</b> : <code>` + time.Now().Format("2006-01-02 15:04:05") + `</code>
|
|
<b>내용</b> :
|
|
<pre>` + body + `</pre>`
|
|
messageBody, _ := json.Marshal(map[string]any{
|
|
"chat_id": telegramChatId,
|
|
"text": message,
|
|
"parse_mode": "html",
|
|
})
|
|
sendUrl := "https://api.telegram.org/bot" + telegramToken + "/sendMessage"
|
|
resp, err := http.Post(sendUrl, "application/json", bytes.NewBuffer(messageBody))
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// 결과 출력
|
|
_, err = ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
}
|
|
}
|
|
|
|
func SendMessageDirect(chatId int64, telegramToken string, title string, body string) {
|
|
message := `<b>` + title + `</b>
|
|
<b>발생시간(UTC)</b> : <code>` + time.Now().Format("2006-01-02 15:04:05") + `</code>
|
|
<b>내용</b> :
|
|
` + body
|
|
messageBody, _ := json.Marshal(map[string]any{
|
|
"chat_id": chatId,
|
|
"text": message,
|
|
"parse_mode": "html",
|
|
})
|
|
sendUrl := "https://api.telegram.org/bot" + telegramToken + "/sendMessage"
|
|
resp, err := http.Post(sendUrl, "application/json", bytes.NewBuffer(messageBody))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// 결과 출력
|
|
_, err = ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
fmt.Println(err.Error())
|
|
}
|
|
}
|