Initial work on improving docs.
This commit is contained in:
parent
2f7211a708
commit
5be25266b5
12 changed files with 674 additions and 0 deletions
60
docs/examples/command-handling.md
Normal file
60
docs/examples/command-handling.md
Normal file
|
@ -0,0 +1,60 @@
|
|||
# Command Handling
|
||||
|
||||
This is a simple example of changing behavior based on a provided command.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/go-telegram-bot-api/telegram-bot-api"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue