Rewriting in different framework
This commit is contained in:
parent
c4a1519d77
commit
c9706bd48a
77
cmd/main.go
77
cmd/main.go
@ -2,65 +2,62 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/mattburchett/go_telegram/pkg/config"
|
"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")
|
cfg, err := config.GetConfig("config.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to load config.")
|
log.Fatal("Failed to read JSON.")
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := tb.NewBot(tb.Settings{
|
app := &application{}
|
||||||
Token: cfg.TelegramToken,
|
|
||||||
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
|
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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
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) {
|
func (a *application) testHandler(m *tbot.Message) {
|
||||||
var stored *tb.Message
|
buttons := make([]string, 0)
|
||||||
inlineBtns := []tb.InlineButton{tb.InlineButton{Unique: "1", Text: "Ping"}, tb.InlineButton{Unique: "2", Text: "Is"}, tb.InlineButton{Unique: "3", Text: "Stupid"}}
|
buttons = append(buttons, "ping", "is", "stupid")
|
||||||
inlineKeys := [][]tb.InlineButton{inlineBtns}
|
|
||||||
|
|
||||||
b.Handle("/test", func(m *tb.Message) {
|
inline2 := make([][]tbot.InlineKeyboardButton, 0)
|
||||||
msg, _ := b.Send(m.Sender, "Inline test.", &tb.ReplyMarkup{
|
|
||||||
InlineKeyboard: inlineKeys,
|
|
||||||
})
|
|
||||||
stored = msg
|
|
||||||
|
|
||||||
})
|
for _, i := range buttons {
|
||||||
|
inline2 = append(inline2, []tbot.InlineKeyboardButton{{
|
||||||
for _, btn := range inlineBtns {
|
Text: i,
|
||||||
type button struct {
|
CallbackData: i,
|
||||||
ID int
|
}})
|
||||||
Stuff tb.InlineButton
|
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Handle(&btn, func(c *tb.Callback) {
|
msg, _ := a.client.SendMessage(m.Chat.ID, "Inline test.", tbot.OptInlineKeyboardMarkup(&tbot.InlineKeyboardMarkup{InlineKeyboard: inline2}))
|
||||||
b.Respond(c, &tb.CallbackResponse{Text: "Callback received."})
|
a.callbackMessageID = msg.MessageID
|
||||||
func(m *tb.Message) {
|
a.callbackChatID = m.Chat.ID
|
||||||
b.EditReplyMarkup(m, &tb.ReplyMarkup{ReplyKeyboardRemove: true})
|
|
||||||
b.Edit(m, "Callback received. Processing.")
|
}
|
||||||
}(stored)
|
|
||||||
b.Send(c.Sender, btn.Text)
|
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
11
go.mod
@ -2,4 +2,13 @@ module github.com/mattburchett/go_telegram
|
|||||||
|
|
||||||
go 1.13
|
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
18
go.sum
@ -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 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
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 h1:EMQ2hWPVLRFE/1jEruLFkhy1ACshSEg3X3PeCtGM8mA=
|
||||||
gopkg.in/tucnak/telebot.v2 v2.0.0-20200120165535-b6c3367fed99/go.mod h1:+//wyPtHTeW2kfyEBwB05Hqnxev7AGrsLIyylSH++KU=
|
gopkg.in/tucnak/telebot.v2 v2.0.0-20200120165535-b6c3367fed99/go.mod h1:+//wyPtHTeW2kfyEBwB05Hqnxev7AGrsLIyylSH++KU=
|
||||||
|
Loading…
Reference in New Issue
Block a user