Skip to content

Commit

Permalink
Edit sample codes
Browse files Browse the repository at this point in the history
  • Loading branch information
meinside committed Apr 3, 2024
1 parent c09b1fa commit b30b174
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 78 deletions.
26 changes: 16 additions & 10 deletions samples/commands/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// sample code for telegram-bot-go (handle commands),
//
// last update: 2024.01.04.
// last update: 2024.04.03.

package main

Expand Down Expand Up @@ -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",
Expand All @@ -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 {
Expand Down
39 changes: 21 additions & 18 deletions samples/polling/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// sample code for telegram-bot-go (get updates),
//
// last update: 2024.01.04.
// last update: 2024.04.03.

package main

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion samples/wasm/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions samples/wasm/go.sum
Original file line number Diff line number Diff line change
@@ -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=
35 changes: 9 additions & 26 deletions samples/wasm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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__`
Expand Down Expand Up @@ -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",
Expand All @@ -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{
Expand Down
42 changes: 21 additions & 21 deletions samples/webhook/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// sample code for telegram-bot-go (receive webhooks),
//
// last update: 2024.01.04.
// last update: 2024.04.03.

package main

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down

0 comments on commit b30b174

Please sign in to comment.