Kogno
Search
⌃K

Telegram Inline Query

Allows you to receive and answer an inline query from Telegram.
Read more information about inlineQuery here, and before starting, it is necessary that this mode needs to be enabled in Telegram.

Configuration

Define the context that will receive inline queries, by modify the field bellow in config/platforms/telegram.rb configuration file.
config.routes.inline_query = :main

Send Answers

In order to send answers to an inline query, call @reply.inline_query_result() method:

inline_query_result(type=Symbol, answer=Hash)

Params

Name
Description
type Symbol
Required.
Can be article, audio, contact, game, document, gif, location, mpeg4_gif, photo, venue, video or voice.
answer Hash
Required. The answer, that varies depending on the type defined, read about the response formats for each type on Telegram documentation.

Usage

When an inline query arrives, the configured context will handle it, through action blocks that capture text messages such as keyword, intent, entity, any_text and so on.
In the next example, we've created a context called NewsContext, which has been configured to handle inline queries as follows:
config.routes.inline_query = :news
This context will call two keyword blocks with the arguments "nytimes" and "cnn" respectively. Each of them will return news extracted from the RSS service from the The New York Times or CNN.
class NewsContext < Conversation
def blocks
keyword "nytimes" do
feed_entries("https://rss.nytimes.com/services/xml/rss/nyt/World.xml")[0..10].each do |article|
@reply.inline_query_result(
:article,
{
title: article.title,
description: article.summary.to_s,
url: article.url,
thumb_url: article.image.to_s,
photo_width: 128,
photo_height: 128,
input_message_content: {
message_text: @reply.render_html_template(:news, :preview, {article: article}),
parse_mode: "HTML"
}
}
)
end
end
keyword "cnn" do
if @msg.type == :inline_query
feed_entries("http://rss.cnn.com/rss/edition_world.rss").each do |article|
@reply.inline_query_result(
:article,
{
title: article.title,
description: article.summary.to_s,
url: article.url,
thumb_url: article.image.to_s,
photo_width: 128,
photo_height: 128,
input_message_content: {
message_text: html_template("news/preview", {article: article}),
parse_mode: "HTML"
}
}
)
end
else
@reply.text "This example only works in Inline Mode for Telegram"
end
end
end
protected
def feed_entries(url)
xml = HTTParty.get(url).body
feed = Feedjira.parse(xml)
return feed.entries
end
end
To implement this example you'll need to add the gems feedjira and httparty to the project's Gemfile.

How would it look?

Shared content

In the example above, html_template("news/preview", {article: article}) has been called, this method loads a template from bot/templates/news/preview.rhtml with the following code:
<b><%=article.title%></b>
<i><%=article.summary.to_s.truncate(50)%></i>
<a href="<%=article.url%>"> Read more </a>
This content is what the person with whom the user is sharing the article will receive.
Learn more about html_template() method here.