telebot: tele is our new alias

pull/341/head
Demian 4 years ago
parent 4062f24361
commit bc180b4a47

@ -51,13 +51,13 @@ import (
)
func main() {
b, err := tb.NewBot(tb.Settings{
b, err := tele.NewBot(tele.Settings{
// You can also set custom API URL.
// If field is empty it equals to "https://api.telegram.org".
URL: "http://195.129.111.17:8012",
Token: "TOKEN_HERE",
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
@ -65,7 +65,7 @@ func main() {
return
}
b.Handle("/hello", func(m *tb.Message) {
b.Handle("/hello", func(m *tele.Message) {
b.Send(m.Sender, "Hello World!")
})
@ -81,22 +81,22 @@ endpoints. You can find the full list
[here](https://godoc.org/gopkg.in/tucnak/telebot.v2#pkg-constants).
```go
b, _ := tb.NewBot(settings)
b, _ := tele.NewBot(settings)
b.Handle(tb.OnText, func(m *tb.Message) {
b.Handle(tele.OnText, func(m *tele.Message) {
// all the text messages that weren't
// captured by existing handlers
})
b.Handle(tb.OnPhoto, func(m *tb.Message) {
b.Handle(tele.OnPhoto, func(m *tele.Message) {
// photos only
})
b.Handle(tb.OnChannelPost, func (m *tb.Message) {
b.Handle(tele.OnChannelPost, func (m *tele.Message) {
// channel posts only
})
b.Handle(tb.OnQuery, func (q *tb.Query) {
b.Handle(tele.OnQuery, func (q *tele.Query) {
// incoming inline queries
})
```
@ -133,8 +133,8 @@ 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:
```go
poller := &tb.LongPoller{Timeout: 15 * time.Second}
spamProtected := tb.NewMiddlewarePoller(poller, func(upd *tb.Update) bool {
poller := &tele.LongPoller{Timeout: 15 * time.Second}
spamProtected := tele.NewMiddlewarePoller(poller, func(upd *tele.Update) bool {
if upd.Message == nil {
return true
}
@ -146,7 +146,7 @@ spamProtected := tb.NewMiddlewarePoller(poller, func(upd *tb.Update) bool {
return true
})
bot, _ := tb.NewBot(tb.Settings{
bot, _ := tele.NewBot(tele.Settings{
// ...
Poller: spamProtected,
})
@ -167,7 +167,7 @@ other bot, even if [privacy mode](https://core.telegram.org/bots#privacy-mode) i
For simplified deep-linking, telebot also extracts payload:
```go
// Command: /start <PAYLOAD>
b.Handle("/start", func(m *tb.Message) {
b.Handle("/start", func(m *tele.Message) {
if !m.Private() {
return
}
@ -183,7 +183,7 @@ 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")}
a := &tele.Audio{File: tele.FromDisk("file.ogg")}
fmt.Println(a.OnDisk()) // true
fmt.Println(a.InCloud()) // false
@ -227,10 +227,10 @@ for that. Albums were added not so long ago, so they are slightly quirky for bac
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")}
p := &tele.Photo{File: tele.FromDisk("chicken.jpg")}
v := &tele.Video{File: tele.FromURL("http://video.mp4")}
msgs, err := b.SendAlbum(user, tb.Album{p, v})
msgs, err := b.SendAlbum(user, tele.Album{p, v})
```
### Send options
@ -241,18 +241,18 @@ 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{
b.Send(user, "text", &tele.SendOptions{
// ...
})
// ReplyMarkup is a part of SendOptions,
// but often it's the only option you need
b.Send(user, "text", &tb.ReplyMarkup{
b.Send(user, "text", &tele.ReplyMarkup{
// ...
})
// flags: no notification && no web link preview
b.Send(user, "text", tb.Silent, tb.NoPreview)
b.Send(user, "text", tele.Silent, tele.NoPreview)
```
Full list of supported option-flags you can find
@ -303,7 +303,7 @@ func (x StoredMessage) MessageSig() (int, int64) {
Why bother at all? Well, it allows you to do things like this:
```go
// just two integer columns in the database
var msgs []tb.StoredMessage
var msgs []tele.StoredMessage
db.Find(&msgs) // gorm syntax
for _, msg := range msgs {
@ -333,7 +333,7 @@ The main goal is to avoid a lot of boilerplate and to make code clearer.
```go
func main() {
b, _ := tb.NewBot(tb.Settings{...})
b, _ := tele.NewBot(tele.Settings{...})
var (
// Universal markup builders.
@ -365,7 +365,7 @@ func main() {
)
// Command: /start <PAYLOAD>
b.Handle("/start", func(m *tb.Message) {
b.Handle("/start", func(m *tele.Message) {
if !m.Private() {
return
}
@ -374,13 +374,13 @@ func main() {
})
// On reply button pressed (message)
b.Handle(&btnHelp, func(m *tb.Message) {...})
b.Handle(&btnHelp, func(m *tele.Message) {...})
// On inline button pressed (callback)
b.Handle(&btnPrev, func(c *tb.Callback) {
b.Handle(&btnPrev, func(c *tele.Callback) {
// ...
// Always respond!
b.Respond(c, &tb.CallbackResponse{...})
b.Respond(c, &tele.CallbackResponse{...})
})
b.Start()
@ -395,7 +395,7 @@ r := &ReplyMarkup{}
r.Text("Hello!")
r.Contact("Send phone number")
r.Location("Send location")
r.Poll(tb.PollQuiz)
r.Poll(tele.PollQuiz)
// Inline buttons:
r.Data("Show help", "help") // data is optional
@ -403,25 +403,25 @@ 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{...})
r.Login("Login", &tele.Login{...})
```
## Inline mode
So if you want to handle incoming inline queries you better plug the `tb.OnQuery`
So if you want to handle incoming inline queries you better plug the `tele.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) {
b.Handle(tele.OnQuery, func(q *tele.Query) {
urls := []string{
"http://photo.jpg",
"http://photo2.jpg",
}
results := make(tb.Results, len(urls)) // []tb.Result
results := make(tele.Results, len(urls)) // []tele.Result
for i, url := range urls {
result := &tb.PhotoResult{
result := &tele.PhotoResult{
URL: url,
// required for photos
@ -433,7 +433,7 @@ b.Handle(tb.OnQuery, func(q *tb.Query) {
results[i].SetResultID(strconv.Itoa(i))
}
err := b.Answer(q, &tb.QueryResponse{
err := b.Answer(q, &tele.QueryResponse{
Results: results,
CacheTime: 60, // a minute
})

@ -30,7 +30,7 @@ func NoRights() Rights { return Rights{} }
// NoRestrictions should be used when un-restricting or
// un-promoting user.
//
// member.Rights = tb.NoRestrictions()
// member.Rights = tele.NoRestrictions()
// b.Restrict(chat, member)
//
func NoRestrictions() Rights {

@ -155,7 +155,7 @@ func (b *Bot) Group() *Group {
// })
//
// b.Handle(&inlineButton, func (c tele.Context) error {
// return c.Respond(&tb.CallbackResponse{Text: "Hello!"})
// return c.Respond(&tele.CallbackResponse{Text: "Hello!"})
// })
//
// Middleware usage:
@ -488,7 +488,7 @@ func (b *Bot) Send(to Recipient, what interface{}, opts ...interface{}) (*Messag
}
// SendAlbum sends multiple instances of media as a single message.
// From all existing options, it only supports tb.Silent.
// From all existing options, it only supports tele.Silent.
func (b *Bot) SendAlbum(to Recipient, a Album, opts ...interface{}) ([]Message, error) {
if to == nil {
return nil, ErrBadRecipient
@ -636,10 +636,10 @@ func (b *Bot) Forward(to Recipient, msg Editable, opts ...interface{}) (*Message
// Use cases:
//
// b.Edit(m, m.Text, newMarkup)
// b.Edit(m, "new <b>text</b>", tb.ModeHTML)
// b.Edit(m, &tb.ReplyMarkup{...})
// b.Edit(m, &tb.Photo{File: ...})
// b.Edit(m, tb.Location{42.1337, 69.4242})
// b.Edit(m, "new <b>text</b>", tele.ModeHTML)
// b.Edit(m, &tele.ReplyMarkup{...})
// b.Edit(m, &tele.Photo{File: ...})
// b.Edit(m, tele.Location{42.1337, 69.4242})
//
func (b *Bot) Edit(msg Editable, what interface{}, opts ...interface{}) (*Message, error) {
var (
@ -757,8 +757,8 @@ func (b *Bot) EditCaption(msg Editable, caption string, opts ...interface{}) (*M
//
// Use cases:
//
// b.EditMedia(m, &tb.Photo{File: tb.FromDisk("chicken.jpg")})
// b.EditMedia(m, &tb.Video{File: tb.FromURL("http://video.mp4")})
// b.EditMedia(m, &tele.Photo{File: tele.FromDisk("chicken.jpg")})
// b.EditMedia(m, &tele.Video{File: tele.FromURL("http://video.mp4")})
//
func (b *Bot) EditMedia(msg Editable, media InputMedia, opts ...interface{}) (*Message, error) {
var (

@ -82,7 +82,7 @@ type ChatMember struct {
// 30 seconds from the current time, they are considered to be
// restricted forever.
//
// Use tb.Forever().
// Use tele.Forever().
//
RestrictedUntil int64 `json:"until_date,omitempty"`
}
@ -90,15 +90,15 @@ type ChatMember struct {
// ChatID represents a chat or an user integer ID, which can be used
// as recipient in bot methods. It is very useful in cases where
// you have special group IDs, for example in your config, and don't
// want to wrap it into *tb.Chat every time you send messages.
// want to wrap it into *tele.Chat every time you send messages.
//
// Example:
//
// group := tb.ChatID(-100756389456)
// group := tele.ChatID(-100756389456)
// b.Send(group, "Hello!")
//
// type Config struct {
// AdminGroup tb.ChatID `json:"admin_group"`
// AdminGroup tele.ChatID `json:"admin_group"`
// }
// b.Send(conf.AdminGroup, "Hello!")
//

@ -5,14 +5,14 @@ This bot is different from a typical bot in two ways:
1. It is configured with `Settings.Synchronous = true`. This disables asynchronous handlers to let Lambda wait for their completion:
```go
b, _ := tb.NewBot(tb.Settings{Token: token, Synchronous: true})
b, _ := tele.NewBot(tele.Settings{Token: token, Synchronous: true})
```
2. Instead of `Settings.Poller` and `bot.Start` it calls `bot.ProcessUpdate` inside `lambda.Start`:
```go
lambda.Start(func(req events.APIGatewayProxyRequest) (err error) {
var u tb.Update
var u tele.Update
if err = json.Unmarshal([]byte(req.Body), &u); err == nil {
b.ProcessUpdate(u)
}

@ -10,7 +10,7 @@ import (
)
func main() {
b, err := tb.NewBot(tb.Settings{
b, err := tele.NewBot(tele.Settings{
Token: os.Getenv("TELEBOT_SECRET"),
Synchronous: true,
})
@ -18,10 +18,10 @@ func main() {
panic(err)
}
b.Handle(tb.OnText, func(m *tb.Message) { b.Send(m.Chat, m.Text) })
b.Handle(tele.OnText, func(m *tele.Message) { b.Send(m.Chat, m.Text) })
lambda.Start(func(req events.APIGatewayProxyRequest) (err error) {
var u tb.Update
var u tele.Update
if err = json.Unmarshal([]byte(req.Body), &u); err == nil {
b.ProcessUpdate(u)
}

@ -31,7 +31,7 @@ type File struct {
// so upon uploading media you'll need to set embedded File
// with something. NewFile() returning File makes it a one-liner.
//
// photo := &tb.Photo{File: tb.FromDisk("chicken.jpg")}
// photo := &tele.Photo{File: tele.FromDisk("chicken.jpg")}
//
func FromDisk(filename string) File {
return File{FileLocal: filename}
@ -44,7 +44,7 @@ func FromDisk(filename string) File {
// so upon uploading media you'll need to set embedded File
// with something. NewFile() returning File makes it a one-liner.
//
// photo := &tb.Photo{File: tb.FromURL("https://site.com/picture.jpg")}
// photo := &tele.Photo{File: tele.FromURL("https://site.com/picture.jpg")}
//
func FromURL(url string) File {
return File{FileURL: url}
@ -57,7 +57,7 @@ func FromURL(url string) File {
// so upon uploading media you'll need to set embedded File
// with something. NewFile() returning File makes it a one-liner.
//
// photo := &tb.Photo{File: tb.FromReader(bytes.NewReader(...))}
// photo := &tele.Photo{File: tele.FromReader(bytes.NewReader(...))}
//
func FromReader(reader io.Reader) File {
return File{FileReader: reader}

@ -3,6 +3,7 @@ module gopkg.in/tucnak/telebot.v2
go 1.12
require (
github.com/aws/aws-lambda-go v1.18.0 // indirect
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.5.1
)

@ -1,12 +1,20 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/aws/aws-lambda-go v1.18.0 h1:13AfxzFoPlFjOzXHbRnKuTbteCzHbu4YQgKONNhWcmo=
github.com/aws/aws-lambda-go v1.18.0/go.mod h1:FEwgPLE6+8wcGBTe5cJN3JWurd1Ztm9zN4jsXsjzKKw=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=

@ -10,16 +10,16 @@
// )
//
// func main() {
// b, err := tb.NewBot(tb.Settings{
// b, err := tele.NewBot(tele.Settings{
// Token: "TOKEN_HERE",
// Poller: &tb.LongPoller{Timeout: 10 * time.Second},
// Poller: &tele.LongPoller{Timeout: 10 * time.Second},
// })
//
// if err != nil {
// return
// }
//
// b.Handle(tb.OnText, func(m *tb.Message) {
// b.Handle(tele.OnText, func(m *tele.Message) {
// b.Send(m.Sender, "hello world")
// })
//

Loading…
Cancel
Save