Skip to content

Commit 7f37376

Browse files
author
Syfaro
committed
Merge pull request #49 from llorephie/master
Add support for Markdown/HTML in NewInlineQueryResultArticle
2 parents 217764b + 862c673 commit 7f37376

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

bot_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,3 +516,71 @@ func ExampleAnswerInlineQuery() {
516516
}
517517
}
518518
}
519+
520+
func ExampleAnswerInlineQueryMarkdown() {
521+
bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") // create new bot
522+
if err != nil {
523+
log.Panic(err)
524+
}
525+
526+
log.Printf("Authorized on account %s", bot.Self.UserName)
527+
528+
u := tgbotapi.NewUpdate(0)
529+
u.Timeout = 60
530+
531+
updates, err := bot.GetUpdatesChan(u)
532+
533+
for update := range updates {
534+
if update.InlineQuery == nil { // if no inline query, ignore it
535+
continue
536+
}
537+
538+
article := tgbotapi.NewInlineQueryResultArticleMarkdown(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
539+
article.Description = update.InlineQuery.Query
540+
541+
inlineConf := tgbotapi.InlineConfig{
542+
InlineQueryID: update.InlineQuery.ID,
543+
IsPersonal: true,
544+
CacheTime: 0,
545+
Results: []interface{}{article},
546+
}
547+
548+
if _, err := bot.AnswerInlineQuery(inlineConf); err != nil {
549+
log.Println(err)
550+
}
551+
}
552+
}
553+
554+
func ExampleAnswerInlineQueryHTML() {
555+
bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") // create new bot
556+
if err != nil {
557+
log.Panic(err)
558+
}
559+
560+
log.Printf("Authorized on account %s", bot.Self.UserName)
561+
562+
u := tgbotapi.NewUpdate(0)
563+
u.Timeout = 60
564+
565+
updates, err := bot.GetUpdatesChan(u)
566+
567+
for update := range updates {
568+
if update.InlineQuery == nil { // if no inline query, ignore it
569+
continue
570+
}
571+
572+
article := tgbotapi.NewInlineQueryResultArticleHTML(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
573+
article.Description = update.InlineQuery.Query
574+
575+
inlineConf := tgbotapi.InlineConfig{
576+
InlineQueryID: update.InlineQuery.ID,
577+
IsPersonal: true,
578+
CacheTime: 0,
579+
Results: []interface{}{article},
580+
}
581+
582+
if _, err := bot.AnswerInlineQuery(inlineConf); err != nil {
583+
log.Println(err)
584+
}
585+
}
586+
}

helpers.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,32 @@ func NewInlineQueryResultArticle(id, title, messageText string) InlineQueryResul
328328
}
329329
}
330330

331+
// NewInlineQueryResultArticleMarkdown creates a new inline query article with Markdown parsing.
332+
func NewInlineQueryResultArticleMarkdown(id, title, messageText string) InlineQueryResultArticle {
333+
return InlineQueryResultArticle{
334+
Type: "article",
335+
ID: id,
336+
Title: title,
337+
InputMessageContent: InputTextMessageContent{
338+
Text: messageText,
339+
ParseMode: "Markdown",
340+
},
341+
}
342+
}
343+
344+
// NewInlineQueryResultArticleHTML creates a new inline query article with HTML parsing.
345+
func NewInlineQueryResultArticleHTML(id, title, messageText string) InlineQueryResultArticle {
346+
return InlineQueryResultArticle{
347+
Type: "article",
348+
ID: id,
349+
Title: title,
350+
InputMessageContent: InputTextMessageContent{
351+
Text: messageText,
352+
ParseMode: "HTML",
353+
},
354+
}
355+
}
356+
331357
// NewInlineQueryResultGIF creates a new inline query GIF.
332358
func NewInlineQueryResultGIF(id, url string) InlineQueryResultGIF {
333359
return InlineQueryResultGIF{

helpers_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@ func TestNewInlineQueryResultArticle(t *testing.T) {
1616
}
1717
}
1818

19+
func TestNewInlineQueryResultArticleMarkdown(t *testing.T) {
20+
result := tgbotapi.NewInlineQueryResultArticleMarkdown("id", "title", "*message*")
21+
22+
if result.Type != "article" ||
23+
result.ID != "id" ||
24+
result.Title != "title" ||
25+
result.InputMessageContent.(tgbotapi.InputTextMessageContent).Text != "*message*" ||
26+
result.InputMessageContent.(tgbotapi.InputTextMessageContent).ParseMode != "Markdown" {
27+
t.Fail()
28+
}
29+
}
30+
31+
func TestNewInlineQueryResultArticleHTML(t *testing.T) {
32+
result := tgbotapi.NewInlineQueryResultArticleHTML("id", "title", "<b>message</b>")
33+
34+
if result.Type != "article" ||
35+
result.ID != "id" ||
36+
result.Title != "title" ||
37+
result.InputMessageContent.(tgbotapi.InputTextMessageContent).Text != "<b>message</b>" ||
38+
result.InputMessageContent.(tgbotapi.InputTextMessageContent).ParseMode != "HTML" {
39+
t.Fail()
40+
}
41+
}
42+
1943
func TestNewInlineQueryResultGIF(t *testing.T) {
2044
result := tgbotapi.NewInlineQueryResultGIF("id", "google.com")
2145

0 commit comments

Comments
 (0)