Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Chatbots are being used almost everywhere today from social messaging platforms to integration into websites for booking tickets, finding nearby restaurants, generating leads, buying and selling products. Some chatbots, like Ruuh by Microsoft have been able to deliver human-like conversations using artificial intelligence and deep learning.
Do you remember Natasha from Hike? I used it 4 years ago and was amazed to see how she handled our conversations which were so much better than a bot could possibly handle. I hadnât heard the concept of machine learning back then.
Now chatbots have made us so dependent on them that it has become a part of our lives today.
âOk Google, remind me on 20th to publish my chatbot articleâ
âWhat time?â
âMidnightâ
âSure, Iâll remind you on Wednesday at midnight.â
Chatbots are not only making our lives easier by managing our tasks but they are also becoming quite interesting to have conversations with.
âOk Google, tell me a joke.â
âHereâs a joke just in time for Valentineâs Day, I forgot to tell you I have a date for a Valentineâs Day. Itâs February 14th.â
But all these come at a cost of our data being stored and used for the companyâs benefits. Recently Google India tweeted asking âwhy do Indian users keep asking Google Assistant to marry them?â
Can we do anything about this? Most Probably No.
What if we could build our own chatbot?
We could add all the features we need and tweak them as per our likings.
So letâs build a chatbot which will help you get more productive at work as it runs on Slack. The chatbot we are about to build is in no way near to the Google Assistant. It is not even voice-enabled.
Slack is a messaging platform built for teams to collaborate and work with each other. It is the most common tool used in companies today for communicating with their employees.
Getting Started
Letâs build a chatbot together on Slack.
DISCLAIMER: This project was created by a team of 2 for a competition but unfortunately we couldnât make it to the finals.
This is our Architecture for our Slackbot.
And this is our Entity Relationship Diagram which will help you create your own database.
Clone the repository from GitHub.
Create a .env file in the /src directory of your project.
Install the requirements:
pip install -r requirements.txt
This is your main file: slackbot.py.
slackbot.py first imports all the packages required to run the Slackbot. It then initiates the slack client using your Slack API key stored in your .env file like this:
API_KEY=âYour Slack API Keyâ
It initializes the constants and tries to connect with the Slackâs RTM API and if it fails to establish a connection then it returns Connection failed with the error message printed above.
If the connection is successful, our slack client runs in an infinite loop and tries to read every second if any userâs message is received and if it receives any message, it extracts the channel id and the message text received from the Slackâs RTM API and further checks if the message received has any assigned command which can be processed to generate a response.
Features with code and Explanation
Song Lyrics
Our users can get lyrics for songs by passing in spelled or misspelled song names right from the Slackbot. This code snippet has already been defined in your slackbot.py file.
elif message.startswith("lyrics for "):
get_song_name = message[11:]
lyrics_gen = Song_Lyrics(settings.GCS_API_KEY, settings.GCS_ENGINE_ID)
song = lyrics_gen.get_lyrics(get_song_name)
response = '*' + song[0] + '*' + '\n\n' + song[1].replace('<br>','\n')
You need to create a Custom Search Engine ID by adding any or all of the following websites as per your choice:
- https://genius.com/
- http://www.lyricsted.com/
- http://www.lyricsbell.com/
- https://www.glamsham.com/
- http://www.lyricsoff.com/
- http://www.lyricsmint.com/
Note: For more information, you may look at the Lyrics Extractor Python Library.
After you get your Custom Search Engine ID, get a Google Custom Search JSON API key and you are good to go.
Our users can get audio and video version for songs by passing in spelled or misspelled song names right on their Slackbot.
This is your get_music.py.
After importing all the dependencies, It requires a YouTube Data API to fetch songs and extracts the first link from the search results received for spelled or misspelled song names.
Note: We have assumed our first YouTube search result to be the most accurate one for our users requesting for songs.
It then makes use of Pafy Python Library to extract the audio from the video link of the song. It requires a Bitly username and Bitly API key to shorten long URLs generated for streaming audio which expires within a few hours as well as shortens the YouTube video link for providing the video version of the song.
Note: You may find this article useful to install the Bitly Package from GitHub.
Live Scores for Football
Our users get notified about the latest scores for live football matches after every set time intervals. I have only selected top football leagues which fetch live matches for Premier League, Championship, Serie A, Primeira Liga, La Liga.
I selected only a few leagues as there are numerous matches live at the moment and sending scores for all the live matches would make no sense to the users.
Note: This is a subscription-based service so you need to set up a Database as per my shared schema at the beginning. You can then use a Schedule Python Library to schedule your live scores to be sent to the subscribed users after every set time interval.
You need to get the API key of Football Data API.
You can choose your favorite football leagues from the leagues offered in the Football Data API.
Here is your football.py file.
The live_football function fetches and extracts the live scores for the live football matches of the selected leagues stored in comp_id dictionary and returns a list of tuples for the live matches with the required information of both the teams.
When the user subscribes for live football scores from the Slackbot, our football_res class object stores the userâs channel id and our slack client API key which further verifies the userâs response and stores the selected leagues by the user in our Database and sends the follow-up confirmation response to the subscribed user with the latest scores for the live matches.
News
Our users will be updated about current affairs and breaking news daily so that they can be up-to-date about the current happenings.
This is your news.py file.
It requires a News API key to fetch the latest news and a Bitly username and Bitly API key to shorten long URLs.
Note: You may find this article useful to install the Bitly Package from GitHub.
It returns the News with the title, description, and the news link as a formatted message.
Note: This is a subscription-based service so you need to set up a Database as per my shared schema at the beginning. You can then use a Schedule Python Library to schedule your live scores to be sent to the subscribed users after every set time interval.
Tasks
Our users can schedule tasks in the Slackbot and it will remind them for the set task at the set date and time. This will help our users manage and complete their tasks on time leading to an increase in their productivity at work.
This is our task.py file.
If the user message starts with âremind me onâ then our parse_tasks function extracts the date, task description and time from the user message received and verifies whether the date and time provided are valid.
If everything is parsed correctly then the task gets stored in the tasks table in our database and our users get a confirmation message letting them know that the task is set with the formatted date and time for the event.
Note: You need to set up a tasks table in your database as per my shared schema at the beginning. When the current date and time are equal to the set date and time, then send the task to the assigned user.
Reminders
Users will be able to set reminders for birthdays and anniversaries of their colleagues and friends. This will help them stay connected and keep the communication going.
Here is our reminder.py file.
Our reminders module works similar to tasks but the only difference is reminders are sent every year whereas tasks are sent only once at the set date and time.
If the user message starts with âremind me onâ and does not contain time then our parse_reminders function extracts the date & reminders from the user message received and verifies whether the date provided is valid.
If everything is parsed correctly then the reminder gets stored in the reminders table in our database and our users get a confirmation message letting them know that the reminder is set with the formatted date for the occasion.
Note: You need to set up a reminders table in your database as per my shared schema at the beginning. When the current date is equal to the set date, then send the reminder to the assigned user every year.
Conclusion
Phew! We have finally come to an end. Congratulations on building your own Slackbot offered with some great features. Here is my Slackbot Github Repository.
There are many features like facts, quotes offered in the Slackbot which I havenât discussed in this article as their implementations were pretty straight-forward. There is also a help command provided to our users where they can know about all the available features and their assigned commands.
I would be glad to review your pull requests if you contribute in this open-source community to make this Slackbot a better one.
Also, do check out my Lyrics Extractor Python Library to get song lyrics by just passing in spelled or misspelled song names.
Thank you so much for taking the time to read this! If you enjoyed it, please give it a bunch of claps as itâll help more people see this story. And â this repository on GitHub if you liked this project. â€
A Guide to Building a Multi-featured Slackbot with Python was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.