Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 21, 2020 07:21 pm GMT

DEV Telegram

Here at DEV we roughly follow the Shape Up product development methodology. This includes regular downtimes and during the current one, I built a Telegram bot as a fun little side project.

Meet @devto_bot

Our bot's first feature has been implemented inline, similar to how the IMDB or Wikipedia bots work.

You can find it at @devto_bot, but you don't really need to do that since it's automatically available in all your private and group chats. Don't worry, our bot runs in privacy mode so it won't be reading your messages.

To use it simply mention @devto_bot, optionally followed by a tag name. This will bring up a list of the 5 most recent articles in that tag. While this is not quite as helpful as I'd like, our API doesn't currently support article search. Once we've added that I'll update the bot accordingly.

Let's see it in action:
bot video

The implementation

The bot is written in Crystal, using the Tourmaline Telegram bot API framework.

This is the main part of the code:

require "tourmaline"require "./api_client"class DevtoBot < Tourmaline::Client  VERSION = "0.1.0"  @[On(:inline_query)]  def on_inline_query(client, update)    tag = update.inline_query.try(&.query)    articles = ApiClient.articles(tag)    results = build_results(articles)    update.inline_query.try(&.answer(results))  end  private def build_results(articles)    InlineQueryResult.build do |builder|      articles.map do |article|        builder.article(          id: article.id.to_s,          title: article.title,          thumb_url: article.social_image,          input_message_content: InputTextMessageContent.new(article.url),          description: article.description        )      end    end  endend

We receive the tag via an inline query and use it in the request to the DEV API. We then use Tourmaline::InlineQueryResult::Builder to turn the resulting objects into query results for the bot's popup.

The API client uses HTTP::Client from Crystal's standard library and is very minimal:

require "http"require "./article"class ApiClient  API_URL = "https://dev.to/api/%s?%s"  def self.articles(tag)    query = HTTP::Params.encode({per_page: "5", tag: tag})    url = sprintf API_URL, "/articles", query    response = HTTP::Client.get(url)    Article.array_from_json(response.body)  endend

The Article class defines a mapping between JSON attributes and a Crystal object.

require "json"class Article  # Only map the few attributes we need  JSON.mapping({    id:           {type: Int32},    title:        {type: String},    url:          {type: String},    social_image: {type: String?},    description:  {type: String?},  })  def self.array_from_json(json)    Array(self).from_json(json)  endend

The bot is hosted on Heroku with the Crystal buildpack. We use the following app.cr and Procfile:

require "env"require "./src/devto_bot"token = ENV.fetch("TELEGRAM_BOT_TOKEN")bot = DevtoBot.new(token)bot.set_webhook("https://.../bot-webhook/#{token}")bot.serve("0.0.0.0", ENV.fetch("PORT").to_i)
web: ./app --port $PORT

Once I clean up the code a bit and add some basic documentation I'll make the repository public.

Your turn

How about you, do you use Telegram? Do you have ideas for other features you'd like to see from this bot? Or is there some other platform you would really like to see a DEV bot for? Let us know in the comments!


Original Link: https://dev.to/devteam/dev-telegram-efb

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To