In this article I will explain how to create a bot that allows you to be informed of the latest CVEs that are being published at the moment.
The first thing we have to do is to install our Telegram Pyhon library using pip.
pip install python-telegram-bot
For the development of our bot you have to make use of “The Botfather” (https://core.telegram.org/bots) which consists of an application created by Telegram that will act as a mediator between Telegram and our code.
To do this, you must access the “BotFather” channel through one of the platforms offered by Telefram (iOS, Android or Windows) or (Mac, Windows, Linux, web version).
In this case, I will use your web version (https://web.telegram.org/).
Once inside that channel, just put “/start” then “/newbot” and then enter the name of your bot, remember that it has to go just “_bot” or “bot”.
With the previous message, we will confirm that everything has been created correctly.
It is very important that we have our Bot in contacts because you will need to know the chat_id we have in common.
Surely there are ways to get this “chat_id” simpler but I will explain the one I use.
This Script will help you get the chat_id but for it to work you have to (previously) have talked to your bot (e. g. from the chat bot: t. me/XXXXXXXX_bot)
Remember to put your TOKEN previously obtained
# -*- coding: utf-8 -*- #Importamos liberia telegram import telegram #TOKEN de la API - Botfather TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' bot = telegram.Bot(token=TOKEN) updates = bot.get_updates() print([u.message.chat_id for u in updates])
The result would be something like that:
The chat_id will be in the style of 17XXXXX. This chat_id can also be a negative value (e. g. -17XXXXXX).
Now with your chat_id and TOKEN we can use this script to get the latest CVEs that are being published via Twitter.
# -*- coding: utf-8 -*- #Importamos liberia de Telegram y BeautifulSoup import bs4,telegram #TOKEN de la API - Botfather TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' mi_bot = telegram.Bot(token=TOKEN) #ID del chat de Telefram chat_id = 1000000000000 #Nombre de la cuenta que se quiere obtener los Twits account = "CVEnew" #Obtenemos el contenido de de la página de twitter result = requests.get("https://twitter.com/"+account) c = result.content #Nos ayudamos la librería BeautifulSoup soup = bs4.BeautifulSoup(c, "html.parser") for div in soup.find_all("li", {"class":"stream-item"}): #Obtención de ID único que usa Twitter item_id = div["data-item-id"] #Se obtiene el contenido del Twitt content = str(div.find("p", {"class":"TweetTextSize"}).text) #Se envia el resultado a mi chat de telegram mi_bot.sendMessage(chat_id=chat_id, text="@" + account + " -- " + content)
Remember that this Script is an example of how you get the Twits from the CVEnew channel and only collects the Twits, that is, you would need to implement a Cronjob that constantly returns these Twits and work under a database that controls the notifications received.