2015-06-25 19:43:19 +00:00
|
|
|
|
# Telebot
|
2017-11-30 09:01:52 +00:00
|
|
|
|
>"I never knew creating Telegram bots could be so _sexy_!"
|
2015-06-25 19:43:19 +00:00
|
|
|
|
|
2017-11-21 12:55:46 +00:00
|
|
|
|
[![GoDoc](https://godoc.org/gopkg.in/tucnak/telebot.v2?status.svg)](https://godoc.org/gopkg.in/tucnak/telebot.v2)
|
|
|
|
|
[![Travis](https://travis-ci.org/tucnak/telebot.svg?branch=v2)](https://travis-ci.org/tucnak/telebot)
|
2020-04-27 19:35:16 +00:00
|
|
|
|
[![codecov.io](https://codecov.io/gh/tucnak/telebot/coverage.svg?branch=develop)](https://codecov.io/gh/tucnak/telebot)
|
2021-01-23 13:30:44 +00:00
|
|
|
|
[![Discuss on Telegram](https://img.shields.io/badge/telegram-discuss-0088cc.svg)](https://t.me/go_telebot)
|
2017-11-17 06:41:48 +00:00
|
|
|
|
|
2017-11-21 02:49:37 +00:00
|
|
|
|
```bash
|
2017-11-25 14:22:13 +00:00
|
|
|
|
go get -u gopkg.in/tucnak/telebot.v2
|
2017-11-21 02:49:37 +00:00
|
|
|
|
```
|
2016-09-24 11:54:43 +00:00
|
|
|
|
|
2017-11-28 22:27:23 +00:00
|
|
|
|
* [Overview](#overview)
|
|
|
|
|
* [Getting Started](#getting-started)
|
|
|
|
|
- [Poller](#poller)
|
|
|
|
|
- [Commands](#commands)
|
|
|
|
|
- [Files](#files)
|
|
|
|
|
- [Sendable](#sendable)
|
|
|
|
|
- [Editable](#editable)
|
|
|
|
|
- [Keyboards](#keyboards)
|
|
|
|
|
- [Inline mode](#inline-mode)
|
|
|
|
|
* [Contributing](#contributing)
|
|
|
|
|
* [Donate](#donate)
|
|
|
|
|
* [License](#license)
|
|
|
|
|
|
|
|
|
|
# Overview
|
2017-12-02 11:25:09 +00:00
|
|
|
|
Telebot is a bot framework for [Telegram Bot API](https://core.telegram.org/bots/api).
|
2017-11-24 13:26:11 +00:00
|
|
|
|
This package provides the best of its kind API for command routing, inline query requests and keyboards, as well
|
|
|
|
|
as callbacks. Actually, I went a couple steps further, so instead of making a 1:1 API wrapper I chose to focus on
|
2017-11-28 22:27:23 +00:00
|
|
|
|
the beauty of API and performance. Some of the strong sides of telebot are:
|
2016-09-24 11:54:43 +00:00
|
|
|
|
|
2017-11-29 20:33:50 +00:00
|
|
|
|
* Real concise API
|
2017-11-28 22:27:23 +00:00
|
|
|
|
* Command routing
|
|
|
|
|
* Middleware
|
|
|
|
|
* Transparent File API
|
|
|
|
|
* Effortless bot callbacks
|
|
|
|
|
|
|
|
|
|
All the methods of telebot API are _extremely_ easy to memorize and get used to. Also, consider Telebot a
|
|
|
|
|
highload-ready solution. I'll test and benchmark the most popular actions and if necessary, optimize
|
|
|
|
|
against them without sacrificing API quality.
|
|
|
|
|
|
|
|
|
|
# Getting Started
|
|
|
|
|
Let's take a look at the minimal telebot setup:
|
2015-06-27 11:30:35 +00:00
|
|
|
|
```go
|
2017-11-28 22:27:23 +00:00
|
|
|
|
package main
|
|
|
|
|
|
2015-06-27 11:30:35 +00:00
|
|
|
|
import (
|
2020-05-22 11:52:56 +00:00
|
|
|
|
"log"
|
|
|
|
|
"time"
|
2017-11-28 22:27:23 +00:00
|
|
|
|
|
2020-05-22 11:52:56 +00:00
|
|
|
|
tb "gopkg.in/tucnak/telebot.v2"
|
2015-06-27 11:30:35 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2017-11-28 22:27:23 +00:00
|
|
|
|
b, err := tb.NewBot(tb.Settings{
|
2020-04-26 18:48:20 +00:00
|
|
|
|
// You can also set custom API URL.
|
|
|
|
|
// If field is empty it equals to "https://api.telegram.org".
|
2018-12-12 22:45:03 +00:00
|
|
|
|
URL: "http://195.129.111.17:8012",
|
2020-04-26 18:48:20 +00:00
|
|
|
|
|
|
|
|
|
Token: "TOKEN_HERE",
|
2017-11-28 22:27:23 +00:00
|
|
|
|
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
|
|
|
|
|
})
|
2017-11-21 02:49:37 +00:00
|
|
|
|
|
2017-11-28 22:27:23 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-11-21 02:49:37 +00:00
|
|
|
|
|
2017-11-28 22:27:23 +00:00
|
|
|
|
b.Handle("/hello", func(m *tb.Message) {
|
2020-04-26 18:48:20 +00:00
|
|
|
|
b.Send(m.Sender, "Hello World!")
|
2017-11-28 22:27:23 +00:00
|
|
|
|
})
|
2017-11-21 02:49:37 +00:00
|
|
|
|
|
2017-11-28 22:27:23 +00:00
|
|
|
|
b.Start()
|
2015-06-27 11:30:35 +00:00
|
|
|
|
}
|
2017-11-28 22:27:23 +00:00
|
|
|
|
|
2015-06-27 11:30:35 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-03-28 23:35:23 +00:00
|
|
|
|
Simple, innit? Telebot's routing system takes care of delivering updates
|
2017-11-24 13:26:11 +00:00
|
|
|
|
to their endpoints, so in order to get to handle any meaningful event,
|
2017-11-28 22:27:23 +00:00
|
|
|
|
all you got to do is just plug your function to one of the Telebot-provided
|
|
|
|
|
endpoints. You can find the full list
|
|
|
|
|
[here](https://godoc.org/gopkg.in/tucnak/telebot.v2#pkg-constants).
|
2016-06-26 14:05:37 +00:00
|
|
|
|
|
2016-01-22 11:38:45 +00:00
|
|
|
|
```go
|
2017-11-21 12:55:46 +00:00
|
|
|
|
b, _ := tb.NewBot(settings)
|
2016-01-22 11:38:45 +00:00
|
|
|
|
|
2017-11-29 20:33:50 +00:00
|
|
|
|
b.Handle(tb.OnText, func(m *tb.Message) {
|
2017-11-28 22:27:23 +00:00
|
|
|
|
// all the text messages that weren't
|
|
|
|
|
// captured by existing handlers
|
2018-01-08 16:41:00 +00:00
|
|
|
|
})
|
2017-11-24 15:24:07 +00:00
|
|
|
|
|
2017-11-29 20:33:50 +00:00
|
|
|
|
b.Handle(tb.OnPhoto, func(m *tb.Message) {
|
2017-11-28 22:27:23 +00:00
|
|
|
|
// photos only
|
2018-01-08 16:41:00 +00:00
|
|
|
|
})
|
2017-11-24 15:24:07 +00:00
|
|
|
|
|
2017-11-29 20:33:50 +00:00
|
|
|
|
b.Handle(tb.OnChannelPost, func (m *tb.Message) {
|
2017-11-28 22:27:23 +00:00
|
|
|
|
// channel posts only
|
2017-11-21 12:55:46 +00:00
|
|
|
|
})
|
2016-01-22 11:38:45 +00:00
|
|
|
|
|
2020-05-10 22:55:56 +00:00
|
|
|
|
b.Handle(tb.OnQuery, func (q *tb.Query) {
|
2017-11-28 22:27:23 +00:00
|
|
|
|
// incoming inline queries
|
2017-11-21 12:55:46 +00:00
|
|
|
|
})
|
2016-01-22 11:38:45 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-03-28 23:35:23 +00:00
|
|
|
|
There's dozens of supported endpoints (see package consts). Let me know
|
2017-11-21 13:26:59 +00:00
|
|
|
|
if you'd like to see some endpoint or endpoint idea implemented. This system
|
2018-02-11 09:16:17 +00:00
|
|
|
|
is completely extensible, so I can introduce them without breaking
|
2020-05-21 09:02:35 +00:00
|
|
|
|
backwards-compatibility.
|
2017-11-21 13:26:59 +00:00
|
|
|
|
|
2017-11-28 22:27:23 +00:00
|
|
|
|
## Poller
|
|
|
|
|
Telebot doesn't really care how you provide it with incoming updates, as long
|
2020-05-01 14:56:51 +00:00
|
|
|
|
as you set it up with a Poller, or call ProcessUpdate for each update (see
|
|
|
|
|
[examples/awslambdaechobot](examples/awslambdaechobot)):
|
|
|
|
|
|
2017-11-28 22:27:23 +00:00
|
|
|
|
```go
|
|
|
|
|
// Poller is a provider of Updates.
|
|
|
|
|
//
|
|
|
|
|
// All pollers must implement Poll(), which accepts bot
|
|
|
|
|
// pointer and subscription channel and start polling
|
|
|
|
|
// synchronously straight away.
|
|
|
|
|
type Poller interface {
|
|
|
|
|
// Poll is supposed to take the bot object
|
|
|
|
|
// subscription channel and start polling
|
|
|
|
|
// for Updates immediately.
|
|
|
|
|
//
|
|
|
|
|
// Poller must listen for stop constantly and close
|
|
|
|
|
// it as soon as it's done polling.
|
|
|
|
|
Poll(b *Bot, updates chan Update, stop chan struct{})
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-02-09 12:31:23 +00:00
|
|
|
|
Telegram Bot API supports long polling and webhook integration. Poller means you
|
|
|
|
|
can plug telebot into whatever existing bot infrastructure (load balancers?) you
|
|
|
|
|
need, if you need to. Another great thing about pollers is that you can chain
|
|
|
|
|
them, making some sort of middleware:
|
2017-11-28 22:27:23 +00:00
|
|
|
|
```go
|
|
|
|
|
poller := &tb.LongPoller{Timeout: 15 * time.Second}
|
|
|
|
|
spamProtected := tb.NewMiddlewarePoller(poller, func(upd *tb.Update) bool {
|
|
|
|
|
if upd.Message == nil {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.Contains(upd.Message.Text, "spam") {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
bot, _ := tb.NewBot(tb.Settings{
|
|
|
|
|
// ...
|
2017-11-29 20:33:50 +00:00
|
|
|
|
Poller: spamProtected,
|
2017-11-28 22:27:23 +00:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// graceful shutdown
|
2020-04-26 18:48:20 +00:00
|
|
|
|
time.AfterFunc(N * time.Second, b.Stop)
|
2017-11-28 22:27:23 +00:00
|
|
|
|
|
2020-04-26 18:48:20 +00:00
|
|
|
|
// blocks until shutdown
|
|
|
|
|
bot.Start()
|
2017-11-28 22:27:23 +00:00
|
|
|
|
|
|
|
|
|
fmt.Println(poller.LastUpdateID) // 134237
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Commands
|
|
|
|
|
When handling commands, Telebot supports both direct (`/command`) and group-like
|
|
|
|
|
syntax (`/command@botname`) and will never deliver messages addressed to some
|
|
|
|
|
other bot, even if [privacy mode](https://core.telegram.org/bots#privacy-mode) is off.
|
|
|
|
|
For simplified deep-linking, telebot also extracts payload:
|
|
|
|
|
```go
|
|
|
|
|
// Command: /start <PAYLOAD>
|
|
|
|
|
b.Handle("/start", func(m *tb.Message) {
|
|
|
|
|
if !m.Private() {
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-11-29 20:33:50 +00:00
|
|
|
|
|
2017-11-28 22:27:23 +00:00
|
|
|
|
fmt.Println(m.Payload) // <PAYLOAD>
|
|
|
|
|
})
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Files
|
|
|
|
|
>Telegram allows files up to 20 MB in size.
|
|
|
|
|
|
|
|
|
|
Telebot allows to both upload (from disk / by URL) and download (from Telegram)
|
|
|
|
|
and files in bot's scope. Also, sending any kind of media with a File created
|
|
|
|
|
from disk will upload the file to Telegram automatically:
|
|
|
|
|
```go
|
|
|
|
|
a := &tb.Audio{File: tb.FromDisk("file.ogg")}
|
|
|
|
|
|
|
|
|
|
fmt.Println(a.OnDisk()) // true
|
|
|
|
|
fmt.Println(a.InCloud()) // false
|
|
|
|
|
|
|
|
|
|
// Will upload the file from disk and send it to recipient
|
|
|
|
|
bot.Send(recipient, a)
|
|
|
|
|
|
|
|
|
|
// Next time you'll be sending this very *Audio, Telebot won't
|
|
|
|
|
// re-upload the same file but rather utilize its Telegram FileID
|
|
|
|
|
bot.Send(otherRecipient, a)
|
|
|
|
|
|
|
|
|
|
fmt.Println(a.OnDisk()) // true
|
|
|
|
|
fmt.Println(a.InCloud()) // true
|
|
|
|
|
fmt.Println(a.FileID) // <telegram file id: ABC-DEF1234ghIkl-zyx57W2v1u123ew11>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
You might want to save certain `File`s in order to avoid re-uploading. Feel free
|
|
|
|
|
to marshal them into whatever format, `File` only contain public fields, so no
|
|
|
|
|
data will ever be lost.
|
|
|
|
|
|
|
|
|
|
## Sendable
|
2020-03-28 23:35:23 +00:00
|
|
|
|
Send is undoubtedly the most important method in Telebot. `Send()` accepts a
|
|
|
|
|
`Recipient` (could be user, group or a channel) and a `Sendable`. Other types other than
|
|
|
|
|
the telebot-provided media types (`Photo`, `Audio`, `Video`, etc.) are `Sendable`.
|
|
|
|
|
If you create composite types of your own, and they satisfy the `Sendable` interface,
|
2017-11-28 22:27:23 +00:00
|
|
|
|
Telebot will be able to send them out.
|
2017-11-21 13:26:59 +00:00
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
// Sendable is any object that can send itself.
|
|
|
|
|
//
|
|
|
|
|
// This is pretty cool, since it lets bots implement
|
2020-03-28 23:35:23 +00:00
|
|
|
|
// custom Sendables for complex kinds of media or
|
2017-11-21 13:26:59 +00:00
|
|
|
|
// chat objects spanning across multiple messages.
|
|
|
|
|
type Sendable interface {
|
2020-05-22 11:52:56 +00:00
|
|
|
|
Send(*Bot, Recipient, *SendOptions) (*Message, error)
|
2017-11-21 13:26:59 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2017-11-28 22:27:23 +00:00
|
|
|
|
The only type at the time that doesn't fit `Send()` is `Album` and there is a reason
|
|
|
|
|
for that. Albums were added not so long ago, so they are slightly quirky for backwards
|
|
|
|
|
compatibilities sake. In fact, an `Album` can be sent, but never received. Instead,
|
|
|
|
|
Telegram returns a `[]Message`, one for each media object in the album:
|
|
|
|
|
```go
|
|
|
|
|
p := &tb.Photo{File: tb.FromDisk("chicken.jpg")}
|
|
|
|
|
v := &tb.Video{File: tb.FromURL("http://video.mp4")}
|
|
|
|
|
|
|
|
|
|
msgs, err := b.SendAlbum(user, tb.Album{p, v})
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Send options
|
|
|
|
|
Send options are objects and flags you can pass to `Send()`, `Edit()` and friends
|
|
|
|
|
as optional arguments (following the recipient and the text/media). The most
|
|
|
|
|
important one is called `SendOptions`, it lets you control _all_ the properties of
|
|
|
|
|
the message supported by Telegram. The only drawback is that it's rather
|
|
|
|
|
inconvenient to use at times, so `Send()` supports multiple shorthands:
|
|
|
|
|
```go
|
|
|
|
|
// regular send options
|
|
|
|
|
b.Send(user, "text", &tb.SendOptions{
|
|
|
|
|
// ...
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// ReplyMarkup is a part of SendOptions,
|
|
|
|
|
// but often it's the only option you need
|
|
|
|
|
b.Send(user, "text", &tb.ReplyMarkup{
|
|
|
|
|
// ...
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// flags: no notification && no web link preview
|
|
|
|
|
b.Send(user, "text", tb.Silent, tb.NoPreview)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Full list of supported option-flags you can find
|
|
|
|
|
[here](https://github.com/tucnak/telebot/blob/v2/options.go#L9).
|
|
|
|
|
|
|
|
|
|
## Editable
|
2017-11-21 13:26:59 +00:00
|
|
|
|
If you want to edit some existing message, you don't really need to store the
|
2017-12-02 11:25:09 +00:00
|
|
|
|
original `*Message` object. In fact, upon edit, Telegram only requires `chat_id`
|
|
|
|
|
and `message_id`. So you don't really need the Message as the whole. Also you
|
|
|
|
|
might want to store references to certain messages in the database, so I thought
|
|
|
|
|
it made sense for *any* Go struct to be editable as a Telegram message, to implement
|
|
|
|
|
`Editable`:
|
2017-11-21 13:26:59 +00:00
|
|
|
|
```go
|
|
|
|
|
// Editable is an interface for all objects that
|
|
|
|
|
// provide "message signature", a pair of 32-bit
|
|
|
|
|
// message ID and 64-bit chat ID, both required
|
|
|
|
|
// for edit operations.
|
|
|
|
|
//
|
|
|
|
|
// Use case: DB model struct for messages to-be
|
2020-05-21 09:02:35 +00:00
|
|
|
|
// edited with, say two columns: msg_id,chat_id
|
2017-11-21 13:26:59 +00:00
|
|
|
|
// could easily implement MessageSig() making
|
|
|
|
|
// instances of stored messages editable.
|
|
|
|
|
type Editable interface {
|
|
|
|
|
// MessageSig is a "message signature".
|
|
|
|
|
//
|
|
|
|
|
// For inline messages, return chatID = 0.
|
|
|
|
|
MessageSig() (messageID int, chatID int64)
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2017-12-02 11:25:09 +00:00
|
|
|
|
For example, `Message` type is Editable. Here is the implementation of `StoredMessage`
|
2017-11-24 13:26:11 +00:00
|
|
|
|
type, provided by telebot:
|
2017-11-21 13:26:59 +00:00
|
|
|
|
```go
|
|
|
|
|
// StoredMessage is an example struct suitable for being
|
|
|
|
|
// stored in the database as-is or being embedded into
|
|
|
|
|
// a larger struct, which is often the case (you might
|
|
|
|
|
// want to store some metadata alongside, or might not.)
|
|
|
|
|
type StoredMessage struct {
|
|
|
|
|
MessageID int `sql:"message_id" json:"message_id"`
|
|
|
|
|
ChatID int64 `sql:"chat_id" json:"chat_id"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (x StoredMessage) MessageSig() (int, int64) {
|
|
|
|
|
return x.MessageID, x.ChatID
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2017-11-24 13:26:11 +00:00
|
|
|
|
Why bother at all? Well, it allows you to do things like this:
|
2017-11-21 13:26:59 +00:00
|
|
|
|
```go
|
|
|
|
|
// just two integer columns in the database
|
2017-11-29 20:33:50 +00:00
|
|
|
|
var msgs []tb.StoredMessage
|
2017-11-21 13:26:59 +00:00
|
|
|
|
db.Find(&msgs) // gorm syntax
|
|
|
|
|
|
|
|
|
|
for _, msg := range msgs {
|
2020-04-26 18:48:20 +00:00
|
|
|
|
bot.Edit(&msg, "Updated text")
|
2017-11-28 22:27:23 +00:00
|
|
|
|
// or
|
|
|
|
|
bot.Delete(&msg)
|
2017-11-21 13:26:59 +00:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
I find it incredibly neat. Worth noting, at this point of time there exists
|
|
|
|
|
another method in the Edit family, `EditCaption()` which is of a pretty
|
2017-11-28 22:27:23 +00:00
|
|
|
|
rare use, so I didn't bother including it to `Edit()`, just like I did with
|
|
|
|
|
`SendAlbum()` as it would inevitably lead to unnecessary complications.
|
2017-11-21 13:26:59 +00:00
|
|
|
|
```go
|
|
|
|
|
var m *Message
|
|
|
|
|
|
|
|
|
|
// change caption of a photo, audio, etc.
|
|
|
|
|
bot.EditCaption(m, "new caption")
|
|
|
|
|
```
|
2017-11-21 12:55:46 +00:00
|
|
|
|
|
2017-11-28 22:27:23 +00:00
|
|
|
|
## Keyboards
|
|
|
|
|
Telebot supports both kinds of keyboards Telegram provides: reply and inline
|
2020-05-22 11:52:56 +00:00
|
|
|
|
keyboards. Any button can also act as an endpoints for `Handle()`.
|
|
|
|
|
|
|
|
|
|
In `v2.2` we're introducing a little more convenient way in building keyboards.
|
|
|
|
|
The main goal is to avoid a lot of boilerplate and to make code clearer.
|
2016-11-10 19:31:32 +00:00
|
|
|
|
|
2015-07-03 16:56:35 +00:00
|
|
|
|
```go
|
2017-11-28 22:27:23 +00:00
|
|
|
|
func main() {
|
|
|
|
|
b, _ := tb.NewBot(tb.Settings{...})
|
|
|
|
|
|
2020-05-22 11:52:56 +00:00
|
|
|
|
var (
|
|
|
|
|
// Universal markup builders.
|
|
|
|
|
menu = &ReplyMarkup{ResizeReplyKeyboard: true}
|
|
|
|
|
selector = &ReplyMarkup{}
|
|
|
|
|
|
|
|
|
|
// Reply buttons.
|
|
|
|
|
btnHelp = menu.Text("ℹ Help")
|
|
|
|
|
btnSettings = menu.Text("⚙ Settings")
|
|
|
|
|
|
|
|
|
|
// Inline buttons.
|
|
|
|
|
//
|
|
|
|
|
// Pressing it will cause the client to
|
|
|
|
|
// send the bot a callback.
|
|
|
|
|
//
|
|
|
|
|
// Make sure Unique stays unique as per button kind,
|
|
|
|
|
// as it has to be for callback routing to work.
|
|
|
|
|
//
|
|
|
|
|
btnPrev = selector.Data("⬅", "prev", ...)
|
|
|
|
|
btnNext = selector.Data("➡", "next", ...)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
menu.Reply(
|
|
|
|
|
menu.Row(btnHelp),
|
|
|
|
|
menu.Row(btnSettings),
|
|
|
|
|
)
|
|
|
|
|
selector.Inline(
|
|
|
|
|
selector.Row(btnPrev, btnNext),
|
|
|
|
|
)
|
2017-11-28 22:27:23 +00:00
|
|
|
|
|
|
|
|
|
// Command: /start <PAYLOAD>
|
|
|
|
|
b.Handle("/start", func(m *tb.Message) {
|
|
|
|
|
if !m.Private() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 11:52:56 +00:00
|
|
|
|
b.Send(m.Sender, "Hello!", menu)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// On reply button pressed (message)
|
|
|
|
|
b.Handle(&btnHelp, func(m *tb.Message) {...})
|
|
|
|
|
|
|
|
|
|
// On inline button pressed (callback)
|
|
|
|
|
b.Handle(&btnPrev, func(c *tb.Callback) {
|
|
|
|
|
// ...
|
|
|
|
|
// Always respond!
|
|
|
|
|
b.Respond(c, &tb.CallbackResponse{...})
|
2017-11-28 22:27:23 +00:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
b.Start()
|
|
|
|
|
}
|
2015-07-06 13:46:19 +00:00
|
|
|
|
```
|
|
|
|
|
|
2020-05-22 11:52:56 +00:00
|
|
|
|
You can use markup constructor for every type of possible buttons:
|
|
|
|
|
```go
|
|
|
|
|
r := &ReplyMarkup{}
|
|
|
|
|
|
|
|
|
|
// Reply buttons:
|
|
|
|
|
r.Text("Hello!")
|
|
|
|
|
r.Contact("Send phone number")
|
|
|
|
|
r.Location("Send location")
|
|
|
|
|
r.Poll(tb.PollQuiz)
|
|
|
|
|
|
|
|
|
|
// Inline buttons:
|
|
|
|
|
r.Data("Show help", "help") // data is optional
|
|
|
|
|
r.Data("Delete item", "delete", item.ID)
|
|
|
|
|
r.URL("Visit", "https://google.com")
|
|
|
|
|
r.Query("Search", query)
|
|
|
|
|
r.QueryChat("Share", query)
|
|
|
|
|
r.Login("Login", &tb.Login{...})
|
|
|
|
|
```
|
|
|
|
|
|
2017-11-28 22:27:23 +00:00
|
|
|
|
## Inline mode
|
2017-11-29 20:33:50 +00:00
|
|
|
|
So if you want to handle incoming inline queries you better plug the `tb.OnQuery`
|
|
|
|
|
endpoint and then use the `Answer()` method to send a list of inline queries
|
|
|
|
|
back. I think at the time of writing, telebot supports all of the provided result
|
|
|
|
|
types (but not the cached ones). This is how it looks like:
|
|
|
|
|
|
|
|
|
|
```go
|
|
|
|
|
b.Handle(tb.OnQuery, func(q *tb.Query) {
|
|
|
|
|
urls := []string{
|
|
|
|
|
"http://photo.jpg",
|
|
|
|
|
"http://photo2.jpg",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results := make(tb.Results, len(urls)) // []tb.Result
|
|
|
|
|
for i, url := range urls {
|
|
|
|
|
result := &tb.PhotoResult{
|
|
|
|
|
URL: url,
|
|
|
|
|
|
|
|
|
|
// required for photos
|
|
|
|
|
ThumbURL: url,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results[i] = result
|
2020-04-26 18:48:20 +00:00
|
|
|
|
// needed to set a unique string ID for each result
|
|
|
|
|
results[i].SetResultID(strconv.Itoa(i))
|
2017-11-29 20:33:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := b.Answer(q, &tb.QueryResponse{
|
2020-04-26 18:48:20 +00:00
|
|
|
|
Results: results,
|
2017-11-29 20:33:50 +00:00
|
|
|
|
CacheTime: 60, // a minute
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
2020-04-26 18:48:20 +00:00
|
|
|
|
log.Println(err)
|
2017-11-29 20:33:50 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
```
|
|
|
|
|
|
2020-03-28 23:35:23 +00:00
|
|
|
|
There's not much to talk about really. It also supports some form of authentication
|
2017-11-29 20:33:50 +00:00
|
|
|
|
through deep-linking. For that, use fields `SwitchPMText` and `SwitchPMParameter`
|
|
|
|
|
of `QueryResponse`.
|
|
|
|
|
|
|
|
|
|
# Contributing
|
|
|
|
|
|
|
|
|
|
1. Fork it
|
2020-04-26 18:48:20 +00:00
|
|
|
|
2. Clone develop: `git clone -b develop https://github.com/tucnak/telebot`
|
|
|
|
|
3. Create your feature branch: `git checkout -b new-feature`
|
2017-11-29 20:33:50 +00:00
|
|
|
|
4. Make changes and add them: `git add .`
|
2020-04-26 18:48:20 +00:00
|
|
|
|
5. Commit: `git commit -m "Add some feature"`
|
|
|
|
|
6. Push: `git push origin new-feature`
|
2017-11-29 20:33:50 +00:00
|
|
|
|
7. Pull request
|
|
|
|
|
|
|
|
|
|
# Donate
|
|
|
|
|
|
|
|
|
|
I do coding for fun but I also try to search for interesting solutions and
|
|
|
|
|
optimize them as much as possible.
|
|
|
|
|
If you feel like it's a good piece of software, I wouldn't mind a tip!
|
|
|
|
|
|
|
|
|
|
Bitcoin: `1DkfrFvSRqgBnBuxv9BzAz83dqur5zrdTH`
|
|
|
|
|
|
|
|
|
|
# License
|
2017-11-21 02:49:37 +00:00
|
|
|
|
|
2017-11-29 20:33:50 +00:00
|
|
|
|
Telebot is distributed under MIT.
|