Merge pull request #49 from llorephie/master
Add support for Markdown/HTML in NewInlineQueryResultArticlebot-api-6.1
commit
7f37376fd0
68
bot_test.go
68
bot_test.go
|
@ -516,3 +516,71 @@ func ExampleAnswerInlineQuery() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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{
|
||||||
|
InlineQueryID: update.InlineQuery.ID,
|
||||||
|
IsPersonal: true,
|
||||||
|
CacheTime: 0,
|
||||||
|
Results: []interface{}{article},
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := bot.AnswerInlineQuery(inlineConf); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
26
helpers.go
26
helpers.go
|
@ -328,6 +328,32 @@ func NewInlineQueryResultArticle(id, title, messageText string) InlineQueryResul
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NewInlineQueryResultGIF creates a new inline query GIF.
|
// NewInlineQueryResultGIF creates a new inline query GIF.
|
||||||
func NewInlineQueryResultGIF(id, url string) InlineQueryResultGIF {
|
func NewInlineQueryResultGIF(id, url string) InlineQueryResultGIF {
|
||||||
return InlineQueryResultGIF{
|
return InlineQueryResultGIF{
|
||||||
|
|
|
@ -16,6 +16,30 @@ func TestNewInlineQueryResultArticle(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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", "<b>message</b>")
|
||||||
|
|
||||||
|
if result.Type != "article" ||
|
||||||
|
result.ID != "id" ||
|
||||||
|
result.Title != "title" ||
|
||||||
|
result.InputMessageContent.(tgbotapi.InputTextMessageContent).Text != "<b>message</b>" ||
|
||||||
|
result.InputMessageContent.(tgbotapi.InputTextMessageContent).ParseMode != "HTML" {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestNewInlineQueryResultGIF(t *testing.T) {
|
func TestNewInlineQueryResultGIF(t *testing.T) {
|
||||||
result := tgbotapi.NewInlineQueryResultGIF("id", "google.com")
|
result := tgbotapi.NewInlineQueryResultGIF("id", "google.com")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue