Myra: A Smart Zulip Chatbot
Making chat bots the Zulip way
Zulip is a powerful open source group chat application which gives a tough competition to its competitors such as Slack, Discord, etc.
The Python Zulip API
To set up the development environment for creating a Zulip bot, you need to have the a clone of python-zulip-api repository. Follow the steps to set up the environment.
- Clone the python-zulip-api repository.
git clone https://github.com/zulip/python-zulip-api.git
- Change directory to cloned repository.
- Make sure you have a working internet connection to setup a virtual environment and install all the requirements.
- Finally activate the virtual environment.
Creating a Zulip Realm
A Zulip realm is required so that the chat bot can interact with the users present in that realm.
Head over to https://zulip.com/new/ to create a new zulip realm.
Creating a bot in Zulip
To create a Zulip bot, you’ll have to create an organisation.
Creating an organisation and bot
Login to your Zulip and create an organisation from the settings menu. Then navigate to the settings pane > click on the ‘Your Bots’ option > go to ‘Add a new bot’ option and create your ‘generic bot’.
Download the zuliprc file. This is the file which contains all the details about all the configurations of your bot so make sure you keep it secure. Do not share this file with anyone.
You’ll need a bot.py file and the .zuliprc file in a folder. You can use seperate .py files to create classes for specific functionalities.
- Install pprint, zulip, sys, re, json, httplib2, os packages using pip.
2. Import the above mentioned packages into your file.
3. The file needs to know where to look for new messages. Define the variable
BOT_MAIL = “<your chat-bot name>@<organisation name>.zulipchat.com”
4. Now the main part is to define the zulipBot class. Inside the class, define constructor function __init__. This will contain the details about the client, calling the subscribe_all() function and an array which will contain all the keywords to look out for.
5. subscribe_all() function: This will subscribe and listen to messages on all streams.
6. process() function: Now that the bot is listening to messages on all the streams of your organization, the process function will process each message and call different functions based on conditions.
7. The if condition here makes sure that the bot doesn’t respond to its own messages. We want the bot to respond to ‘myra’ so we check if the 0 index of the array has the word myra in it or not. If it does we check for the second word of the array as our function keyword. In this case we are invoking the translate function. The rest of the array is saved as the argument to be translated. Now from Zulip documentation the send_message function can be reviewed which sends the message to the specific recipient.
8. Translate function
Here Google Translate API is used to translate the content using the Translator() and the message is returned. Similarly, other APIs could be implemented as separate files and imported in bot.py.
7. main() function
In the main function an object of class ZulipBot will be created and this will call bot. Process whenever it listens to a new message.
Running your bot
Navigate to your project directory, open the terminal or command prompt and run this bot.py file: python bot.py
Now, let’s have a look at its functionality…
Myra Bot can:
- Translate any language
- Get top news from HackerNews
- Find the lyrics to any song
- Get realtime currency conversion rates
- Find details of your favourite movie
- Find the list of holidays
- Stay updated with cricket scores
Translate messages received in different languages using out Zulip translate integration.
myra translate "any language"
Get top 10 stories of hackernews in no time in your Zulip server.
Know all about cricket scores without leaving Zulip.
Find the lyrics to your fav song and jam to the beats!
myra lyrics "artist" "song"
Find the details to your fav movie !
myra movie "title"
Find the list of all the holidays!
Get the realtime conversion rates!
myra currency "curr1" "curr2"
Peace out peeps!
Check out this github repo for more-https://github.com/aayushjoglekar/HINT2018-Project