github tucnak/telebot v3.4.2-beta
Telebot V3.4

55 minutes ago

Changelog

  • Bot API 7.2, 7.3, 7.4, 7.5, 7.6, 7.7, 7.8
  • Telegram Business support
  • Payments in Telegram Stars and paid media
  • New API interface, implemented by *Bot
  • New OnForward event
  • Reactions reworked to match the Bot API
  • Lots of minor fixes and improvements

V3.4 only ever got beta tags (v3.4.0-beta to v3.4.2-beta) and somehow never got its release page, so here it is, a little late, for the sake of history. Everything below is part of V4 as well.

Bot API 7.2: Business accounts

Bots can now be connected to Telegram Business accounts and handle the messages of their chats.

// 1. Handle the messages of the connected business accounts
b.Handle(tele.OnBusinessMessage, func(c tele.Context) error {
  msg := c.Message()

  // 2. Get the connection the message came through
  conn, err := c.Bot().BusinessConnection(msg.BusinessConnectionID)
  if err != nil {
    return err
  }

  conn.Sender
  conn.CanReply
  ...
})

// 3. Or track the connections themselves
b.Handle(tele.OnBusinessConnection, ...)
b.Handle(tele.OnEditedBusinessMessage, ...)
b.Handle(tele.OnDeletedBusinessMessages, ...)

Bot API 7.4, 7.6: Telegram Stars

Digital goods can be paid for in Telegram Stars, no payment provider needed. Use the tele.Stars currency and leave the token empty:

// 1. Send an invoice in Stars
b.Handle("/buy", func(c tele.Context) error {
  return c.Send(&tele.Invoice{
    Title:       "Premium",
    Description: "One month of premium",
    Payload:     "premium-1m",
    Currency:    tele.Stars,
    Prices:      []tele.Price{{Label: "Premium", Amount: 100}},
  })
})

// 2. Handle the successful payment, and refund it if you need to
b.Handle(tele.OnPayment, func(c tele.Context) error {
  payment := c.Payment()
  return c.Bot().RefundStars(c.Sender(), payment.TelegramChargeID)
})

// 3. Look through the bot's transactions
txs, err := b.StarTransactions(0, 100)

Photos and videos can be sold for Stars too:

b.SendPaid(user, 50, tele.PaidAlbum{
  &tele.Photo{File: tele.FromDisk("teaser.jpg"), Caption: "Unlock the full set"},
  &tele.Video{File: tele.FromDisk("making-of.mp4")},
})

API interface

Every bot method is now described by the tele.API interface, which makes it easy to wrap the bot or replace it with a mock in tests. Context.Bot() returns it as well:

var api tele.API = b

b.Handle("/start", func(c tele.Context) error {
  return c.Bot().SetMyName("Telebot", "en")
})

Reactions

Reactions now carry their type, which Telegram requires, and custom emoji reactions work as expected. ReactionOptions was folded into Reactions:

// 1. The telebot/react shorthand works as before
b.React(to, msg, react.React(react.ThumbUp, react.Fire))

// 2. Pass the Reactions directly for the special options
b.React(to, msg, tele.Reactions{
  Reactions: []tele.Reaction{react.Heart},
  Big:       true,
})

More shorthands

  • Handle forwarded messages with the new OnForward event (#697):
b.Handle(tele.OnForward, func(c tele.Context) error {
  return c.Reply("Thanks for sharing!")
})
  • Manage the webhook yourself and keep telebot from calling setWebhook on start:
b, err := tele.NewBot(tele.Settings{
  Token: token,
  Poller: &tele.Webhook{
    Listen:           ":8080",
    IgnoreSetWebhook: true,
  },
})
  • Pass a nil user to SetMenuButton to change the default menu button for all chats.

Backward incompatible changes

Some of the Bot API updates renamed or removed fields, and we followed them:

  • b.React takes tele.Reactions instead of ...ReactionOptions
  • Context.Bot() returns tele.API instead of *tele.Bot
  • MessageReactionCount.Reactions is a slice
  • StickerSet.Animated and StickerSet.Video are removed, since sticker sets can mix formats now
  • RecipientShared.UserID is replaced by RecipientShared.Users
  • Message.ExternalReplyInfo is Message.ExternalReply, and Message.SenderBoostCount is Message.SenderBoosts
  • Custom emoji fields end with ID now: Sticker.CustomEmojiID, Reaction.CustomEmojiID, MessageEntity.CustomEmojiID, Topic.IconCustomEmojiID, Chat.StatusCustomEmojiID, Chat.BackgroundCustomEmojiID and others

Fixes

  • Spoilers are sent as has_spoiler (#690)
  • Inline buttons support callback_game and pay (#680)
  • Reactions carry their required type (#716)
  • MessageReactionCount is decoded correctly
  • A photo's caption is kept when unmarshalling
  • QueryResponseButton.Start is omitted when empty
  • layout.New resolves relative paths again

Discussion

Discuss on Telegram

Don't miss a new telebot release

NewReleases is sending notifications on new releases.