199 lines
4.3 KiB
Go
199 lines
4.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
var apiToken = "421791796:AAE4wPbcqfLP1GNeGR3RTBiyX16fCj3HPAM"
|
|
|
|
func main() {
|
|
fmt.Println("vim-go")
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
})
|
|
http.HandleFunc("/zeexkfcsdjpmncirkyelwzotjmmefcqtcogrfwnafidionxiacwnslwuhbwfuppjgwzbmazd", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("ok"))
|
|
body, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
log.Println(string(body))
|
|
update := struct {
|
|
Message *struct {
|
|
ID int `json:"message_id"`
|
|
Chat struct {
|
|
ID int `json:"id"`
|
|
}
|
|
Text string
|
|
}
|
|
CallbackQuery *struct {
|
|
ID string `json:"id"`
|
|
Message struct {
|
|
ID int `json:"message_id"`
|
|
Chat struct {
|
|
ID int `json:"id"`
|
|
}
|
|
}
|
|
Data string
|
|
} `json:"callback_query"`
|
|
}{}
|
|
err = json.Unmarshal(body, &update)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
|
|
if update.Message != nil {
|
|
go sendButton(update.Message.Chat.ID, update.Message.ID, update.Message.Text)
|
|
go DeleteMessage(update.Message.Chat.ID, update.Message.ID)
|
|
} else if update.CallbackQuery != nil {
|
|
go markButtonChecked(update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Message.ID, update.CallbackQuery.Data)
|
|
go answerCallbackQuery(update.CallbackQuery.ID)
|
|
}
|
|
})
|
|
port := 28923
|
|
log.Panic(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
|
|
}
|
|
|
|
func markButtonChecked(chatID int, messageID int, text string) {
|
|
url := fmt.Sprintf("https://api.telegram.org/bot%s/editMessageText", apiToken)
|
|
text = fmt.Sprintf("✓ %s", text)
|
|
sendMsgFmt := `
|
|
{
|
|
"chat_id": "%d",
|
|
"message_id": "%d",
|
|
"text": "ok",
|
|
"reply_markup": {
|
|
"inline_keyboard": [
|
|
[
|
|
{
|
|
"text": "%s",
|
|
"callback_data": "%s"
|
|
}
|
|
]
|
|
]
|
|
}
|
|
}
|
|
`
|
|
sendMsgReq := fmt.Sprintf(sendMsgFmt, chatID, messageID, text, text)
|
|
resp, err := http.Post(url, "application/json", strings.NewReader(sendMsgReq))
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
logBody(resp.Body)
|
|
}
|
|
|
|
func answerCallbackQuery(callbackQueryID string) {
|
|
answerUrl := fmt.Sprintf("https://api.telegram.org/bot%s/answerCallbackQuery?callback_query_id=%stext=ok", apiToken, callbackQueryID)
|
|
resp, err := http.Get(answerUrl)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
logBody(resp.Body)
|
|
}
|
|
|
|
func sendButton(chatID int, messageID int, text string) {
|
|
url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", apiToken)
|
|
sendMsgFmt := `
|
|
{
|
|
"chat_id": "%d",
|
|
"text": "ok",
|
|
"reply_markup": {
|
|
"inline_keyboard": [
|
|
[
|
|
{
|
|
"text": "%s",
|
|
"callback_data": "%s"
|
|
}
|
|
]
|
|
]
|
|
}
|
|
}
|
|
`
|
|
sendMsgReq := fmt.Sprintf(sendMsgFmt, chatID, text, text)
|
|
resp, err := http.Post(url, "application/json", strings.NewReader(sendMsgReq))
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
logBody(resp.Body)
|
|
}
|
|
func DeleteMessage(chatID int, messageID int) {
|
|
deleteUrl := fmt.Sprintf("https://api.telegram.org/bot%s/deleteMessage?chat_id=%d&message_id=%d", apiToken, chatID, messageID)
|
|
resp, err := http.Get(deleteUrl)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
logBody(resp.Body)
|
|
}
|
|
|
|
func logBody(respBody io.ReadCloser) {
|
|
defer func() {
|
|
err := respBody.Close()
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
}()
|
|
body, err := io.ReadAll(respBody)
|
|
if err != nil {
|
|
log.Println(err)
|
|
return
|
|
}
|
|
log.Println(string(body))
|
|
}
|
|
|
|
/*
|
|
update_id: 547400623
|
|
message:
|
|
message_id: 869
|
|
from:
|
|
id: 385713421
|
|
is_bot: false
|
|
first_name: Balakrishnan
|
|
language_code: en
|
|
chat:
|
|
id: -832015899
|
|
title: Playbot
|
|
type: group
|
|
all_members_are_administrators: true
|
|
date: 1671850862
|
|
text: Hi
|
|
update_id: 547400624
|
|
callback_query:
|
|
id: "1656626530601531051"
|
|
from:
|
|
id: 385713421
|
|
is_bot: false
|
|
first_name: Balakrishnan
|
|
language_code: en
|
|
message:
|
|
message_id: 868
|
|
from:
|
|
id: 421791796
|
|
is_bot: true
|
|
first_name: grocery_guy
|
|
username: grocery_guy_bot
|
|
chat:
|
|
id: -832015899
|
|
title: Playbot
|
|
type: group
|
|
all_members_are_administrators: true
|
|
date: 1671074930
|
|
text: foo
|
|
reply_markup:
|
|
inline_keyboard:
|
|
- - text: blah
|
|
callback_data: "42"
|
|
chat_instance: "5165047605234327446"
|
|
data: "42"
|
|
*/
|