go_telegram/cmd/main.go

64 lines
1.4 KiB
Go
Raw Normal View History

2020-01-21 19:38:02 +00:00
package main
import (
"log"
"github.com/mattburchett/go_telegram/pkg/config"
2020-01-22 20:51:26 +00:00
"github.com/yanzay/tbot/v2"
2020-01-21 19:38:02 +00:00
)
2020-01-22 20:51:26 +00:00
type application struct {
client *tbot.Client
callbackChatID string
callbackMessageID int
}
2020-01-21 19:38:02 +00:00
2020-01-22 20:51:26 +00:00
func main() {
2020-01-21 19:38:02 +00:00
cfg, err := config.GetConfig("config.json")
if err != nil {
2020-01-22 20:51:26 +00:00
log.Fatal("Failed to read JSON.")
2020-01-21 19:38:02 +00:00
}
2020-01-22 20:51:26 +00:00
app := &application{}
bot := tbot.New(cfg.TelegramToken)
app.client = bot.Client()
c := bot.Client()
bot.HandleMessage("/ping", func(m *tbot.Message) {
c.SendMessage(m.Chat.ID, "pong")
2020-01-21 19:38:02 +00:00
})
2020-01-22 20:51:26 +00:00
bot.HandleMessage("/test", app.testHandler)
bot.HandleCallback(app.callbackHandler)
err = bot.Start()
2020-01-21 19:38:02 +00:00
if err != nil {
log.Fatal(err)
}
}
2020-01-22 20:51:26 +00:00
func (a *application) testHandler(m *tbot.Message) {
buttons := make([]string, 0)
buttons = append(buttons, "ping", "is", "stupid")
2020-01-21 19:38:02 +00:00
2020-01-22 20:51:26 +00:00
inline2 := make([][]tbot.InlineKeyboardButton, 0)
2020-01-21 20:51:19 +00:00
2020-01-22 20:51:26 +00:00
for _, i := range buttons {
inline2 = append(inline2, []tbot.InlineKeyboardButton{{
Text: i,
CallbackData: i,
}})
}
2020-01-21 19:38:02 +00:00
2020-01-22 20:51:26 +00:00
msg, _ := a.client.SendMessage(m.Chat.ID, "Inline test.", tbot.OptInlineKeyboardMarkup(&tbot.InlineKeyboardMarkup{InlineKeyboard: inline2}))
a.callbackMessageID = msg.MessageID
a.callbackChatID = m.Chat.ID
2020-01-21 20:51:19 +00:00
2020-01-22 20:51:26 +00:00
}
func (a *application) callbackHandler(cq *tbot.CallbackQuery) {
a.client.EditMessageText(a.callbackChatID, a.callbackMessageID, "Callback received.")
a.client.SendMessage(a.callbackChatID, cq.Data)
2020-01-21 19:38:02 +00:00
}