Analytics Vidhya
Published in

Analytics Vidhya

Build a Telegram bot in python to get sentiment for a particular cryptocurrency using data from reddit.

Introduction

One of the things we want to know when dealing with cryptocurrencies is the worldview; do the world think it is a bad idea or is everyone happy about it. That there is sentiment.

So in this tutorial we would be walking through the design of building a telegram bot that can accept a cryptocurrency and based on data crawled off reddit tell us the current sentiment.

Terms

  • A Bot is a piece of software system that performs automated tasks. So our telegram bot could receive and send messages through telegram. Just like chatting with a friend on telegram.
  • Sentiment Analysis is a Natural Language technique to get whether a statement is positive, negative or neutral.

Reddit is the data source.

Here is the link to the complete code repository.

Pre-requisites

  • Knowledge of Python (not an advanced knowledge, but basic understanding of functions).
  • A Reddit account (you can just create one if you don’t have using google) and a little bit of familiarity.
  • Be using Telegram if you want to implement the Telegram bot part. In case you do not want to, you could build a web app on top of the functionality or any other client.

Design

USE CASE:

A user sends a cryptocurrency name to the telegram bot. The telegram bot receives the message, does the crawling on the data sources, then sentiment analyses, then we generate the report for the user.

PS: This is an ideal scenario, a real life scenario could be more complicated.

So we have some design choices and principles we use:

Choice Data Source: Cos of Internet Restrictions I can only crawl Reddit data. (Twitter would be a good addition, so make sure to add that).

Programming Paradigms: I have chosen functional programming. Other options you could go for include the Object Oriented Programming, Imperative Programming etc. But let’s stick to functional programming approach together.

Separation-of-Concerns (SoC): the crawler script only crawls, sentiment analyser only analyse etc. This helps with extensibility like when I realized the twitter crawler wasn’t working cos of Internet Restrictions.

Our final flow

our app flow

The fancy boxes are the services we extracted from our app flow. The TELEGRAM_BOT LOGIC, REDDIT_CRAWLER LOGIC, and SENTIMENT_ANALYZER LOGIC (or services)

Building the Services

So we have extracted the services we would be building:

  • TELEGRAM _BOT LOGIC: it is the one that helps us to set up telegram chat with users where we can accept their input and send them the report of our sentiment analysis.
  • REDDIT_CRAWLER LOGIC: It is the one responsible for taking in the keyword and crawling reddit looking for post that are related to the keyword
  • SENTIMENT_ANALYSER LOGIC: It is the one that takes in different sentences and infers whether it is positive, negative or neutral.

Let’s build.

Create a folder in Visual Studio Code (or your fav IDE)

CREATING REDDIT_CRAWLER

Step — 1 Get Access:

  • Go to Preferences (reddit.com) then select create another app at the bottom
  • Fill in the details. Select (script). Redirect url (http://localhost:8080) and click Create App.
  • On Creation you get a Dialog box. Take note of the personal use script and the secret, they are your client id and secret key respectively.
  • add a .env to the folder and add the fields to the file. It looks like this.
REDDIT_USERNAME = {see developers}
REDDIT_SECRET_KEY= {secret key}
REDDIT_CLIENT_ID={client key}

Step — 2 Installing some packages

so run:

pip install praw
pip install python-dotenv

Step — 3 Writing the logic

We would be creating a function that:

  • takes in a keyword (cryptocurrency name)
  • search the Cryptocurrency Subreddit for all occurrences over a week.
  • append the post to a list and ignore that load more comments option we do see that elongates the thread.

Step — 4 Let’s test

I created a run.py file to be the entry point of the app, so here

run a python run.py to see the result .

We have our reddit data. Onto the sentiment analyzer

BUILDING THE SENTIMENT ANALYZER

Step — 1 Installing Library

Sentiment analyser is a technique under natural language processing and there are various techniques to achieve it. But for this purpose, we would be using TextBlob.

TextBlob determines the sentiment of a sentence using Rule-based Method, there are other techniques like using deep learning algorithms.

pip install textblob

Step — 2 Discussion and Writing Code

This is how TextBlob works:

Now since we are dealing with a list of sentences we keep track of the ones with negative sentiments, positives and the neutral, then the overall sentiment is the one with the highest percentage. (sticking with simple inference).

So if we analyse 4 posts, 2 is positive, 1 is negative and 1 is neutral. Overall sentiment is positive.

our code:

I have written some other functions like

  • line 17: get_sentiments(dataList, provider), get a list of data and also the provider. I added the provider caveat in an event that we are getting data from several sources like reddit, twitter and all to help us generate a comprehensive report.
  • line 6 -7: clean_text(text) removes all special characters from a string
  • line 9 -15: maximum(pos, neg, neu)takes in the number positives, negatives and neutral and returns the maximum in textual definition — if it maximum number is the positives return POSITIVE.

Step — 3 Let’s Test

Let’s update the run.py file. We would be using some dummy list of sentences to ensure it performance.

You would see our functional programming approach allows us to test each functionality without bothering about the intricacy of other functions. So our sentiment_analyzer.py doesn’t need to be aware of the reddit_crawler.py

run this to see the result.

Sentiment_analyzer Check

BUILDING THE TELEGRAM BOT

Step — 1 Getting Access

For us to build have access to the telegram API we need the Token and we can get it from BotFather (think father of all chatbots).

So:

  • Search/Contact BotFather here Telegram: Contact @BotFather
  • send /new as a message
  • Fill in the details and you should get your API TOKEN
  • Copy the token into the .env file to have something like this:
TELEGRAM_TOKEN = xxxxxxxxxxx
  • you should also get a chatId for your bot, something like t.me/{name_of_bot}. This is the bot you would be chatting with.

Step — 2 Installing libraries

There are a number of python libraries we could use to access the Telegram API, but for this purpose, I chose the one by eternnoir.

pip install pyTelegramBotAPI

Step — 3 Logic and Discussion

Now we are only interested in three functionalities for our telegram bot:

  • send a welcome message to display what the bot does and instructions.
  • receive user’s response which in this case is a cryptocurrency name.
  • sends back the response to user.

Here is the code for this part:

  • line 7 is all about setup
  • line 10–12 is just a welcome sequence
  • line 14–18 is where we take in a message and reply
  • line 20 is what keep the bot running on and on; the name

now on run.py, update the code to

I know you are running it locally, but if you go to your telegram and chat up at t.me/{name_of_bot} you would interact with it very fine.

It should give you all the messages we have hardcoded.

But that’s not what we want.

Setup — 4 Putting it all together

We already have our previous services to get data and do sentiment analysis on it.

Thus what we really want is to take the cryptocurrency name on line 15 (message parameter), and between line 16 and 18 put in our logic to get data from reddit, and do a sentiment analysis. Good thing we have both already.

So let’s write our final implementation.

and update our entry file, the run.py

step — 5 Let’s test everything

Run

python run.py

Then bring up your telegram and chat up your robot. Here is a sample result from me.

Deployment

Basically what we are looking for is server that allow us to run a script forever, if we want to keep out telegram bot always online. There are several paid options.

An option I found to run it for free was from this YouTube named Frank. Check him out.

Here is the link to my bot running on Replit

Here is the telegram link to chatting with my bot. (PS: I might have taken this bot offline when you are reading this tutorial)

Here is the link to the code repository on GitHub.

Advancement and Addition

We have a reasonable starting point, but there are things that could be added.

Multiple Sources: I have only used reddit as my source, but we could include other social media sources like twitter, Facebook, even public forums.

Verifying User Input: For the Telegram Bot, we are just taking in the user input as the insert it, no verification or validation which is not at all secured. In a future addition we would want to validate that it is a text, that is a coin and other validation we might think of.

Using a more advanced Sentiment Analyzer: We have chosen TextBlob as our sentiment analyser but it is most sophisticated one. You could choose to use a more sophisticated and accurate one. Also you might actually build your own sentiment analyser from scratch.

Conclusion

In this tutorial we build a telegram bot that could take in a cryptocurrency name, gather data from reddit and do sentiment analysis on it based on the data and help us generate report.

But it is more than that. We have actually build an extensive system and body of knowledge that could be extracted on its own. We could write an enter crawler for other data in reddit or we could extend our data to other sources. We could also use our telegram bot logic as a starting point for building other kinds of bot.

I hope this was an insightful read.

Once again, Here is the link to the code repository on GitHub.

Here is the link to my bot running on Replit

--

--

--

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

Recommended from Medium

Running Cassandra using docker in Apple Macbooks with M1 chip

Our first project in Masai School

Time travel and Schema adaptation in Databricks

[Leet Code] Find N Unique Integers Sum up to Zero

The Routing Table setup !

Dart for Flutter : Mixins in dart

Andy just had an episode He started to collapse and go unconscious when he was moving from his…

Python List Comprehension

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mr. Simi

Mr. Simi

.Software Engineering ♤ Web3 ♧ Robotics □ AI ○Blockchain. Research.

More from Medium

How to analyze Opensea NFTs market data using Python and Pandas

How-to setup your Trading Platform — Part I

Collect crypto data on telegram using Python

Introduction to Technical Analysis in Python using TA-Lib