Initial commit

This commit is contained in:
Balakrishnan Balasubramanian 2022-12-23 23:43:19 -05:00
commit d6ba4236df
2 changed files with 162 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module gitea.balki.me/telegram-msgchkbox
go 1.19

159
main.go Normal file
View File

@ -0,0 +1,159 @@
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
)
var api_token = "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 sendButtonAndDelete(update.Message.Chat.ID, update.Message.ID, update.Message.Text)
} else if update.CallbackQuery != nil {
answer_url := fmt.Sprintf("https://api.telegram.org/bot%s/answerCallbackQuery?callback_query_id=%stext=ok", api_token, update.CallbackQuery.ID)
resp, err := http.Get(answer_url)
if err != nil {
log.Println(err)
return
}
body, err = io.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return
}
log.Println(string(body))
}
})
port := 28923
log.Panic(http.ListenAndServe(fmt.Sprintf(":%v", port), nil))
}
func sendButtonAndDelete(chatId int, messageId int, text string) {
url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", api_token)
editReqFmt := `
{
"chat_id": "%d",
"text": "ok",
"reply_markup": {
"inline_keyboard": [[
{
"text": "%s",
"callback_data": "42"
}
]]
}
`
editReq := fmt.Sprintf(editReqFmt, chatId, text)
resp, err := http.Post(url, "application/json", strings.NewReader(editReq))
if err != nil {
log.Println(err)
return
}
log.Println(resp)
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return
}
log.Println(string(body))
delete_url := fmt.Sprintf("https://api.telegram.org/bot%s/deleteMessage?chat_id=%d&message_id=%d", api_token, chatId, messageId)
resp, err = http.Get(delete_url)
if err != nil {
log.Println(err)
return
}
body, err = io.ReadAll(resp.Body)
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"
*/