diff --git a/samples/commands/main.go b/samples/commands/main.go index 10d679b..71e2f4e 100644 --- a/samples/commands/main.go +++ b/samples/commands/main.go @@ -1,6 +1,6 @@ // sample code for telegram-bot-go (handle commands), // -// last update: 2024.01.04. +// last update: 2024.04.03. package main @@ -70,11 +70,8 @@ func send(b *bot.Bot, chatID, messageID int64, message string) { if sent := b.SendMessage( chatID, message, - // option bot.OptionsSendMessage{}. - SetReplyParameters(bot.ReplyParameters{ - MessageID: messageID, - }), // show original message + SetReplyParameters(bot.NewReplyParameters(messageID)), // show original message ); !sent.Ok { log.Printf( "*** failed to send a message: %s", @@ -93,17 +90,26 @@ func react(b *bot.Bot, chatID, messageID int64, emoji string) { } } +// generate bot's name +func botName(bot *bot.User) string { + if bot != nil { + if bot.Username != nil { + return fmt.Sprintf("@%s (%s)", *bot.Username, bot.FirstName) + } else { + return bot.FirstName + } + } + + return "Unknown" +} + func main() { client := bot.NewClient(apiToken) client.Verbose = verbose // get info about this bot if me := client.GetMe(); me.Ok { - log.Printf( - "Bot information: @%s (%s)", - *me.Result.Username, - me.Result.FirstName, - ) + log.Printf("Bot information: %s", botName(me.Result)) // delete webhook (getting updates will not work when wehbook is set up) if unhooked := client.DeleteWebhook(true); unhooked.Ok { diff --git a/samples/polling/main.go b/samples/polling/main.go index 12961f3..565d2bb 100644 --- a/samples/polling/main.go +++ b/samples/polling/main.go @@ -1,6 +1,6 @@ // sample code for telegram-bot-go (get updates), // -// last update: 2024.01.04. +// last update: 2024.04.03. package main @@ -66,18 +66,20 @@ func updateHandler(b *bot.Bot, update bot.Update, err error) { bot.OptionsSendMessage{}. SetReplyParameters(bot.ReplyParameters{ MessageID: update.Message.MessageID, - }). // show original message - SetReplyMarkup(bot.ReplyKeyboardMarkup{ // show keyboards - Keyboard: [][]bot.KeyboardButton{ + }). // show original message + SetReplyMarkup(bot.NewReplyKeyboardMarkup( // show keyboards + [][]bot.KeyboardButton{ { - keyboardButton("Just a button", false, false), + bot.NewKeyboardButton("Just a button"), }, { - keyboardButton("Request contact", true, false), - keyboardButton("Request location", false, true), + bot.NewKeyboardButton("Request contact"). + SetRequestContact(true), + bot.NewKeyboardButton("Request location"). + SetRequestLocation(true), }, }, - }), + )), ); !sent.Ok { log.Printf( "*** failed to send message: %s", @@ -120,12 +122,17 @@ func updateHandler(b *bot.Bot, update bot.Update, err error) { } } -func keyboardButton(text string, contact, location bool) bot.KeyboardButton { - return bot.KeyboardButton{ - Text: text, - RequestContact: &contact, - RequestLocation: &location, +// generate bot's name +func botName(bot *bot.User) string { + if bot != nil { + if bot.Username != nil { + return fmt.Sprintf("@%s (%s)", *bot.Username, bot.FirstName) + } else { + return bot.FirstName + } } + + return "Unknown" } func main() { @@ -134,11 +141,7 @@ func main() { // get info about this bot if me := client.GetMe(); me.Ok { - log.Printf( - "Bot information: @%s (%s)", - *me.Result.Username, - me.Result.FirstName, - ) + log.Printf("Bot information: %s", botName(me.Result)) // delete webhook (getting updates will not work when wehbook is set up) if unhooked := client.DeleteWebhook(true); unhooked.Ok { diff --git a/samples/wasm/go.mod b/samples/wasm/go.mod index 9ecbf39..632657d 100644 --- a/samples/wasm/go.mod +++ b/samples/wasm/go.mod @@ -3,6 +3,6 @@ module github.com/meinside/telegram-bot-go/samples/wasm go 1.21.3 require ( - github.com/meinside/telegram-bot-go v0.10.6 + github.com/meinside/telegram-bot-go v0.10.7 github.com/meinside/wasm-helper-go v0.3.2 ) diff --git a/samples/wasm/go.sum b/samples/wasm/go.sum index 4a89068..34acce4 100644 --- a/samples/wasm/go.sum +++ b/samples/wasm/go.sum @@ -1,4 +1,4 @@ -github.com/meinside/telegram-bot-go v0.10.6 h1:kn9sDW4E6Ir9iCUI+ckq4Ob/KelS7ftJXXPbEj2iXTk= -github.com/meinside/telegram-bot-go v0.10.6/go.mod h1:i9gGJrrfhdAIElC/HCUprMmccGjMKPVq52av4n54Y2s= +github.com/meinside/telegram-bot-go v0.10.7 h1:j+NrAdqpOaYlGHa9KOHIvxGx4HLyij4Ps85Wmbh/9vc= +github.com/meinside/telegram-bot-go v0.10.7/go.mod h1:i9gGJrrfhdAIElC/HCUprMmccGjMKPVq52av4n54Y2s= github.com/meinside/wasm-helper-go v0.3.2 h1:TN7yws7Cr/cfvuEf5XnRZNr9dYmTj1NsHa++Z2NyAk4= github.com/meinside/wasm-helper-go v0.3.2/go.mod h1:+thzjZBs0c6fmJcvB8seB6ppD0Rr9Z9b05H2hHxPzkA= diff --git a/samples/wasm/main.go b/samples/wasm/main.go index 6ff1027..aaf7dd7 100644 --- a/samples/wasm/main.go +++ b/samples/wasm/main.go @@ -4,7 +4,7 @@ // // Wasm version // -// created on: 2024.01.04. +// created on: 2024.04.03. // NOTE: open related files with GOOS and GOARCH environment variables like: // `$ GOOS=js GOARCH=wasm vi __FILENAME__` @@ -130,17 +130,17 @@ func handleUpdate(b *bot.Bot, update bot.Update, err error) { if sent := b.SendMessage( update.Message.Chat.ID, message, - // options bot.OptionsSendMessage{}. - SetReplyParameters(bot.ReplyParameters{ - MessageID: update.Message.MessageID, - }). // reply to the original message - SetReplyMarkup(replyKeyboardMarkup(true, [][]bot.KeyboardButton{ // show keyboards + SetReplyParameters(bot.NewReplyParameters(update.Message.MessageID)). // reply to the original message + SetReplyMarkup(bot.NewReplyKeyboardMarkup([][]bot.KeyboardButton{ // show resized keyboards { - keyboardButton("Send contact", true, false), - keyboardButton("Send location", false, true), + bot.NewKeyboardButton("Send contact"). + SetRequestContact(true), + bot.NewKeyboardButton("Send location"). + SetRequestLocation(true), }, - })), + }). + SetResizeKeyboard(true)), ); !sent.Ok { log.Printf( "*** failed to send message: %s", @@ -156,23 +156,6 @@ func handleUpdate(b *bot.Bot, update bot.Update, err error) { } } -// generate a reply keyboard markup -func replyKeyboardMarkup(resize bool, keyboards [][]bot.KeyboardButton) bot.ReplyKeyboardMarkup { - return bot.ReplyKeyboardMarkup{ - ResizeKeyboard: &resize, - Keyboard: keyboards, - } -} - -// generate a keyboard button -func keyboardButton(text string, contact, location bool) bot.KeyboardButton { - return bot.KeyboardButton{ - Text: text, - RequestContact: &contact, - RequestLocation: &location, - } -} - func main() { // `runBot` will be exposed to js _wasmHelper.RegisterFunctions(map[string]wh.WasmFunction{ diff --git a/samples/webhook/main.go b/samples/webhook/main.go index 739dd85..2b3513d 100644 --- a/samples/webhook/main.go +++ b/samples/webhook/main.go @@ -1,6 +1,6 @@ // sample code for telegram-bot-go (receive webhooks), // -// last update: 2024.01.04. +// last update: 2024.04.03. package main @@ -69,22 +69,21 @@ func webhookHandler(b *bot.Bot, webhook bot.Update, err error) { if sent := b.SendMessage( webhook.Message.Chat.ID, message, - // option bot.OptionsSendMessage{}. - SetReplyParameters(bot.ReplyParameters{ - MessageID: webhook.Message.MessageID, - }). // show original message - SetReplyMarkup(bot.ReplyKeyboardMarkup{ // show keyboards - Keyboard: [][]bot.KeyboardButton{ + SetReplyParameters(bot.NewReplyParameters(webhook.Message.MessageID)). // show original message + SetReplyMarkup(bot.NewReplyKeyboardMarkup( // show keyboards + [][]bot.KeyboardButton{ { - keyboardButton("Just a button", false, false), + bot.NewKeyboardButton("Just a button"), }, { - keyboardButton("Request contact", true, false), - keyboardButton("Request location", false, true), + bot.NewKeyboardButton("Request contact"). + SetRequestContact(true), + bot.NewKeyboardButton("Request location"). + SetRequestLocation(true), }, }, - }), + )), ); !sent.Ok { log.Printf( "*** failed to send message: %s", @@ -127,12 +126,17 @@ func webhookHandler(b *bot.Bot, webhook bot.Update, err error) { } } -func keyboardButton(text string, contact, location bool) bot.KeyboardButton { - return bot.KeyboardButton{ - Text: text, - RequestContact: &contact, - RequestLocation: &location, +// generate bot's name +func botName(bot *bot.User) string { + if bot != nil { + if bot.Username != nil { + return fmt.Sprintf("@%s (%s)", *bot.Username, bot.FirstName) + } else { + return bot.FirstName + } } + + return "Unknown" } func main() { @@ -141,11 +145,7 @@ func main() { // get info about this bot if me := client.GetMe(); me.Ok { - log.Printf( - "Bot information: @%s (%s)", - *me.Result.Username, - me.Result.FirstName, - ) + log.Printf("Bot information: %s", botName(me.Result)) // delete webhook if unhooked := client.DeleteWebhook(true); unhooked.Ok {