Small doc fixes.

bot-api-6.1
Syfaro 2021-03-10 16:47:14 -05:00
parent a36ca53925
commit 80a1cbdb44
1 changed files with 43 additions and 43 deletions

View File

@ -10,7 +10,7 @@ approaches to solve common problems.
## Installing ## Installing
```bash ```bash
go get -u github.com/go-telegram-bot-api/telegram-bot-api@develop go get -u github.com/go-telegram-bot-api/telegram-bot-api/v5@develop
``` ```
It's currently suggested to use the develop branch. While there may be breaking It's currently suggested to use the develop branch. While there may be breaking
@ -31,18 +31,18 @@ Let's start by constructing a new [BotAPI][bot-api-docs].
package main package main
import ( import (
"os" "os"
"github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
func main() { func main() {
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN")) bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
if err != nil { if err != nil {
panic(err) panic(err)
} }
bot.Debug = true bot.Debug = true
} }
``` ```
@ -64,46 +64,46 @@ things. We can add this code in right after the line enabling debug mode.
[get-me]: https://core.telegram.org/bots/api#getme [get-me]: https://core.telegram.org/bots/api#getme
```go ```go
// Create a new UpdateConfig struct with an offset of 0. Offsets are used // Create a new UpdateConfig struct with an offset of 0. Offsets are used
// to make sure Telegram knows we've handled previous values and we don't // to make sure Telegram knows we've handled previous values and we don't
// need them repeated. // need them repeated.
updateConfig := tgbotapi.NewUpdate(0) updateConfig := tgbotapi.NewUpdate(0)
// Tell Telegram we should wait up to 30 seconds on each request for an // Tell Telegram we should wait up to 30 seconds on each request for an
// update. This way we can get information just as quickly as making many // update. This way we can get information just as quickly as making many
// frequent requests without having to send nearly as many. // frequent requests without having to send nearly as many.
updateConfig.Timeout = 30 updateConfig.Timeout = 30
// Start polling Telegram for updates. // Start polling Telegram for updates.
updates := bot.GetUpdatesChan(updateConfig) updates := bot.GetUpdatesChan(updateConfig)
// Let's go through each update that we're getting from Telegram. // Let's go through each update that we're getting from Telegram.
for update := range updates { for update := range updates {
// Telegram can send many types of updates depending on what your Bot // Telegram can send many types of updates depending on what your Bot
// is up to. We only want to look at messages for now, so we can // is up to. We only want to look at messages for now, so we can
// discard any other updates. // discard any other updates.
if update.Message == nil { if update.Message == nil {
continue continue
} }
// Now that we know we've gotten a new message, we can construct a // Now that we know we've gotten a new message, we can construct a
// reply! We'll take the Chat ID and Text from the incoming message // reply! We'll take the Chat ID and Text from the incoming message
// and use it to create a new message. // and use it to create a new message.
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text) msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
// We'll also say that this message is a reply to the previous message. // We'll also say that this message is a reply to the previous message.
// For any other specifications than Chat ID or Text, you'll need to // For any other specifications than Chat ID or Text, you'll need to
// set fields on the `MessageConfig`. // set fields on the `MessageConfig`.
msg.ReplyToMessageID = update.Message.MessageID msg.ReplyToMessageID = update.Message.MessageID
// Okay, we're sending our message off! We don't care about the message // Okay, we're sending our message off! We don't care about the message
// we just sent, so we'll discard it. // we just sent, so we'll discard it.
if _, err := bot.Send(msg); err != nil { if _, err := bot.Send(msg); err != nil {
// Note that panics are a bad way to handle errors. Telegram can // Note that panics are a bad way to handle errors. Telegram can
// have service outages or network errors, you should retry sending // have service outages or network errors, you should retry sending
// messages or more gracefully handle failures. // messages or more gracefully handle failures.
panic(err) panic(err)
} }
} }
``` ```
Congradulations! You've made your very own bot! Congradulations! You've made your very own bot!