Documentation for latest changes.

pull/108/head
Ian Byrd 7 years ago
parent 9e92b919f8
commit 3cf300d59d
No known key found for this signature in database
GPG Key ID: 598F598CA3B8055F

@ -104,6 +104,20 @@ func (b *Bot) poll(
}
}
// Send accepts 2+ arguments, starting with destination chat, followed by
// some Sendable (or string!) and optional send options.
//
// Note: since most arguments are of type interface{}, make sure to pass
// them by-pointer, NOT by-value, which will result in a panic.
//
// What is a send option exactly? It can be one of the following types:
//
// - Option (a shorcut flag for popular options)
// - *SendOptions (the actual object accepted by Telegram API)
// - *ReplyMarkup (a component of SendOptions)
// - ParseMode (HTML, Markdown, etc)
//
// This function will panic upon unsupported payloads and options!
func (b *Bot) Send(to Recipient, what interface{}, how ...interface{}) (*Message, error) {
options := extractOptions(how)
@ -117,6 +131,9 @@ func (b *Bot) Send(to Recipient, what interface{}, how ...interface{}) (*Message
}
}
// Reply behaves just like Send() with an exception of "reply-to" indicator.
//
// This function will panic upon unsupported payloads and options!
func (b *Bot) Reply(to *Message, what interface{}, how ...interface{}) (*Message, error) {
options := extractOptions(how)
if options == nil {
@ -128,6 +145,10 @@ func (b *Bot) Reply(to *Message, what interface{}, how ...interface{}) (*Message
return b.Send(to.Chat, what, options)
}
// Forward behaves just like Send() but of all options it
// only supports Silent (see Bots API).
//
// This function will panic upon unsupported payloads and options!
func (b *Bot) Forward(to Recipient, what *Message, how ...interface{}) (*Message, error) {
params := map[string]string{
"chat_id": to.Destination(),
@ -149,6 +170,16 @@ func (b *Bot) Forward(to Recipient, what *Message, how ...interface{}) (*Message
return extractMsgResponse(respJSON)
}
// Delete removes the message, including service messages,
// with the following limitations:
//
// - A message can only be deleted if it was sent less than 48 hours ago.
// - Bots can delete outgoing messages in groups and supergroups.
// - Bots granted can_post_messages permissions can delete outgoing
// messages in channels.
// - If the bot is an administrator of a group, it can delete any message there.
// - If the bot has can_delete_messages permission in a supergroup or a
// channel, it can delete any message there.
func (b *Bot) Delete(what *Message) error {
params := map[string]string{
"chat_id": what.Chat.Destination(),

@ -1,5 +1,6 @@
package telebot
// Option is a shorcut flag for certain SendOptions.
type Option int
const (
@ -12,12 +13,6 @@ const (
// ReplyMarkup.ForceReply
ForceReply
// ReplyMarkup.HideCustomKeyboard
HideKeyboard
// ReplyMarkup.ResizeKeyboard
ResizeKeyboard
// ReplyMarkup.OneTimeKeyboard
OneTimeKeyboard
)
@ -58,11 +53,6 @@ type ReplyMarkup struct {
// Note: you don't need to set HideCustomKeyboard field to show custom keyboard.
ReplyKeyboard [][]KeyboardButton `json:"keyboard,omitempty"`
// Requests clients to hide the custom keyboard.
//
// Note: You dont need to set CustomKeyboard field to hide custom keyboard.
HideReplyKeyboard bool `json:"hide_keyboard,omitempty"`
// Requests clients to resize the keyboard vertically for optimal fit
// (e.g. make the keyboard smaller if there are just two rows of buttons).
//

@ -3,6 +3,9 @@ package telebot
import "strconv"
// Sendable is any object that can send itself.
//
// This is pretty cool, since it lets bots implement
// custom Sendables with certain properties.
type Sendable interface {
Send(*Bot, Recipient, *SendOptions) (*Message, error)
}

@ -46,27 +46,67 @@ func extractOkResponse(respJSON []byte) error {
}
func extractOptions(how []interface{}) *SendOptions {
var options *SendOptions
var opts *SendOptions
for _, object := range how {
switch option := object.(type) {
for _, prop := range how {
switch opt := prop.(type) {
case *SendOptions:
options = option
opts = opt
break
case *ReplyMarkup:
if options == nil {
options = &SendOptions{}
if opts == nil {
opts = &SendOptions{}
}
options.ReplyMarkup = option
opts.ReplyMarkup = opt
break
case Option:
if opts == nil {
opts = &SendOptions{}
}
switch opt {
case NoPreview:
opts.DisableWebPagePreview = true
break
case Silent:
opts.DisableNotification = true
break
case ForceReply:
if opts.ReplyMarkup == nil {
opts.ReplyMarkup = &ReplyMarkup{}
}
opts.ReplyMarkup.ForceReply = true
break
case OneTimeKeyboard:
if opts.ReplyMarkup == nil {
opts.ReplyMarkup = &ReplyMarkup{}
}
opts.ReplyMarkup.OneTimeKeyboard = true
break
default:
panic("telebot: unsupported option")
}
break
case ParseMode:
if opts == nil {
opts = &SendOptions{}
}
opts.ParseMode = opt
break
default:
panic(fmt.Sprintf("telebot: %v is not a send-option", option))
panic(fmt.Sprintf("telebot: %v is not a send-option", opt))
}
}
return options
return opts
}
func embedSendOptions(params map[string]string, opt *SendOptions) {
@ -91,14 +131,7 @@ func embedSendOptions(params map[string]string, opt *SendOptions) {
}
if opt.ReplyMarkup != nil {
force := opt.ReplyMarkup.ForceReply
reply := opt.ReplyMarkup.ReplyKeyboard != nil
inline := opt.ReplyMarkup.InlineKeyboard != nil
hidden := opt.ReplyMarkup.HideReplyKeyboard
resize := opt.ReplyMarkup.ResizeReplyKeyboard
if force || reply || inline || hidden || resize {
replyMarkup, _ := json.Marshal(opt.ReplyMarkup)
params["reply_markup"] = string(replyMarkup)
}
replyMarkup, _ := json.Marshal(opt.ReplyMarkup)
params["reply_markup"] = string(replyMarkup)
}
}

Loading…
Cancel
Save