SMS Workflow Madness: Twilio to PHP to Python to Dropbox to Autohotkey to Conquer The World

Recently, I’ve been trying to trigger some python code using a text message. It has been a complicated little journey, so I thought I’d write it up for you. If you don’t want to read through it all, the summary is – twilio to PHP to launch Python to put a file in Dropbox, autohotkey to monitor dropbox and run a python script. Away we go…

First, Twilio is a great service if you want to develop anything with text messages. At first, I built a quick fix using If This Then That (which you should check out either way). However, I soon realized that the benefit of a text message is that it is nearly instant. IFTTT only checks tasks every 15 minutes and in a crunch, I would want a response back before then…

So I signed up for Twilio and created my application. The applications can be very complex, but for my purposes, I just needed a few lines of PHP to receive the text from the SMS and then use that information. Here is my test script:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$body = $_REQUEST['Body'];
$from = $_REQUEST['From'];
$path = "/var/www/cgi-bin/addfile.py";
$command = "python ".$path." '$body'";
$command = escapeshellcmd($command);
exec($command,$result);
echo "<Response>
<Sms>Thanks for the message:".$body." your num:".$from." </Sms>
</Response>";
?>

There’s a lot going on there, but here is the gist. The first two lines format the document as XML for Twilio to understand what should be done. No surprises here.

1
2
header("content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";

The next part pulls the data from the text message into body and from and then passes these values to a python script I wrote to interact with dropbox.

1
2
3
4
5
6
$body = $_REQUEST['Body'];
$from = $_REQUEST['From'];
$path = "/var/www/cgi-bin/addfile.py";
$command = "python ".$path." '$body'";
$command = escapeshellcmd($command);
exec($command,$result);

The final part is the xml. This I pulled straight from the Twilio getting started guide

1
2
3
echo "<Response>
<Sms>Thanks for the message:".$body." your num:".$from." </Sms>
</Response>";

Ok so now we have a file for Twilio to interact with. Next we need to put some content in that python file. Before you try this out, you’ll need to install the dropbox api libraries. I used the command

1
easy_install dropbox

but you might have to do that differently based on your operating system.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/python
 
# Include the Dropbox SDK libraries
from dropbox import client, rest, session
import sys
 
name = sys.argv[1]
 
# Get your app key and secret from the Dropbox developer website
APP_KEY = 'xxxxxxxxxxxxxxx'
APP_SECRET = 'xxxxxxxxxxxxxxx'
 
# Access type will be defined in your dropbox settings
ACCESS_TYPE = 'app_folder'
sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
 
# I removed this section after obtaining my access_token
# and access_token_secret, but you'll need to do it once.
# The return value will be a string that you can parse.
#request_token = sess.obtain_request_token()
#url = sess.build_authorize_url(request_token)
#print "url:", url
#print "Please visit this website and press the 'Allow' button.
#raw_input()
 
access_token = "xxxxxxxxxxxxxxx"
access_token_secret= "xxxxxxxxxxxxxxx"
 
sess.set_token(access_token, access_token_secret)
 
client = client.DropboxClient(sess)
print "linked account:", client.account_info()
 
#create the file if it doesn't exist
#f = open('file.txt', "w")
#f.close()
 
#open it for reading only...
f = open('file.txt')
# put the file to the app_folder in dropbox
response = client.put_file('/'+name+'.txt', f)
# this is the response passed back to PHP for debugging.
print "uploaded:", response

The file above is a bit of a mess but the idea is simple, take an argument as the command, authenticate with dropbox and put a file in dropbox with that name. I’ve tried a few different ways to do this a Dropbox PHP class or two… The python script turned out to be much easier for me – perhaps you have had better luck?

So now, with all that lovely code above, when I send a text message to my twilio account number, the php file takes the SMS message as a command and launches the python dropbox script, putting a file with that command name in my folder. The last part is an autohotkey script that I have to monitor the app_folder (it’s actually sitting in the app folder for simplicity). Here is that file:

1
2
3
4
5
6
7
8
9
10
#persistent
setTimer check_file,1000
return
 
check_file:
IfExist, command.txt
{
 filemove command.txt, %A_ScriptDir%\processed\command%A_Now%.txt
 run myprogram.py command
}

This script checks my folder for a file called “command.txt” and then if it finds it, runs a script and moves the file to a processed folder with a time stamp. It’s not perfect, as it requires a separate “look” for each command that you want to run, but it was perfect for my needs.

So that’s my system. It’s not pretty and it has a few more steps than I’d like for efficiency and safety, but it does work. Fast. In fact, a text message can trigger a program on my remote machine within 10 seconds. That is not bad…

Let me know if you’ve tried something similar or have suggestions on improvements. I’d love to hear it.

-ab

 

Posted in coding | Tagged , , , , , , | Leave a comment

shift8solutions.com -> Check. It. Out.

For a while now, I’ve been working on a warehouse management system (WMS) on my business website shift8solutions.com. As I’m finally done, I thought I’d share the good news with those who visit this site. Please take a look at shift8WMS and share with your friends!

Shift8WMS is a user friendly, web based warehousing system for small businesses. The goal is to provide inexpensive but excellent products to small business owners. I think visiting shift8 will give you a more complete understanding so check it out!

All the very best,

-ab

Posted in Uncategorized | Leave a comment

SAPeedie… for free

I’ve decided to give away my portable SAPeedie app for SAP testing. It’s a simple enough windows app, but below is the extract from the help file. I haven’t spent much time on it recently, so please be aware that I’m not going to provide active support. However, I’ll post answers to questions if enough people have issues, so feel free to share your experience…

Here is the link (V2.2)

Sapeedie is a tool I developed to aid in SAP testing. The program simplifies actions that I found tedious and repetitive and includes the standard windows keyboard shortcuts that are missing in SAP.

Quick Shortcuts:
Control + N – open a new session
Control + A – select all (some text fields do not allow you to select all)
` – sends you to the command box directly (keeping your hands on the keyboard and avoiding the built in shortcut which is awkward)
Windows + S – Sets the current window to stay “on top”. All other windows will remain behind the tacked window(s). The same keys to remove this property.
Alt + Right Button – a bit more convenient than that silly Ctrl + Y selection shortcut.

Advanced Features (the cool stuff):
Control + Right Mouse Click – Opens a special menu that you can customize as needed. You can put favorite transactions in your txns.txt file and have quick access to them from anywhere in SAP. The same menu also provides access to the DocLog (more details on that in a second). Each file you click in the DocLog is transferred to your clipboard.

Control + Shift + Right Click – Captures the message on the screen from SAP. For example, if you create a PO, the message might be “PO 12345 created”. When you control + shift + right click, the message is saved into your doclog.txt file, with the name of the transaction you were running, and the date/time. This will allow you to keep track of your testing – every time you press those keys (even if there isn’t a number) the text will be saved. It’s a sort of “this is what I did” log. Just remember to click!

Copy Cliboard to your DocLog – it’s in the menu, but if you want to capture a large quantity of text, with a reference to where you got it, you can use this feature… (There’s room for improvement but this allows you to bring ANYTHING into your doclog. Just copy it first.)

Posted in Software | Tagged , , | Leave a comment

Mapping (made-up) Shipping Data

I’m reading Ben Fry’s book Visualizing Data and having some serious fun with mapping. I invented some shipping data (from my home state of Ohio to the other 50) and then put it on a map of the US using some of Ben’s example code. I didn’t manage to get the animated version to work on the site, but it’s still pretty… If you’re interested, let me know and I’ll pass along the code.

Below is an example of the output. Love to hear your thoughts.

~ab

Shipping Map

 

Posted in Uncategorized | Leave a comment

Spotify control script for AHK

I found it rather annoying to reopen Spotify whenever I wanted to change tracks or increase the volume… Using Autohotkey, it’s no longer a problem (I found some great examples via Google).

Enjoy

~ab

;
; Spotify volume and track controls.
;

#persistent

^!Space::
{
DetectHiddenWindows, On
ControlSend, ahk_parent, {Space}, ahk_class SpotifyMainWindow
DetectHiddenWindows, Off

}
return

^!Right::
{
DetectHiddenWindows, On
ControlSend, ahk_parent, ^{Right}, ahk_class SpotifyMainWindow
DetectHiddenWindows, Off

}
return

^!Left::
{
DetectHiddenWindows, On
ControlSend, ahk_parent, ^{Left}, ahk_class SpotifyMainWindow
DetectHiddenWindows, Off
}
return

^!Down::
{
DetectHiddenWindows, On
ControlSend, ahk_parent, ^{Down}, ahk_class SpotifyMainWindow
DetectHiddenWindows, Off
}
return

^!Up::
{
DetectHiddenWindows, On
ControlSend, ahk_parent, ^{Up}, ahk_class SpotifyMainWindow
DetectHiddenWindows, Off
}
return

Posted in Autohotkey | Leave a comment

New post on just an asterisk

Check it out here – link

Posted in Uncategorized | Leave a comment

Stock widget…

I’ve been thinking a lot about dashboards recently and wanted to use processing to create a “stock tool” – something to grab real-time stock data for a company and display it in an interesting way.

Take a look here

Posted in Uncategorized | Leave a comment

New processing project: Clock

Another Processing project – this one is kinda fun. It’s a clock, created with code and some basic shapes. I think the coolest part is that I used “processing.js”, a library that renders the result in an HTML5 format – you can see the animation on your iPad! (or iPhone)

Check it out here

Here is an image of the output.

Posted in Processing | Leave a comment

Arc_flowers

This applet uses random numbers (within some boundaries) to create the stems and petals.  If you press any key while it runs, it will stop. Each time will be different, so enjoy!

Take a look at the java applet herehtml5 animation here and below is a sample of the output.

Image generated using processing

Posted in Processing | Leave a comment