Photo by Igor Shabalin

Get Latest Business Information via Your Own Channel in a Messenger

Yuli Vasiliev
Analytics Vidhya
Published in
4 min readJun 16, 2020

--

The process of finding necessary information in the internet manually can be quite time consuming. This article discusses how you might tune your own channel in a messenger to automatically get latest information on the topics of your interest. The idea is the following: you create a bot that will collect information for your channel and then connect this bot to the channel. You need to make the bot available for everyone, so that the results of bot users’ requests come to your channel.

This approach allows you to discover the latest trends, since you will receive the information in which the users of your bot are interested. For example, if you notice that many users are recently interested about Tesla stock, it may encourage you to take a closer look at it. And due to your channel, you’ll have this information in the form of the latest articles written by stock analysts on this topic.

How It Works in General

The procedure of creating a news channel is quite straightforward. In general steps, it can be described as follows:

  1. Employing a news search API
  2. Creating a bot in a messenger
  3. Connecting the bot to a channel

As a news search API, you might use News API: https://newsapi.org/, allowing you to programmatically search for latest articles on the topic of interest from a wide range of news sources and blogs. To use the API, you’ll need a key. The developer key is available for free. If you want to look at an example on how to make a request from Python with News API, refer to my previous Medium article: Getting Straight to the Point with Scraping and Natural Language Processing

As to creating a bot, this can be easily done on the Telegram bot platform. Telegram is a cloud-based messenger and one of the top messenger apps in the world. In the following section, you’ll see the skeleton of the implementation for such a bot. Some further details can be found in my previous article mentioned above. For details on how to deploy Telegram bots, visit the Bots: An introduction for developers page at https://core.telegram.org/bots.

Telegram allows sending content from bots directly into any chat, group or channel. You can create a Telegram channel with a few mouse clicks as described on the Telegram Channels page at https://telegram.org/tour/channels. You might also want to check out Channels FAQ at https://telegram.org/faq_channels.

Once you have a channel, you may use it as a means of transmitting information to many people, who will want to join your channel.

Implementing a Bot

As you might guess, implementing a bot is the most complicated part here, since you will need to write some programming code and deploy it to the Telegram developer platform. In particular, you’ll need to implement a bot that uses a news search API to get the latest articles on the specified topic. Also, the bot is supposed to use some natural language processing (NLP) techniques to filter out the articles, choosing the most relevant ones. Below is the skeleton of such a bot:

from telegram.ext import Updater, MessageHandler, Filters
def input_handler(update, context):
inpt = update.message.text
keywrd = inpt + ' ' + 'stock'
keywrd_syn = inpt + ' ' + 'shares'
answers_stock = []
#Sending a request to News API

#Filtering the results of the request

for ent in doc.ents:
if (ent.label_ == 'MONEY') or (ent.label_ == 'PERCENT'):
answers_stock.append(sent.text.strip() + '| '+ article['publishedAt'] + '| '+ article['url'])
break

#Sending the filtered results of the request to the channel
context.bot.send_message(chat_id=********, text=output)

def main():
updater = Updater(‘TOKEN’, use_context=True)
updater.dispatcher.add_handler(MessageHandler(Filters.text, input_handler))
updater.start_polling()
updater.idle()
if __name__ == ‘__main__’:
main()

A Working Example of the Bot/Channel Implementation

I’ve implemented the bot that retrieves links to the latest articles about the stock of a corporation. You can find the bot at https://t.me/stocknewstip_bot. Before you can try it, you’ll need to sign up for a Telegram account. After signing in to Telegram, find the bot by clicking the Telegram Search and typing in @stocknewstip_bot. To start a conversation with the bot, type in /start. After that, you can type in the name of a corporation, as illustrated in the screenshot below:

As a result, three most relevant articles will be outputted:

The articles shown in the bot may then also appear in the https://t.me/stocknewstip channel connected to the bot. Note, however, that the system does not allow the same article to appear in the channel. So, some links you may see in the bot will not necessary appear in the channel.

--

--

Analytics Vidhya
Analytics Vidhya

Published in Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Yuli Vasiliev
Yuli Vasiliev