From 862c67342c8aa2db813e37ccc638935cf2b7128b Mon Sep 17 00:00:00 2001 From: Maxim Martyanov Date: Thu, 2 Jun 2016 23:41:55 +0300 Subject: [PATCH] Rollback for backward compatibility, added NewInlineQueryResultArticleMarkdown and NewInlineQueryResultArticleHTML --- bot_test.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++- helpers.go | 29 ++++++++++++++++++-- helpers_test.go | 27 +++++++++++++++++-- 3 files changed, 121 insertions(+), 5 deletions(-) diff --git a/bot_test.go b/bot_test.go index bfd0875..fd61d22 100644 --- a/bot_test.go +++ b/bot_test.go @@ -501,7 +501,75 @@ func ExampleAnswerInlineQuery() { continue } - article := tgbotapi.NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query, "Markdown") + article := tgbotapi.NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query) + article.Description = update.InlineQuery.Query + + inlineConf := tgbotapi.InlineConfig{ + InlineQueryID: update.InlineQuery.ID, + IsPersonal: true, + CacheTime: 0, + Results: []interface{}{article}, + } + + if _, err := bot.AnswerInlineQuery(inlineConf); err != nil { + log.Println(err) + } + } +} + +func ExampleAnswerInlineQueryMarkdown() { + bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") // create new bot + if err != nil { + log.Panic(err) + } + + log.Printf("Authorized on account %s", bot.Self.UserName) + + u := tgbotapi.NewUpdate(0) + u.Timeout = 60 + + updates, err := bot.GetUpdatesChan(u) + + for update := range updates { + if update.InlineQuery == nil { // if no inline query, ignore it + continue + } + + article := tgbotapi.NewInlineQueryResultArticleMarkdown(update.InlineQuery.ID, "Echo", update.InlineQuery.Query) + article.Description = update.InlineQuery.Query + + inlineConf := tgbotapi.InlineConfig{ + InlineQueryID: update.InlineQuery.ID, + IsPersonal: true, + CacheTime: 0, + Results: []interface{}{article}, + } + + if _, err := bot.AnswerInlineQuery(inlineConf); err != nil { + log.Println(err) + } + } +} + +func ExampleAnswerInlineQueryHTML() { + bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") // create new bot + if err != nil { + log.Panic(err) + } + + log.Printf("Authorized on account %s", bot.Self.UserName) + + u := tgbotapi.NewUpdate(0) + u.Timeout = 60 + + updates, err := bot.GetUpdatesChan(u) + + for update := range updates { + if update.InlineQuery == nil { // if no inline query, ignore it + continue + } + + article := tgbotapi.NewInlineQueryResultArticleHTML(update.InlineQuery.ID, "Echo", update.InlineQuery.Query) article.Description = update.InlineQuery.Query inlineConf := tgbotapi.InlineConfig{ diff --git a/helpers.go b/helpers.go index 5d98cba..084a06b 100644 --- a/helpers.go +++ b/helpers.go @@ -317,14 +317,39 @@ func NewWebhookWithCert(link string, file interface{}) WebhookConfig { } // NewInlineQueryResultArticle creates a new inline query article. -func NewInlineQueryResultArticle(id, title, messageText string, parseMode string) InlineQueryResultArticle { +func NewInlineQueryResultArticle(id, title, messageText string) InlineQueryResultArticle { return InlineQueryResultArticle{ Type: "article", ID: id, Title: title, InputMessageContent: InputTextMessageContent{ Text: messageText, - ParseMode: parseMode, + }, + } +} + +// NewInlineQueryResultArticleMarkdown creates a new inline query article with Markdown parsing. +func NewInlineQueryResultArticleMarkdown(id, title, messageText string) InlineQueryResultArticle { + return InlineQueryResultArticle{ + Type: "article", + ID: id, + Title: title, + InputMessageContent: InputTextMessageContent{ + Text: messageText, + ParseMode: "Markdown", + }, + } +} + +// NewInlineQueryResultArticleHTML creates a new inline query article with HTML parsing. +func NewInlineQueryResultArticleHTML(id, title, messageText string) InlineQueryResultArticle { + return InlineQueryResultArticle{ + Type: "article", + ID: id, + Title: title, + InputMessageContent: InputTextMessageContent{ + Text: messageText, + ParseMode: "HTML", }, } } diff --git a/helpers_test.go b/helpers_test.go index ed17d07..d2a9e0d 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -6,17 +6,40 @@ import ( ) func TestNewInlineQueryResultArticle(t *testing.T) { - result := tgbotapi.NewInlineQueryResultArticle("id", "title", "message", "Markdown") + result := tgbotapi.NewInlineQueryResultArticle("id", "title", "message") if result.Type != "article" || result.ID != "id" || result.Title != "title" || - result.InputMessageContent.(tgbotapi.InputTextMessageContent).Text != "message" || + result.InputMessageContent.(tgbotapi.InputTextMessageContent).Text != "message" { + t.Fail() + } +} + +func TestNewInlineQueryResultArticleMarkdown(t *testing.T) { + result := tgbotapi.NewInlineQueryResultArticleMarkdown("id", "title", "*message*") + + if result.Type != "article" || + result.ID != "id" || + result.Title != "title" || + result.InputMessageContent.(tgbotapi.InputTextMessageContent).Text != "*message*" || result.InputMessageContent.(tgbotapi.InputTextMessageContent).ParseMode != "Markdown" { t.Fail() } } +func TestNewInlineQueryResultArticleHTML(t *testing.T) { + result := tgbotapi.NewInlineQueryResultArticleHTML("id", "title", "message") + + if result.Type != "article" || + result.ID != "id" || + result.Title != "title" || + result.InputMessageContent.(tgbotapi.InputTextMessageContent).Text != "message" || + result.InputMessageContent.(tgbotapi.InputTextMessageContent).ParseMode != "HTML" { + t.Fail() + } +} + func TestNewInlineQueryResultGIF(t *testing.T) { result := tgbotapi.NewInlineQueryResultGIF("id", "google.com")