Pulling, formatting and publishing a cocktail recipe every few hours is fine, but somewhat limited. It serves well enough as a source of inspiration when you have no idea what to make, but what if you need the recipe of a particular cocktail at a particular moment? Or even better, what if you’re craving a cocktail based on that one ingredient you have at home?
With the excuse of @CocktailsBot surpassing 300 followers and 1000 tweets, I decided to extend its functionality by adding a couple of interactive commands:
- Make me something with
<ingredient>
. - Make me a
<cocktail>
.
The goal of this is enabling users to actively affect the bot’s behavior, and turn it into a dual-channel mean of communication instead of a mere emitter. With these new features, every time a Twitter user mentions @CocktailsBot with one of these two patterns, the bot will parse and interpret the query, try to satisfy it, and answer accordingly.
After all, if drinking is a social activity, my bot should also be able to socialize 🙂
Implementation
Since I already had most of the logic for pulling, formatting and tweeting cocktails implemented, extending the bot with new functionality was relatively straightforward. I started by making the following modifications to the CocktailsBot
class:
- I added new API endpoints and grouped all of them into a dictionary. Each endpoint is used for a different mode (and the modes are
random
,ingredient
andname
). - I parametrized existing methods like
get_cocktail
andpost_tweet
to act differently depending on the mode that was passed. - I modified
post_tweet
to be capable of posting cocktails as a consecution of replies to some other tweet.
With the base class ready to support the new features, I implemented CocktailsBotInteractive
, a class that inherits from CocktailsBot
and adds a few new methods.
The main difficulty in the new logic is the fact that the process now needs to actively listen for new mentions. Unlike the original application, the interactive version needs to run permanently, pulling new information constantly.
Before Twitter decided to redesign its API, they supported data streams, which could easily be coupled with Python iterators. This way, new mentions would be delivered as they happened, and the listening process would be passive. However, with the new version of the API this feature has been deprecated, so now it’s necessary to follow a polling approach.
The following items sum up the logic followed in the implementation:
- Every 20 seconds (the used endpoint,
mentions_timeline
supports a maximum of 75 queries per 15-minute time window), the last mentions are pulled. - Since it’s not possible to only pull unseen results, I need to keep track of the mentions I have already processed. For this, I use a MongoDB collection, in which I store the IDs of the processed tweets.
- Every tweet is tokenized and analyzed, to discern whether there is a query (and if so, what are its parameters) or just a regular mention.
- Methods of the superclass
CocktailsBot
are used to pull, format and post a cocktail, just as in the non-interactive version of the application.
Since the process is going to be running at all time, it’s important to make it fault tolerant. If one query is not formatted correctly or if it doesn’t throw any results (just to mention a few possible outcomes), an exception could bring the whole application down, becoming unavailable to future queries. To prevent this from happening, try-except
blocks and fallback messages have been implemented.
Last but not least, I also used this occasion to rewrite and improve the emoji dictionary, with hopes of making it more exhaustive.
Result
For now, the new @CocktailsBot (which I guess we could call v.2.0) is running under very close supervision, since it is still early to be sure that I’ve implemented mechanisms contemplating the whole casuistry.
Here’s an example of the feature working as expected:
Give it a try, and don’t doubt on giving me feedback, whether on Twitter (@lxbbc) or through my website!
With this occasion I’ve tidied up and released CocktailsBot’s code on Github as open source. Check it out in the following link:
30 thoughts on “Extending @CocktailsBot with interactive commands”
Comments are closed.