61 lines
1.1 KiB
Markdown
61 lines
1.1 KiB
Markdown
|
# Command Handling
|
||
|
|
||
|
This is a simple example of changing behavior based on a provided command.
|
||
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"os"
|
||
|
|
||
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
|
||
|
if err != nil {
|
||
|
log.Panic(err)
|
||
|
}
|
||
|
|
||
|
bot.Debug = true
|
||
|
|
||
|
log.Printf("Authorized on account %s", bot.Self.UserName)
|
||
|
|
||
|
u := tgbotapi.NewUpdate(0)
|
||
|
u.Timeout = 60
|
||
|
|
||
|
updates := bot.GetUpdatesChan(u)
|
||
|
|
||
|
for update := range updates {
|
||
|
if update.Message == nil { // ignore any non-Message updates
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
if !update.Message.IsCommand() { // ignore any non-command Messages
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
// Create a new MessageConfig. We don't have text yet,
|
||
|
// so we leave it empty.
|
||
|
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
|
||
|
|
||
|
// Extract the command from the Message.
|
||
|
switch update.Message.Command() {
|
||
|
case "help":
|
||
|
msg.Text = "I understand /sayhi and /status."
|
||
|
case "sayhi":
|
||
|
msg.Text = "Hi :)"
|
||
|
case "status":
|
||
|
msg.Text = "I'm ok."
|
||
|
default:
|
||
|
msg.Text = "I don't know that command"
|
||
|
}
|
||
|
|
||
|
if _, err := bot.Send(msg); err != nil {
|
||
|
log.Panic(err)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|