paint-brush
A Guide to Building a Multi-featured Slackbot with Python by@rishabha.1999
9,267 reads
9,267 reads

A Guide to Building a Multi-featured Slackbot with Python

by Rishabh AgrawalFebruary 13th, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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 <strong>Ruuh by Microsoft</strong> have been able to deliver human-like conversations using artificial intelligence and deep learning.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - A Guide to Building a Multi-featured Slackbot with Python
Rishabh Agrawal HackerNoon profile picture

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 . 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.

Image Credit: Sweta Sharma

And this is our Entity Relationship Diagram which will help you create your own database.

Image Credit: Sweta Sharma Clone the repository from .

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 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 by adding any or all of the following websites as per your choice:
  • //genius.com/
  • //www.lyricsted.com/
  • //www.lyricsbell.com/
  • //www.glamsham.com/
  • //www.lyricsoff.com/
  • //www.lyricsmint.com/

Note: For more information, you may look at the Python Library.

After you get your Custom Search Engine ID, get a API key and you are good to go.

Get Audio & Video for a song

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 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 to extract the audio from the video link of the song. It requires a 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 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 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 . You can choose your favorite football leagues from the leagues offered in the .

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 key to fetch the latest news and a to shorten long URLs.

Note: You may find 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 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

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 a better one. Also, do check out my 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. ❤

바카라사이트 바카라사이트 온라인바카라