Kogno
  • Introduction
  • Getting Started
    • Configuration
    • Starting the Server
    • Messenger Configuration
    • Telegram Configuration
    • WhatsApp Configuration
    • NLP Configuration
  • Conversation Class
  • Contexts
    • Blocks
      • before_anything
      • postback
      • deep_link
      • command
      • any_attachment
      • regular_expression
      • keyword
      • any_number
      • any_text
      • intent
      • entity
      • membership
      • recurring_notification
      • everything_else
      • after_all
    • Sub Contexts
    • Routing
    • Sequences
    • Conversational Forms
  • Replies / Notifications
    • text
    • button
    • quick_reply
    • raw
    • list
    • carousel
    • url
    • typing
    • image
    • video
    • html
    • markdown
    • contact
    • location
    • recurring_notification_request
    • messenger_generic_template
    • whatsapp_template
  • Templates
  • Models
    • User model
  • Scheduled Messages
  • Telegram Inline Query
  • Command Line
  • Global Methods
  • Internationalization
Powered by GitBook
On this page
  • Configuration
  • Send Answers
  • inline_query_result(type=Symbol, answer=Hash)
  • Params
  • Usage
  • How would it look?
  • Shared content

Was this helpful?

Telegram Inline Query

Allows you to receive and answer an inline query from Telegram.

PreviousScheduled MessagesNextCommand Line

Last updated 2 years ago

Was this helpful?

Read more information about inlineQuery , and before starting, it is necessary that this mode needs to be .

Configuration

Define the context that will receive inline queries, by modify the field bellow in 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

Usage

When an inline query arrives, the configured context will handle it, through that capture text messages such as , , , 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.

Required. The answer, that varies depending on the type defined, read about the response formats for each type on .

Learn more about html_template() method .

here
enabled in Telegram
config/platforms/telegram.rb
action blocks
keyword
intent
entity
any_text
Telegram documentation
here