2020-07-27 00:22:16 +02:00
|
|
|
# Command Handling
|
|
|
|
|
|
|
|
This is a simple example of changing behavior based on a provided command.
|
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-10 22:36:15 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
2020-07-27 00:22:16 +02:00
|
|
|
|
2021-03-10 22:36:15 +01:00
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
2020-07-27 00:22:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-03-10 22:36:15 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2020-07-27 00:22:16 +02:00
|
|
|
}
|
|
|
|
```
|