<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://cfa.media.mit.edu" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>hellyeah@mit.edu&#039;s blog</title>
 <link>http://cfa.media.mit.edu/blog/75</link>
 <description></description>
 <language>en</language>
<item>
 <title>Sending SMS via Skype w/ only 6 lines of code</title>
 <link>http://cfa.media.mit.edu/content/sending-sms-skype-w-only-6-lines-code</link>
 <description>&lt;p&gt;Hey folks,&lt;br /&gt;
Skype and Python got a thing going on.  Here&#039;s how you start it up.&lt;br /&gt;
1. Download and install the Skype client.&lt;br /&gt;
2. Create a Skype account and buy some Skype credit (like, $10 worth).&lt;br /&gt;
3. Download and install Python from python.org&lt;br /&gt;
3. Download and install Skype4Py from Sourceforge &lt;a href=&quot;http://sourceforge.net/projects/skype4py/&quot; title=&quot;http://sourceforge.net/projects/skype4py/&quot;&gt;http://sourceforge.net/projects/skype4py/&lt;/a&gt;&lt;br /&gt;
3a. Skype4Py reference is here: &lt;a href=&quot;https://developer.skype.com/wiki/Skype4Py&quot; title=&quot;https://developer.skype.com/wiki/Skype4Py&quot;&gt;https://developer.skype.com/wiki/Skype4Py&lt;/a&gt;&lt;br /&gt;
4. Start Skype and log in.&lt;br /&gt;
5. In your Skype client, add a phone number as a SkypeOut contact.&lt;br /&gt;
6. Create a python file with the code below.&lt;br /&gt;
7. In the code below, replace the dummy phone number (+16175551212) with the number you added in step 5.&lt;br /&gt;
8. Run your script!  Under Linux, run with superuser permissions. &lt;/p&gt;
&lt;p&gt;[ note: for code examples, see the attached files.  I just wasted an hour trying to force Drupal (this blogging software) to preserve whitespace so it doesn&#039;t destroy all python code posted.  Can the admin for this blog change the whitespace settings so we can share python code here? ]&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&lt;br /&gt;
import sys, Skype4Py&lt;br /&gt;
api = Skype4Py.Skype() # create a Skype API instance&lt;br /&gt;
api.Attach() # connect to Skype&lt;br /&gt;
message = api.CreateSms(Skype4Py.smsMessageTypeOutgoing, &#039;+16175551212&#039;) # create SMS object. CHANGE THE PHONE NUMBER&lt;br /&gt;
message.Body = &quot;Hello World!&quot; # set value of body&lt;br /&gt;
message.Send()  # send message&lt;br /&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The Skype API under Linux has one complication: it will not run when invoked by other programs such as chron or mod_python.  This is because it needs access to the XServer environment variables.  My solution to this is to create a little Skype relay server that is invoked on the command line and runs perpetually.  Other programs can send SMS messages by sending message data to the relay server via a UDP socket.  The example below simply receives a message body (via UDP) and sends it (via the Skype API) to a hard-coded phone number list.&lt;/p&gt;
&lt;p&gt;SkypeRelayServer.py:&lt;br /&gt;
&lt;code&gt;&lt;br /&gt;
import sys&lt;br /&gt;
import Skype4Py&lt;br /&gt;
import socket&lt;br /&gt;
api = Skype4Py.Skype() # create a Skype API instance&lt;br /&gt;
api.Attach() # connect to Skype&lt;br /&gt;
HOST = &#039;localhost&#039; # The remote host&lt;br /&gt;
PORT = 52007 # The same port as used by the client&lt;br /&gt;
ipSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create network socket&lt;br /&gt;
ipSock.bind((HOST, PORT)) # bind socket to host/port&lt;br /&gt;
ipSock.listen(1) # start listenin&#039;&lt;br /&gt;
while 1: # do this forever (until program is terminated)&lt;br /&gt;
  ipConn, ipAddr = ipSock.accept() # wait here until remote client program connects&lt;br /&gt;
  body_str = ipConn.recv(160) # receive up to 160 characters from remote client program.  this will be the SMS message body&lt;br /&gt;
  pNums_l = [&#039;+16175551212&#039;] # list of phone numbers to send SMS to. in this case, only one number&lt;br /&gt;
  for pNum_str in pNums_l: # loop through phone number list&lt;br /&gt;
    message = api.CreateSms(Skype4Py.smsMessageTypeOutgoing, pNum_str) # create SMS object&lt;br /&gt;
    message.Body = body_str # set value of body&lt;br /&gt;
    message.Send() # send message&lt;br /&gt;
s.shutdown(socket.SHUT_RDWR) # do some network socket housekeeping.&lt;br /&gt;
s.close() # housekeeping&lt;br /&gt;
&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Here is an example of a client script that uses SkypeRelayServer.py to send SMS messages.  It&#039;s a python script in the mod_python environment that reads an SMS message string submitted from a Web form and sends the data to SkypeRelayServer.py to be broadcast via SMS.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;&lt;br /&gt;
from mod_python import apache&lt;br /&gt;
from mod_python import util&lt;br /&gt;
import sys&lt;br /&gt;
import os&lt;br /&gt;
import socket&lt;br /&gt;
import time&lt;br /&gt;
def handler(req):&lt;br /&gt;
  HOST = &#039;localhost&#039;    # The remote host&lt;br /&gt;
  PORT = 52007              # The same port as used by the server&lt;br /&gt;
  ipSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # create network socket&lt;br /&gt;
  ipSock.settimeout(600)&lt;br /&gt;
  ipSock.connect((HOST, PORT)) # connect to SkypeRelayServer.py&lt;br /&gt;
  body_str = util.FieldStorage(req).list[0].value # get the value of the submitted SMS message body&lt;br /&gt;
  ipSock.send(body_str) # send SMS message body to SkypeRelayServer.py&lt;br /&gt;
  ipSock.shutdown(socket.SHUT_RDWR) # housekeeping&lt;br /&gt;
  ipSock.close() # housekeeping&lt;br /&gt;
  util.redirect(req, &quot;send.html&quot;) # redirect the browser back to the Web form&lt;br /&gt;
  return apache.OK # tell Apache this script is finished&lt;br /&gt;
&lt;/code&gt;&lt;/p&gt;
</description>
 <comments>http://cfa.media.mit.edu/content/sending-sms-skype-w-only-6-lines-code#comments</comments>
 <enclosure url="http://cfa.media.mit.edu/sites/cfa.media.mit.edu/files/ic.py_.txt" length="649" type="text/plain" />
 <pubDate>Tue, 17 Mar 2009 01:44:28 +0000</pubDate>
 <dc:creator>hellyeah@mit.edu</dc:creator>
 <guid isPermaLink="false">46 at http://cfa.media.mit.edu</guid>
</item>
<item>
 <title>Liberia’s Blackboard Blogger</title>
 <link>http://cfa.media.mit.edu/content/liberia%E2%80%99s-blackboard-blogger</link>
 <description>&lt;p&gt;Great article: &lt;a href=&quot;http://www.afrigadget.com/2009/03/13/liberias-blackboard-blogger/&quot; title=&quot;http://www.afrigadget.com/2009/03/13/liberias-blackboard-blogger/&quot;&gt;http://www.afrigadget.com/2009/03/13/liberias-blackboard-blogger/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;cite&gt;Alfred Sirleaf is an analog blogger. He take runs the “Daily News”, a news hut by the side of a major road in the middle of Monrovia. He started it a number of years ago, stating that he wanted to get news into the hands of those who couldn’t afford newspapers, in the language that they could understand.&lt;/p&gt;
&lt;p&gt;Alfred serves as a reminder to the rest of us, that simple is often better, just because it works. The lack of electricity never throws him off. The lack of funding means he’s creative in ways that he recruits people from around the city and country to report news to him. He uses his cell phone as the major point of connection between him and the 10,000 (he says) that read his blackboard daily.&lt;/cite&gt;&lt;/p&gt;
</description>
 <comments>http://cfa.media.mit.edu/content/liberia%E2%80%99s-blackboard-blogger#comments</comments>
 <pubDate>Tue, 17 Mar 2009 00:07:10 +0000</pubDate>
 <dc:creator>hellyeah@mit.edu</dc:creator>
 <guid isPermaLink="false">45 at http://cfa.media.mit.edu</guid>
</item>
</channel>
</rss>
