Bigger and better rewrite.

This commit is contained in:
2020-05-17 01:25:29 -05:00
parent aeac9e4e26
commit 7c6b9c4a74
7 changed files with 184 additions and 55 deletions

View File

@ -2,63 +2,18 @@ package main
import (
"log"
"strings"
"github.com/mattburchett/go_telegram/pkg/config"
"github.com/yanzay/tbot/v2"
"github.com/mattburchett/go_telegram/pkg/core/config"
"github.com/mattburchett/go_telegram/pkg/service/telegram"
)
type application struct {
client *tbot.Client
callbackChatID string
callbackMessageID int
}
func main() {
cfg, err := config.GetConfig("config.json")
conf, err := config.GetConfig("config.json")
if err != nil {
log.Fatal("Failed to read JSON.")
}
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")
})
bot.HandleMessage("/test", app.testHandler)
bot.HandleCallback(app.callbackHandler)
err = bot.Start()
if err != nil {
log.Fatal(err)
}
}
func (a *application) testHandler(m *tbot.Message) {
buttons := make([]string, 0)
buttons = append(buttons, "ping", "is", "stupid")
inline2 := make([][]tbot.InlineKeyboardButton, 0)
for _, i := range buttons {
inline2 = append(inline2, []tbot.InlineKeyboardButton{{
Text: i,
CallbackData: i,
}})
}
msg, _ := a.client.SendMessage(m.Chat.ID, "Inline test. "+strings.TrimPrefix(m.Text, "/test "), tbot.OptInlineKeyboardMarkup(&tbot.InlineKeyboardMarkup{InlineKeyboard: inline2}))
a.callbackMessageID = msg.MessageID
a.callbackChatID = m.Chat.ID
}
func (a *application) callbackHandler(cq *tbot.CallbackQuery) {
a.client.EditMessageText(a.callbackChatID, a.callbackMessageID, "Callback received.")
a.client.SendMessage(a.callbackChatID, cq.Data)
tgBot := telegram.Bot{}
tgBot.Config = conf
tgBot.New(conf.Telegram.Token)
}