Tropo is part of CiscoLearn More

Scaling Your Twitter Support, Part 2: Triggering Alerts on Keywords

Posted on December 9, 2010 by tropo

twitterlogo-shadow-2.jpgIf you want to scale your usage of Twitter and do more than just set up a “night service” (discussed in Part 1 and Part 1a), how about we create an app that triggers alerts based on keywords that appear in replies or mentions directed to your account?

In our Developer Jam Session webinar last week (recording and slides available), I demonstrated this app, but I thought I’d walk through it here. To use this app, you need a (free) Tropo account and you need a Twitter account to which you can attach the app.

NOTE: If you are not sure about how to link Tropo apps to Twitter, you may want to visit this step-by-step walkthrough first.

The code for this example is available from Github and you can use the raw URL if you want to get a version you can easily copy/paste.

Once attached to a Twitter account, the app will receive all replies (messages starting with your Twitter ID) or mentions (messages including your Twitter ID) and act as follows:

  • Convert the message to all lowercase for easier matching.
  • If the tweet includes the word “fail”, send out a SMS to someone so that they can know to go online and look at the Twitter stream.
  • If the tweet is a “reply” and includes the word “help”, sind back a message with a URL and send out a SMS alert.
  • If the tweet is a reply and includes the word “faq”, reply back with the URL to a FAQ.
  • If the tweet is a reply and includes the word “documentation”, reply back with the URL to the documentation.

Obviously, you could make it do more… and make it a bit smarter – for instance to also search on “docs” – but this is mostly a demo of what you can do.

Now, to use this app yourself, you’re going to need to do two things:

  1. Replace phone number with your cell phone.
  2. Replace Twitter ID (“tropohelp”) with your Twitter ID

Here is what the full source code looks like. I’m using Python, because that’s how I roll, but you could apply the same ideas in other languages.

text = currentCall.initialText.lower()

if text.find("fail") > -1:
message("Whoa! " + currentCall.callerID + " is tweeting a fail", {"to":"tel:+14075551212", "network":"SMS"})

elif text.find("@tropohelp") == -1:

if text.find("help") > -1:
say("Tropo lets you build apps that interact via voice, SMS, IM and Twitter. See https://www.tropo.com/ for more.")
message("Tropo help request from: " + currentCall.callerID, {"to":"tel:+14075551212", "network":"SMS"})

elif text.find("faq") > -1:
say("The Tropo FAQ is at https://www.tropo.com/docs/scripting/faq.htm")

elif text.find("documentation") > -1:
say("Tropo docs are at https://www.tropo.com/docs/")

Now lets walk through it. First, I take currentCall.initialText, which contains the tweet message, and convert it to lowercase and put it in the text variable.

text = currentCall.initialText.lower()

Next, if that tweet includes the word “fail”, I’m going to use the message command to send out a SMS to get someone to look at it. I’m using the python “find” method which will return “-1” if the text is NOT in the string – otherwise it will return the location of the text in the string.

if text.find("fail") > -1:
message("Whoa! " + currentCall.callerID + " is tweeting a fail", {"to":"tel:+14075551212", "network":"SMS"})

Next I check to see if the message is a “reply”, i.e. a message directly to the Twitter account. If so, I check the message for different keywords and take actions.

The reason for not sending messages out in reply to a “mention” is that it may be harder to guess exactly what the person is looking for based purely on a keyword. With a message sent to the account (a “reply”) you have a better chance of replying with accurate info.

The way I know is by testing whether or not the Twitter ID (“tropohelp” in this case) is in the tweet. If it is a “reply”, Tropo strips out the Twitter ID from the very beginning of the tweet (allowing you to treat it very much like an IM message or SMS message).

So I am testing for the absence of the Twitter ID to show that it is a reply:

elif text.find("@tropohelp") == -1:

if text.find("help") > -1:
say("Tropo lets you build apps that interact via voice, SMS, IM and Twitter. See https://www.tropo.com/ for more.")
message("Tropo help request from: " + currentCall.callerID, {"to":"tel:+14075551212", "network":"SMS"})

elif text.find("faq") > -1:
say("The Tropo FAQ is at https://www.tropo.com/docs/scripting/faq.htm")

elif text.find("documentation") > -1:
say("Tropo docs are at https://www.tropo.com/docs/")

That’s it! A simple and easy way to test for keywords in tweets and take actions based on what you find.

Have fun with it… and if you develop some cool improvements to the app, please drop us an email or leave a comment here.

P.S. And remember the duplicate tweet issue when you are testing your app!

One Response

  1. Tropo.com Lowers SMS Rate to 1 Cent Per Message – Now Super-Cheap To Build SMS Apps…

    Want to build text messaging (SMS) applications for a very cheap price? My colleagues over in Voxeo Labs recently reduced the price of sending or receiving SMS messages to only 1 cent per message. (As a bonus, they also came……

Leave a Reply