How to build an automated Twitter bot using JavaScript and Tropo
August 13th, 2010 by Justin DupreeWe’re back with more how-to goodness! While perusing our relatively extensive blogging history and existing documentation, we noticed the previous examples showing how to link Twitter and Tropo were moderately complex and potentially daunting, especially for someone just trying to get an initial feel for how it works. So, in an attempt to remedy that, we endeavored to provide an easy, all-inclusive example from start to finish. Hopefully we succeeded.
Our example today is a simple RSVP Twitter Bot; when someone tweets the Twitter account attached to your application, it responds back automatically with a confirmation indicating their RSVP was received. In this case, we set it up to be password driven; if the application receives the Tweet “Tropo Rocks!”, it responds with an approval message. Any other message will cause the Twitter bot to respond with a message indicating they sent the wrong password and to try again.
We’ll assume you have a Tropo account at this point and begin with creating your Twitter app. If you don’t already have a account, you can click here to create one.
1) Login to your Tropo account, then go to Your Applications and click the Create New Application link:
2) Choose Tropo Scripting; this will bring up the New Application window:
3) Give the application a name; we chose ClubTropo to fit the example, but you can use whatever you’d like. Next, click the link for Hosted File (next to What URL powers your app?) and choose Create a new hosted file for this application. This will open the New Mapped File window:
4) You will need to give the file a name; as with the application name, it can be anything you like, but make sure this one ends with .js to identify it as Javascript . In the box for File Text, enter in the following simple code:
message = currentCall.initialText;
if(message == "Tropo Rocks!"){
say("You just learned how to make a Twitter bot with @Tropo for free! Welcome to the club.");
}
else {
say ("Say what? Try it again - send us a tweet with the correct password to get on the list.");
}
Then click Create File to save it.
To explain what the code does:
- We define the variable message as currentCall.initialText, which contains the tweet sent by the user (go here for more on currentCall).
- Next, we specify what to tweet back if the user’s tweet has the correct message (i.e., Tropo Rocks!) and when it does not.
- That’s it! That’s all the code you need.
5) Once the file has been created and you’re back on the New Application screen, you’ll see the path to your file has been automatically filled in for you. Now click Create Application to save the application as a whole:
6) This will bring up the page in the next screenshot. Here, we need to add our Twitter account to the application; this will be the Twitter account that will act as the bot. Select the Twitter icon at the bottom of the screen and you’ll see a link that says Click to activate Twitter. This will attempt to connect to whatever Twitter account you’re currently logged into, so make sure you’re in the right one before you click the link.
7) Twitter will display a webpage in your browser asking you to Deny or Allow Tropo access to your Twitter account. If you’re definitely in the right account, go ahead and click Allow:
8) You will then return back to the Tropo New Application screen and your Twitter account name will be listed:
9) Click the Update Application button to save the change, and you’re ready to test! Our Twitter bot is named ClubTropo and we sent it a message from two different accounts. From TropoUser, we sent “@ClubTropo Tropo Rules!”, which was rejected. From JD_Gryph, we sent “@ClubTropo Tropo Rocks!”, which was accepted as the correct password. Check out the responses below:
Voila! RSVP Twitter Bot achieved. Feel free to test with @ClubTropo and/or create your own; the door is wide open!
For more information, check out our Tropo Scripting documentation, or check out our Tutorials and Samples.
(Sidenote: You may want to check out this blogpost before testing any Twitter apps; Twitter can be touchy with multiple instances of identical messages)
Related posts:
- Simple JavaScript SMS API: Text Messaging with Tropo
- Get Notified When Someone Mentions You on Twitter
- Build Node.js apps for Tropo that use voice, SMS, IM and Twitter and host them at NodeFu
- Calling all C# developers! Now YOU can build voice, SMS, IM, Twitter apps with Tropo!
- Testing a Tropo app with Twitter? Remember that Twitter rejects duplicate tweets…
Tags: JavaScript, tropo scripting, Twitter









How to create an automated Twitter self-service “RSVP” app using Tropo.com…
Do you want an application that lets your customers interact with you via Twitter – but to do so via “self-service”? i.e. without a person having to help them? What if you wanted to make it so that people could……
Hi I need a script in PHP to post a particular message to a twitter account. Can anyone please provide it to me, As iam in very much hurry, Any little help will be appreciated greatly. Thanks