MarkdownV2 as default, use MarkdownLegacy for old-fashioned MD.

pull/270/head
Ian Patrick Badtrousers 4 years ago
parent 16baf678a5
commit 12299becfa

281
bot.go

@ -149,6 +149,11 @@ var (
cbackRx = regexp.MustCompile(`^\f(\w+)(\|(.+))?$`)
)
func (b *Bot) handleCommand(m *Message, cmdName, cmdBot string) bool {
return false
}
// Start brings bot into motion by consuming incoming
// updates (see Bot.Updates channel).
func (b *Bot) Start() {
@ -277,7 +282,7 @@ func (b *Bot) incomingUpdate(upd *Update) {
defer b.deferDebug()
}
handler(from, to)
}(b, handler, m.Chat.ID, m.MigrateTo)
}(b, handler, m.MigrateFrom, m.MigrateTo)
} else {
panic("telebot: migration handler is bad")
@ -307,13 +312,8 @@ func (b *Bot) incomingUpdate(upd *Update) {
if upd.Callback != nil {
if upd.Callback.Data != "" {
if upd.Callback.MessageID != "" {
upd.Callback.Message = &Message{
InlineID: upd.Callback.MessageID,
}
}
data := upd.Callback.Data
if data[0] == '\f' {
match := cbackRx.FindAllStringSubmatch(data, -1)
@ -503,7 +503,7 @@ func (b *Bot) Stop() {
// some Sendable (or string!) and optional send options.
//
// Note: since most arguments are of type interface{}, but have pointer
// method receivers, make sure to pass them by-pointer, NOT by-value.
// method recievers, make sure to pass them by-pointer, NOT by-value.
//
// What is a send option exactly? It can be one of the following types:
//
@ -540,15 +540,14 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
f := x.MediaFile()
switch {
case f.InCloud():
if f.InCloud() {
mediaRepr = f.FileID
case f.FileURL != "":
} else if f.FileURL != "" {
mediaRepr = f.FileURL
case f.OnDisk() || f.FileReader != nil:
mediaRepr = "attach://" + strconv.Itoa(i)
} else if f.OnDisk() || f.FileReader != nil {
mediaRepr = fmt.Sprintf("attach://%d", i)
files[strconv.Itoa(i)] = *f
default:
} else {
return nil, errors.Errorf(
"telebot: album entry #%d doesn't exist anywhere", i)
}
@ -556,15 +555,13 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
switch y := x.(type) {
case *Photo:
jsonRepr, _ = json.Marshal(struct {
Type string `json:"type"`
Media string `json:"media"`
Caption string `json:"caption,omitempty"`
ParseMode ParseMode `json:"parse_mode,omitempty"`
Type string `json:"type"`
Caption string `json:"caption"`
Media string `json:"media"`
}{
"photo",
mediaRepr,
y.Caption,
y.ParseMode,
mediaRepr,
})
case *Video:
jsonRepr, _ = json.Marshal(struct {
@ -619,7 +616,7 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag
return nil, errors.Errorf("api error: %s", resp.Description)
}
for attachName := range files {
for attachName, _ := range files {
i, _ := strconv.Atoi(attachName)
var newID string
@ -714,39 +711,11 @@ func (b *Bot) Edit(message Editable, what interface{}, options ...interface{}) (
return extractMsgResponse(respJSON)
}
// EditReplyMarkup used to edit reply markup of already sent message.
//
// On success, returns edited message object
func (b *Bot) EditReplyMarkup(message Editable, markup *ReplyMarkup) (*Message, error) {
messageID, chatID := message.MessageSig()
params := map[string]string{}
// if inline message
if chatID == 0 {
params["inline_message_id"] = messageID
} else {
params["chat_id"] = strconv.FormatInt(chatID, 10)
params["message_id"] = messageID
}
processButtons(markup.InlineKeyboard)
jsonMarkup, _ := json.Marshal(markup)
params["reply_markup"] = string(jsonMarkup)
respJSON, err := b.Raw("editMessageReplyMarkup", params)
if err != nil {
return nil, err
}
return extractMsgResponse(respJSON)
}
// EditCaption used to edit already sent photo caption with known recipient and message id.
// EditCaption used to edit already sent photo caption with known recepient and message id.
//
// On success, returns edited message object
func (b *Bot) EditCaption(message Editable, caption string, options ...interface{}) (*Message, error) {
messageID, chatID := message.MessageSig()
func (b *Bot) EditCaption(originalMsg Editable, caption string) (*Message, error) {
messageID, chatID := originalMsg.MessageSig()
params := map[string]string{"caption": caption}
@ -758,9 +727,6 @@ func (b *Bot) EditCaption(message Editable, caption string, options ...interface
params["message_id"] = messageID
}
sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
respJSON, err := b.Raw("editMessageCaption", params)
if err != nil {
return nil, err
@ -769,7 +735,7 @@ func (b *Bot) EditCaption(message Editable, caption string, options ...interface
return extractMsgResponse(respJSON)
}
// EditMedia used to edit already sent media with known recipient and message id.
// EditMedia used to edit already sent media with known recepient and message id.
//
// Use cases:
//
@ -784,34 +750,28 @@ func (b *Bot) EditMedia(message Editable, inputMedia InputMedia, options ...inte
file := make(map[string]File)
f := inputMedia.MediaFile()
thumbAttachName := "thumb"
switch {
case f.InCloud():
if f.InCloud() {
mediaRepr = f.FileID
case f.FileURL != "":
} else if f.FileURL != "" {
mediaRepr = f.FileURL
case f.OnDisk() || f.FileReader != nil:
} else if f.OnDisk() || f.FileReader != nil {
s := f.FileLocal
if f.FileReader != nil {
s = "0"
}
if s == thumbAttachName {
thumbAttachName = "thumb2"
}
mediaRepr = "attach://" + s
file[s] = *f
default:
} else {
return nil, errors.Errorf(
"telebot: can't edit media, it doesn't exist anywhere")
}
type FileJSON struct {
type FileJson struct {
// All types.
Type string `json:"type"`
Caption string `json:"caption"`
Media string `json:"media"`
ParseMode ParseMode `json:"parse_mode,omitempty"`
Type string `json:"type"`
Caption string `json:"caption"`
Media string `json:"media"`
// Video.
Width int `json:"width,omitempty"`
@ -833,12 +793,7 @@ func (b *Bot) EditMedia(message Editable, inputMedia InputMedia, options ...inte
Performer string `json:"performer,omitempty"`
}
resultMedia := &FileJSON{Media: mediaRepr}
sendOpts := extractOptions(options)
if sendOpts != nil {
resultMedia.ParseMode = sendOpts.ParseMode
}
resultMedia := &FileJson{Media: mediaRepr}
switch y := inputMedia.(type) {
case *Photo:
@ -854,7 +809,7 @@ func (b *Bot) EditMedia(message Editable, inputMedia InputMedia, options ...inte
resultMedia.MIME = y.MIME
thumb = y.Thumbnail
if thumb != nil {
resultMedia.Thumbnail = "attach://" + thumbAttachName
resultMedia.Thumbnail = "attach://thumb"
}
case *Document:
resultMedia.Type = "document"
@ -863,7 +818,7 @@ func (b *Bot) EditMedia(message Editable, inputMedia InputMedia, options ...inte
resultMedia.MIME = y.MIME
thumb = y.Thumbnail
if thumb != nil {
resultMedia.Thumbnail = "attach://" + thumbAttachName
resultMedia.Thumbnail = "attach://thumb"
}
case *Audio:
resultMedia.Type = "audio"
@ -872,10 +827,6 @@ func (b *Bot) EditMedia(message Editable, inputMedia InputMedia, options ...inte
resultMedia.MIME = y.MIME
resultMedia.Title = y.Title
resultMedia.Performer = y.Performer
thumb = y.Thumbnail
if thumb != nil {
resultMedia.Thumbnail = "attach://" + thumbAttachName
}
default:
return nil, errors.Errorf("telebot: inputMedia entry is not valid")
}
@ -895,9 +846,10 @@ func (b *Bot) EditMedia(message Editable, inputMedia InputMedia, options ...inte
}
if thumb != nil {
file[thumbAttachName] = *thumb.MediaFile()
file["thumb"] = *thumb.MediaFile()
}
sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
respJSON, err := b.sendFiles("editMessageMedia", file, params)
@ -940,7 +892,7 @@ func (b *Bot) Delete(message Editable) error {
// Chat action is a status message that recipient would see where
// you typically see "Harry is typing" status message. The only
// difference is that bots' chat actions live only for 5 seconds
// and die just once the client receives a message from the bot.
// and die just once the client recieves a message from the bot.
//
// Currently, Telegram supports only a narrow range of possible
// actions, these are aligned as constants of this package.
@ -1088,23 +1040,16 @@ func (b *Bot) GetFile(file *File) (io.ReadCloser, error) {
if err != nil {
return nil, err
}
// save FilePath
file.FilePath = f.FilePath
req, err := http.NewRequest("GET", b.URL+"/file/bot"+b.Token+"/"+f.FilePath, nil)
if err != nil {
return nil, wrapSystem(err)
}
url := fmt.Sprintf("%s/file/bot%s/%s",
b.URL, b.Token, f.FilePath)
resp, err := b.client.Do(req)
resp, err := http.Get(url)
if err != nil {
return nil, errors.Wrap(err, "file http.GET failed")
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, errors.Errorf("api error: expected 200 OK but got %s", resp.Status)
return nil, err
}
// set FilePath
*file = f
return resp.Body, nil
}
@ -1117,7 +1062,7 @@ func (b *Bot) StopLiveLocation(message Editable, options ...interface{}) (*Messa
messageID, chatID := message.MessageSig()
params := map[string]string{
"chat_id": strconv.FormatInt(chatID, 10),
"chat_id": fmt.Sprintf("%d", chatID),
"message_id": messageID,
}
@ -1161,7 +1106,7 @@ func (b *Bot) GetInviteLink(chat *Chat) (string, error) {
return resp.Result, nil
}
// SetGroupTitle should be used to update group title.
// SetChatTitle should be used to update group title.
func (b *Bot) SetGroupTitle(chat *Chat, newTitle string) error {
params := map[string]string{
"chat_id": chat.Recipient(),
@ -1410,141 +1355,5 @@ func (b *Bot) FileURLByID(fileID string) (string, error) {
if err != nil {
return "", err
}
return b.URL + "/file/bot" + b.Token + "/" + f.FilePath, nil
}
// UploadStickerFile returns uploaded File on success.
func (b *Bot) UploadStickerFile(userID int, pngSticker *File) (*File, error) {
files := map[string]File{
"png_sticker": *pngSticker,
}
params := map[string]string{
"user_id": strconv.Itoa(userID),
}
respJSON, err := b.sendFiles("uploadStickerFile", files, params)
if err != nil {
return nil, err
}
var resp struct {
Ok bool
Result File
Description string
}
err = json.Unmarshal(respJSON, &resp)
if err != nil {
return nil, err
}
if !resp.Ok {
return nil, errors.Errorf("api error: %s", resp.Description)
}
return &resp.Result, nil
}
// GetStickerSet returns StickerSet on success.
func (b *Bot) GetStickerSet(name string) (*StickerSet, error) {
respJSON, err := b.Raw("getStickerSet", map[string]string{"name": name})
if err != nil {
return nil, err
}
var resp struct {
Ok bool
Description string
Result *StickerSet
}
err = json.Unmarshal(respJSON, &resp)
if err != nil {
return nil, err
}
if !resp.Ok {
return nil, errors.Errorf("api error: %s", resp.Description)
}
return resp.Result, nil
}
// CreateNewStickerSet creates new sticker set.
func (b *Bot) CreateNewStickerSet(sp StickerSetParams, containsMasks bool, maskPosition MaskPosition) error {
files := map[string]File{
"png_sticker": *sp.PngSticker,
}
params := map[string]string{
"user_id": strconv.Itoa(sp.UserID),
"name": sp.Name,
"title": sp.Title,
"emojis": sp.Emojis,
}
if containsMasks {
mp, err := json.Marshal(&maskPosition)
if err != nil {
return err
}
params["mask_position"] = string(mp)
}
respJSON, err := b.sendFiles("createNewStickerSet", files, params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
// AddStickerToSet adds new sticker to existing sticker set.
func (b *Bot) AddStickerToSet(sp StickerSetParams, maskPosition MaskPosition) error {
files := map[string]File{
"png_sticker": *sp.PngSticker,
}
params := map[string]string{
"user_id": strconv.Itoa(sp.UserID),
"name": sp.Name,
"title": sp.Title,
"emojis": sp.Emojis,
}
if maskPosition != (MaskPosition{}) {
mp, err := json.Marshal(&maskPosition)
if err != nil {
return err
}
params["mask_position"] = string(mp)
}
respJSON, err := b.sendFiles("addStickerToSet", files, params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
// SetStickerPositionInSet moves a sticker in set to a specific position.
func (b *Bot) SetStickerPositionInSet(sticker string, position int) error {
params := map[string]string{
"sticker": sticker,
"position": strconv.Itoa(position),
}
respJSON, err := b.Raw("setStickerPositionInSet", params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
// DeleteStickerFromSet deletes sticker from set created by the bot.
func (b *Bot) DeleteStickerFromSet(sticker string) error {
respJSON, err := b.Raw("deleteStickerFromSet", map[string]string{"sticker": sticker})
if err != nil {
return err
}
return extractOkResponse(respJSON)
return fmt.Sprintf("%s/file/bot%s/%s", b.URL, b.Token, f.FilePath), nil
}

@ -109,9 +109,10 @@ const (
type ParseMode string
const (
ModeDefault ParseMode = ""
ModeMarkdown ParseMode = "Markdown"
ModeHTML ParseMode = "HTML"
ModeDefault ParseMode = ""
ModeMarkdown ParseMode = "MarkdownV2"
ModeLegacyMarkdown ParseMode = "Markdown"
ModeHTML ParseMode = "HTML"
)
// EntityType is a MessageEntity type.
@ -129,6 +130,8 @@ const (
EntityCode EntityType = "code"
EntityCodeBlock EntityType = "pre"
EntityTextLink EntityType = "text_link"
EntityUnderline EntityType = "underline"
Strikethrough EntityType = "strikethrough"
)
// ChatType represents one of the possible chat types.

Loading…
Cancel
Save