Rewriting in different framework

This commit is contained in:
Matt Burchett 2020-01-22 14:51:26 -06:00
parent c4a1519d77
commit c9706bd48a
3 changed files with 66 additions and 42 deletions

View File

@ -2,65 +2,62 @@ package main
import (
"log"
"time"
"github.com/mattburchett/go_telegram/pkg/config"
tb "gopkg.in/tucnak/telebot.v2"
"github.com/yanzay/tbot/v2"
)
func main() {
type application struct {
client *tbot.Client
callbackChatID string
callbackMessageID int
}
func main() {
cfg, err := config.GetConfig("config.json")
if err != nil {
log.Fatal("Failed to load config.")
log.Fatal("Failed to read JSON.")
}
b, err := tb.NewBot(tb.Settings{
Token: cfg.TelegramToken,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
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)
return
}
b.Handle("/ping", func(m *tb.Message) {
b.Send(m.Sender, "pong")
})
test(b)
b.Start()
}
func test(b *tb.Bot) {
var stored *tb.Message
inlineBtns := []tb.InlineButton{tb.InlineButton{Unique: "1", Text: "Ping"}, tb.InlineButton{Unique: "2", Text: "Is"}, tb.InlineButton{Unique: "3", Text: "Stupid"}}
inlineKeys := [][]tb.InlineButton{inlineBtns}
func (a *application) testHandler(m *tbot.Message) {
buttons := make([]string, 0)
buttons = append(buttons, "ping", "is", "stupid")
b.Handle("/test", func(m *tb.Message) {
msg, _ := b.Send(m.Sender, "Inline test.", &tb.ReplyMarkup{
InlineKeyboard: inlineKeys,
})
stored = msg
inline2 := make([][]tbot.InlineKeyboardButton, 0)
})
for _, btn := range inlineBtns {
type button struct {
ID int
Stuff tb.InlineButton
for _, i := range buttons {
inline2 = append(inline2, []tbot.InlineKeyboardButton{{
Text: i,
CallbackData: i,
}})
}
b.Handle(&btn, func(c *tb.Callback) {
b.Respond(c, &tb.CallbackResponse{Text: "Callback received."})
func(m *tb.Message) {
b.EditReplyMarkup(m, &tb.ReplyMarkup{ReplyKeyboardRemove: true})
b.Edit(m, "Callback received. Processing.")
}(stored)
b.Send(c.Sender, btn.Text)
})
msg, _ := a.client.SendMessage(m.Chat.ID, "Inline 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)
}

11
go.mod
View File

@ -2,4 +2,13 @@ module github.com/mattburchett/go_telegram
go 1.13
require gopkg.in/tucnak/telebot.v2 v2.0.0-20200120165535-b6c3367fed99
replace gopkg.in/tucnak/telebot.v2 => github.com/tucnak/telebot v0.0.0-20171121031619-29bd3707020c
require (
github.com/armon/go-radix v1.0.0 // indirect
github.com/mitchellh/hashstructure v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/yanzay/tbot v1.0.0
github.com/yanzay/tbot/v2 v2.1.0
gopkg.in/tucnak/telebot.v2 v2.0.0-00010101000000-000000000000
)

18
go.sum
View File

@ -1,4 +1,22 @@
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible h1:2cauKuaELYAEARXRkq2LrJ0yDDv1rW7+wrTEdVL3uaU=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/mitchellh/hashstructure v1.0.0 h1:ZkRJX1CyOoTkar7p/mLS5TZU4nJ1Rn/F8u9dGS02Q3Y=
github.com/mitchellh/hashstructure v1.0.0/go.mod h1:QjSHrPWS+BGUVBYkbTZWEnOh3G1DutKwClXU/ABz6AQ=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
github.com/tucnak/telebot v0.0.0-20171121031619-29bd3707020c h1:UJcBc6yzQVO/Xjrglu7UI0zgXb8NWd5QQAE4dqxYc8o=
github.com/tucnak/telebot v0.0.0-20171121031619-29bd3707020c/go.mod h1:TCLoYDyssqVcjhkdyYu+He6eldK40im537vXoex2LM0=
github.com/yanzay/log v0.0.0-20160419144809-87352bb23506 h1:RPrQRi67MUoUPg/3wwL9K9vKVWAB180e2WJgxC02CJE=
github.com/yanzay/log v0.0.0-20160419144809-87352bb23506/go.mod h1:x2hAcqPbZzQz79Fzr9xEe7BM73+UsZ3nBucx2u30oSk=
github.com/yanzay/tbot v1.0.0 h1:pKLHIvdiHRgU5iTfpWlgMTCWyD4vIQtAsOy7LCYMsAo=
github.com/yanzay/tbot v1.0.0/go.mod h1:rS36e4e2P56jkI0bUuZtzjBBIwMIdBBTUHfI4tgG5BM=
github.com/yanzay/tbot/v2 v2.1.0 h1:mppieSOIbzaCjp2en66Fz4unIJ57+aalQfdGbtYmaKg=
github.com/yanzay/tbot/v2 v2.1.0/go.mod h1:q0+8JblBq9tLAnKHdBIZsHwDvMS9TfO6mNfaAk1VrHg=
gopkg.in/tucnak/telebot.v2 v2.0.0-20200120165535-b6c3367fed99 h1:EMQ2hWPVLRFE/1jEruLFkhy1ACshSEg3X3PeCtGM8mA=
gopkg.in/tucnak/telebot.v2 v2.0.0-20200120165535-b6c3367fed99/go.mod h1:+//wyPtHTeW2kfyEBwB05Hqnxev7AGrsLIyylSH++KU=