Skip to content

Commit fb1de2f

Browse files
authored
Merge pull request #434 from go-telegram-bot-api/develop-docs
Add improved documentation
2 parents 2c2c95a + 133755f commit fb1de2f

File tree

15 files changed

+831
-0
lines changed

15 files changed

+831
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea/
22
coverage.out
33
tmp/
4+
book/

book.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[book]
2+
authors = ["Syfaro"]
3+
language = "en"
4+
multilingual = false
5+
src = "docs"
6+
title = "Go Telegram Bot API"
7+
8+
[output.html]
9+
git-repository-url = "https://github.com/go-telegram-bot-api/telegram-bot-api"

docs/SUMMARY.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Summary
2+
3+
- [Getting Started](./getting-started/README.md)
4+
- [Library Structure](./getting-started/library-structure.md)
5+
- [Files](./getting-started/files.md)
6+
- [Important Notes](./getting-started/important-notes.md)
7+
- [Examples](./examples/README.md)
8+
- [Command Handling](./examples/command-handling.md)
9+
- [Keyboard](./examples/keyboard.md)
10+
- [Inline Keyboard](./examples/inline-keyboard.md)
11+
- [Change Log](./changelog.md)
12+
13+
# Contributing
14+
15+
- [Internals](./internals/README.md)
16+
- [Adding Endpoints](./internals/adding-endpoints.md)
17+
- [Uploading Files](./internals/uploading-files.md)

docs/changelog.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Change Log
2+
3+
## v5
4+
5+
**Work In Progress**
6+
7+
- Remove all methods that return `(APIResponse, error)`.
8+
- Use the `Request` method instead.
9+
- For more information, see [Library Structure][library-structure].
10+
- Remove all `New*Upload` and `New*Share` methods, replace with `New*`.
11+
- Use different [file types][files] to specify if upload or share.
12+
- Rename `UploadFile` to `UploadFiles`, accept `[]RequestFile` instead of a
13+
single fieldname and file.
14+
- Fix methods returning `APIResponse` and errors to always use pointers.
15+
- Update user IDs to `int64` because of Bot API changes.
16+
- Add missing Bot API features.
17+
18+
[library-structure]: ./getting-started/library-structure.md#methods
19+
[files]: ./getting-started/files.md

docs/examples/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Examples
2+
3+
With a better understanding of how the library works, let's look at some more
4+
examples showing off some of Telegram's features.

docs/examples/command-handling.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Command Handling
2+
3+
This is a simple example of changing behavior based on a provided command.
4+
5+
```go
6+
package main
7+
8+
import (
9+
"log"
10+
"os"
11+
12+
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
13+
)
14+
15+
func main() {
16+
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
17+
if err != nil {
18+
log.Panic(err)
19+
}
20+
21+
bot.Debug = true
22+
23+
log.Printf("Authorized on account %s", bot.Self.UserName)
24+
25+
u := tgbotapi.NewUpdate(0)
26+
u.Timeout = 60
27+
28+
updates := bot.GetUpdatesChan(u)
29+
30+
for update := range updates {
31+
if update.Message == nil { // ignore any non-Message updates
32+
continue
33+
}
34+
35+
if !update.Message.IsCommand() { // ignore any non-command Messages
36+
continue
37+
}
38+
39+
// Create a new MessageConfig. We don't have text yet,
40+
// so we leave it empty.
41+
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
42+
43+
// Extract the command from the Message.
44+
switch update.Message.Command() {
45+
case "help":
46+
msg.Text = "I understand /sayhi and /status."
47+
case "sayhi":
48+
msg.Text = "Hi :)"
49+
case "status":
50+
msg.Text = "I'm ok."
51+
default:
52+
msg.Text = "I don't know that command"
53+
}
54+
55+
if _, err := bot.Send(msg); err != nil {
56+
log.Panic(err)
57+
}
58+
}
59+
}
60+
```

docs/examples/inline-keyboard.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Inline Keyboard
2+
3+
This bot waits for you to send it the message "open" before sending you an
4+
inline keyboard containing a URL and some numbers. When a number is clicked, it
5+
sends you a message with your selected number.
6+
7+
```go
8+
package main
9+
10+
import (
11+
"log"
12+
"os"
13+
14+
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
15+
)
16+
17+
var numericKeyboard = tgbotapi.NewInlineKeyboardMarkup(
18+
tgbotapi.NewInlineKeyboardRow(
19+
tgbotapi.NewInlineKeyboardButtonURL("1.com", "http://1.com"),
20+
tgbotapi.NewInlineKeyboardButtonData("2", "2"),
21+
tgbotapi.NewInlineKeyboardButtonData("3", "3"),
22+
),
23+
tgbotapi.NewInlineKeyboardRow(
24+
tgbotapi.NewInlineKeyboardButtonData("4", "4"),
25+
tgbotapi.NewInlineKeyboardButtonData("5", "5"),
26+
tgbotapi.NewInlineKeyboardButtonData("6", "6"),
27+
),
28+
)
29+
30+
func main() {
31+
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
32+
if err != nil {
33+
log.Panic(err)
34+
}
35+
36+
bot.Debug = true
37+
38+
log.Printf("Authorized on account %s", bot.Self.UserName)
39+
40+
u := tgbotapi.NewUpdate(0)
41+
u.Timeout = 60
42+
43+
updates := bot.GetUpdatesChan(u)
44+
45+
// Loop through each update.
46+
for update := range updates {
47+
// Check if we've gotten a message update.
48+
if update.Message != nil {
49+
// Construct a new message from the given chat ID and containing
50+
// the text that we received.
51+
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
52+
53+
// If the message was open, add a copy of our numeric keyboard.
54+
switch update.Message.Text {
55+
case "open":
56+
msg.ReplyMarkup = numericKeyboard
57+
58+
}
59+
60+
// Send the message.
61+
if _, err = bot.Send(msg); err != nil {
62+
panic(err)
63+
}
64+
} else if update.CallbackQuery != nil {
65+
// Respond to the callback query, telling Telegram to show the user
66+
// a message with the data received.
67+
callback := tgbotapi.NewCallback(update.CallbackQuery.ID, update.CallbackQuery.Data)
68+
if _, err := bot.Request(callback); err != nil {
69+
panic(err)
70+
}
71+
72+
// And finally, send a message containing the data received.
73+
msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, update.CallbackQuery.Data)
74+
if _, err := bot.Send(msg); err != nil {
75+
panic(err)
76+
}
77+
}
78+
}
79+
}
80+
```

docs/examples/keyboard.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Keyboard
2+
3+
This bot shows a numeric keyboard when you send a "open" message and hides it
4+
when you send "close" message.
5+
6+
```go
7+
package main
8+
9+
import (
10+
"log"
11+
"os"
12+
13+
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
14+
)
15+
16+
var numericKeyboard = tgbotapi.NewReplyKeyboard(
17+
tgbotapi.NewKeyboardButtonRow(
18+
tgbotapi.NewKeyboardButton("1"),
19+
tgbotapi.NewKeyboardButton("2"),
20+
tgbotapi.NewKeyboardButton("3"),
21+
),
22+
tgbotapi.NewKeyboardButtonRow(
23+
tgbotapi.NewKeyboardButton("4"),
24+
tgbotapi.NewKeyboardButton("5"),
25+
tgbotapi.NewKeyboardButton("6"),
26+
),
27+
)
28+
29+
func main() {
30+
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
31+
if err != nil {
32+
log.Panic(err)
33+
}
34+
35+
bot.Debug = true
36+
37+
log.Printf("Authorized on account %s", bot.Self.UserName)
38+
39+
u := tgbotapi.NewUpdate(0)
40+
u.Timeout = 60
41+
42+
updates := bot.GetUpdatesChan(u)
43+
44+
for update := range updates {
45+
if update.Message == nil { // ignore non-Message updates
46+
continue
47+
}
48+
49+
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
50+
51+
switch update.Message.Text {
52+
case "open":
53+
msg.ReplyMarkup = numericKeyboard
54+
case "close":
55+
msg.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true)
56+
}
57+
58+
if _, err := bot.Send(msg); err != nil {
59+
log.Panic(err)
60+
}
61+
}
62+
}
63+
```

docs/getting-started/README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Getting Started
2+
3+
This library is designed as a simple wrapper around the Telegram Bot API.
4+
It's encouraged to read [Telegram's docs][telegram-docs] first to get an
5+
understanding of what Bots are capable of doing. They also provide some good
6+
approaches to solve common problems.
7+
8+
[telegram-docs]: https://core.telegram.org/bots
9+
10+
## Installing
11+
12+
```bash
13+
go get -u github.com/go-telegram-bot-api/telegram-bot-api/v5@develop
14+
```
15+
16+
It's currently suggested to use the develop branch. While there may be breaking
17+
changes, it has a number of features not yet available on master.
18+
19+
## A Simple Bot
20+
21+
To walk through the basics, let's create a simple echo bot that replies to your
22+
messages repeating what you said. Make sure you get an API token from
23+
[@Botfather][botfather] before continuing.
24+
25+
Let's start by constructing a new [BotAPI][bot-api-docs].
26+
27+
[botfather]: https://t.me/Botfather
28+
[bot-api-docs]: https://pkg.go.dev/github.com/go-telegram-bot-api/telegram-bot-api/v5?tab=doc#BotAPI
29+
30+
```go
31+
package main
32+
33+
import (
34+
"os"
35+
36+
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
37+
)
38+
39+
func main() {
40+
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_APITOKEN"))
41+
if err != nil {
42+
panic(err)
43+
}
44+
45+
bot.Debug = true
46+
}
47+
```
48+
49+
Instead of typing the API token directly into the file, we're using
50+
environment variables. This makes it easy to configure our Bot to use the right
51+
account and prevents us from leaking our real token into the world. Anyone with
52+
your token can send and receive messages from your Bot!
53+
54+
We've also set `bot.Debug = true` in order to get more information about the
55+
requests being sent to Telegram. If you run the example above, you'll see
56+
information about a request to the [`getMe`][get-me] endpoint. The library
57+
automatically calls this to ensure your token is working as expected. It also
58+
fills in the `Self` field in your `BotAPI` struct with information about the
59+
Bot.
60+
61+
Now that we've connected to Telegram, let's start getting updates and doing
62+
things. We can add this code in right after the line enabling debug mode.
63+
64+
[get-me]: https://core.telegram.org/bots/api#getme
65+
66+
```go
67+
// Create a new UpdateConfig struct with an offset of 0. Offsets are used
68+
// to make sure Telegram knows we've handled previous values and we don't
69+
// need them repeated.
70+
updateConfig := tgbotapi.NewUpdate(0)
71+
72+
// Tell Telegram we should wait up to 30 seconds on each request for an
73+
// update. This way we can get information just as quickly as making many
74+
// frequent requests without having to send nearly as many.
75+
updateConfig.Timeout = 30
76+
77+
// Start polling Telegram for updates.
78+
updates := bot.GetUpdatesChan(updateConfig)
79+
80+
// Let's go through each update that we're getting from Telegram.
81+
for update := range updates {
82+
// Telegram can send many types of updates depending on what your Bot
83+
// is up to. We only want to look at messages for now, so we can
84+
// discard any other updates.
85+
if update.Message == nil {
86+
continue
87+
}
88+
89+
// Now that we know we've gotten a new message, we can construct a
90+
// reply! We'll take the Chat ID and Text from the incoming message
91+
// and use it to create a new message.
92+
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
93+
// We'll also say that this message is a reply to the previous message.
94+
// For any other specifications than Chat ID or Text, you'll need to
95+
// set fields on the `MessageConfig`.
96+
msg.ReplyToMessageID = update.Message.MessageID
97+
98+
// Okay, we're sending our message off! We don't care about the message
99+
// we just sent, so we'll discard it.
100+
if _, err := bot.Send(msg); err != nil {
101+
// Note that panics are a bad way to handle errors. Telegram can
102+
// have service outages or network errors, you should retry sending
103+
// messages or more gracefully handle failures.
104+
panic(err)
105+
}
106+
}
107+
```
108+
109+
Congradulations! You've made your very own bot!
110+
111+
Now that you've got some of the basics down, we can start talking about how the
112+
library is structured and more advanced features.

0 commit comments

Comments
 (0)