Like A Girl

Pushing the conversation on gender equality.

Code Like A Girl

Myra: A Smart Zulip Chatbot

Making chat bots the Zulip way

Zulip Logo

About Zulip

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 interact with a Zulip realm, Zulip provides a Python and JavaScript API. Using this API, a bot can communicate with any Zulip realm. The documentation for the API can be found here.

Getting Started

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.

  1. Clone the python-zulip-api repository.
    git clone https://github.com/zulip/python-zulip-api.git
  2. Change directory to cloned repository.
    cd python-zulip-api
  3. Make sure you have a working internet connection to setup a virtual environment and install all the requirements.
    ./tools/provision
  4. Finally activate the virtual environment.
    source zulip-api-py3-venv/bin/activate

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.

Directory Structure

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.

bot.py

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

__init__ function

5. subscribe_all() function: This will subscribe and listen to messages on all streams.

Subscribe all function

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:

Translate messages received in different languages using out Zulip translate integration.
myra translate "any language"

HackerNews:

Get top 10 stories of hackernews in no time in your Zulip server.
myra HN

Cricket Scores:

Know all about cricket scores without leaving Zulip.
myra cricnews

Lyrics:

Find the lyrics to your fav song and jam to the beats!
myra lyrics "artist" "song"

Movie:

Find the details to your fav movie !
myra movie "title"

Holidays:

Find the list of all the holidays!
myra holiday

Currency:

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