Powering applications with a Notebook Microservice

Previously, we learned how to create a microservice from a notebook using the Jupyter kernel gateway. We learned both how to annotate existing cells, as well as how to generate a new notebook with code cells ready to be filled-in. Now let’s look at this from the other direction: breaking down a problem into the microservices needed to implement its solution.

For this post, we’ll solve a problem that’s been solved thousands of times before: making a Twitter bot. Specifically, when the bot is mentioned along with a location, it should tweet a search URL for relevant Meetups in that vicinity. Breaking it down further, the tweeted mention has to get to the bot, the bot has to figure out what location is being talked about, then it has to tweet in a way that the original person will see the correct search URL. To see the only code we’ll actually write in the making of this bot, check out the microservice kernel gateway demo on GitHub.

Connecting Twitter to the bot

Keeping with our theme, instead of writing a full Twitter application we’ll be using IFTTT.com. IFTTT creates connections between services, calling them Recipes. We will be employing its ties to Twitter and Maker, which it refers to as Channels. Of course, we’ll want new, different IFTTT and Twitter accounts for our new bot.

For this example, we’ll use the meetup_service.ipynb notebook from the kernel gateway demos on Github. It contains one endpoint marked up for our use: /generate_search_url.

A Twitter-Maker recipe

This part requires a Recipe with the trigger Channel of Twitter, the Trigger of a “New mention of you”, and an Action Channel of Maker with “Make a web request” to the service URL you’ll be standing up, making sure to pass along the text of the tweet and the original Twitter handle:

https://kernel-gateway-microservice-demo.mybluemix.net/generate_search_url?text=<<<{{Text}}>>>&user={{UserName}}

Getting the location and making the search URL

Getting the location from the tweet’s text requires a considerable amount of language processing, and since we don’t want to solve that problem again, we’ll make use of the Entity Extraction API service from AlchemyAPI, Inc. While the notebook was written using a Python kernel, a less contrived service could require a kernel for a different language, run computations through a data scientist’s model in the notebook, or help leverage an existing cluster’s capabilities. In this case, it takes the result from calling Alchemy with your API key, prepares a URL to be tweeted, and then calls another service to have it tweeted. Being written for Python also makes it trivial to deploy our contrived service as a pure Python application on a PaaS, such as IBM Bluemix, which you may have guessed from the URL in the previous step.

Tweeting the result

For the last part, we’ll again make use of IFTTT.
A Maker-Twitter recipeThis time, we need a Recipe going from Maker, with a Trigger “Receiving a web request” at an “Event name” search_url_generated, to Twitter, and an Action to “Post a tweet” with the text:

.{{Value1}} you can find meetups near {{Value2}} at {{Value3}}

The values will be filled in with the original Twitter handle, the search URL, and the extracted location name.

The notebook will require your Maker Channel key to send the right web request. Like the Alchemy API Key, this would be set into the environment around the running Jupyter kernel gateway. If those were correctly set, you’re already done.

Twitter timeline with followup

Learn More

Nitin Dahyabhai

Nitin is a member of the IBM Cloud Emerging Technology group.

Nitin Dahyabhai
Nitin Dahyabhai