daily pastebin goal
2%
SHARE
TWEET

stdin

a guest Sep 23rd, 2008 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/python
  2. #
  3. # Sends a SMS with Skype.
  4. # Uses the Skype4Py Skype API Wrapper. See https://developer.skype.com/wiki/Skype4Py
  5. # for download and installation instructions.
  6. #
  7. # (c) Copyright 2007, Vincent Oberle, vincent@oberle.org
  8. #
  9. # This software may be used and distributed according to the terms
  10. # of the GNU Public License, incorporated herein by reference.
  11.  
  12.  
  13. import sys
  14. import re
  15. from optparse import OptionParser
  16.  
  17. import Skype4Py
  18.  
  19.  
  20. appname = 'sms_sender'
  21.  
  22.  
  23. # Limitations: Can only handle the sending of one SMS at a time
  24. class SkypeSMS:
  25.         def __init__(self):
  26.                 # The ISmsMessage we are trying to send
  27.                 self.message = None
  28.                 # To poll until the SMS has been actually sent
  29.                 self.done = False
  30.                 self.count_success_delivered = 0
  31.                
  32.                 self.api = Skype4Py.Skype(Events = self)
  33.                 self.api.FriendlyName = appname
  34.                 self.api.Attach()  # Attach to Skype client
  35.  
  36.         def OnSmsMessageStatusChanged(self, message, status):
  37.                 #print 'Got OnSmsMessageStatusChanged'
  38.                 if message != self.message: return # event from another SMS
  39.                 if status == Skype4Py.smsMessageStatusReceived:
  40.                         print 'the message has been received' # (but not tagged as read)
  41.                         self.done = True
  42.                 elif status == Skype4Py.smsMessageStatusRead:
  43.                         print 'the message has been read'
  44.                         self.done = True
  45.                 #elif status == Skype4Py.smsMessageStatusComposing: print 'the message has been created but not yet sent'
  46.                 elif status == Skype4Py.smsMessageStatusSendingToServer:
  47.                         print 'the message is in process of being sent to server'
  48.                 elif status == Skype4Py.smsMessageStatusSentToServer:
  49.                         print 'the message has been sent to server'
  50.                 elif status == Skype4Py.smsMessageStatusDelivered:
  51.                         print 'server has confirmed that the message is sent out to recipient'
  52.                         self.done = True
  53.                 elif status == Skype4Py.smsMessageStatusSomeTargetsFailed:
  54.                         print 'server reports failure to deliver the message to one of the recipients within 24h'
  55.                         self.done = True
  56.                 elif status == Skype4Py.smsMessageStatusFailed:
  57.                         print 'the message has failed' # possible reason may be found in FAILUREREASON property
  58.                         fr = self.message.FailureReason
  59.                         if fr == Skype4Py.smsFailureReasonMiscError:
  60.                                 print 'Failure reason: Misc failure'
  61.                         elif fr == Skype4Py.smsFailureReasonServerConnectFailed:
  62.                                 print 'Failure reason: unable to connect to SMS server'
  63.                         elif fr == Skype4Py.smsFailureReasonNoSmsCapability:
  64.                                 print 'Failure reason: recipient is unable to receive SMS messages'
  65.                         elif fr == Skype4Py.smsFailureReasonInsufficientFunds:
  66.                                 print 'Failure reason: insufficient Skype Credit to send an SMS message'
  67.                         elif fr == Skype4Py.smsFailureReasonInvalidConfirmationCode:
  68.                                 print 'Failure reason: an erroneous code was submitted in a CONFIRMATION_CODE_SUBMIT message'
  69.                         elif fr == Skype4Py.smsFailureReasonUserBlocked or fr == Skype4Py.smsFailureReasonIPBlocked or fr == Skype4Py.smsFailureReasonNodeBlocked:
  70.                                 print 'Failure reason: user is blocked ' + fr
  71.                         self.done = True
  72.  
  73.         def SmsTargetStatusChanged(self, target, status):
  74.                 if status == Skype4Py.smsTargetStatusUndefined: print target.Number, 'is undefined'
  75.                 elif status == Skype4Py.smsTargetStatusNotRoutable: print target.Number, 'cannot be routed'
  76.                 elif status == Skype4Py.smsTargetStatusDeliveryFailed: print target.Number, 'could not be deliveted'
  77.                 #elif status == Skype4Py.smsTargetStatusAnalyzing: print target.Number, 'is being analized'
  78.                 #elif status == Skype4Py.smsTargetStatusAcceptable: print target.Number, 'is acceptable'
  79.                 #elif status == Skype4Py.smsTargetStatusDeliveryPending: print target.Number, ''
  80.                 elif status == Skype4Py.smsTargetStatusDeliverySuccessful:
  81.                         print target.Number, 'has been successfully delivered'
  82.                         self.count_success_delivered = self.count_success_delivered + 1
  83.                         if self.count_success_delivered >= self.number_of_targets:
  84.                                 print 'All targets successfully delivered'
  85.                                 self.done = True
  86.  
  87.         def create_sms(self, phonenumbers):
  88.                 if self.message: raise StandardError('SMS already created?!?')
  89.                 # we get also PRICE, PRICE_PRECISION, PRICE_CURRENCY, TARGET_STATUSES
  90.                 if not phonenumbers: return False
  91.                 self.message = self.api.CreateSms(Skype4Py.smsMessageTypeOutgoing, phonenumbers)
  92.                 if self.message:
  93.                         self.number_of_targets = len(self.message.Targets)
  94.                         return True
  95.                 return False
  96.  
  97.         def delete_sms(self):
  98.                 if not self.message: return
  99.                 self.message.Delete()
  100.  
  101.         def set_body(self, text):
  102.                 if not self.message or not text: return
  103.                 self.message.Body = text
  104.  
  105.         def send_sms(self):
  106.                 if not self.message: return
  107.                 self.message.Send()
  108.  
  109.         def set_replyto(self, pstn):
  110.                 if not self.message or not pstn: return
  111.                 self.message.ReplyToNumber = pstn
  112.  
  113.         def print_chunking(self):
  114.                 if not self.message: return
  115.                 print 'Your SMS has', len(self.message.Chunks), 'chunk(s).'
  116.  
  117.         def print_val_numbers(self):
  118.                 numbers = api.CurrentUserProfile.ValidatedSmsNumbers
  119.                 if numbers:
  120.                         print 'Validated numbers:'
  121.                         for n in numbers:
  122.                                 print '\t', n
  123.                         return
  124.                 print 'No validated numbers'
  125.  
  126.         def wait_until_done(self):
  127.                 while not self.done:
  128.                         pass
  129.  
  130.  
  131.  
  132. if __name__ == "__main__":
  133.         parser = OptionParser('%prog [options] phonenumber[,phonenumber*] text')
  134.  
  135.         parser.add_option('-r', '--replyto', dest='replyto',
  136.                 default = None, help='Reply-to number to use')
  137.         parser.add_option('-n', '--dontsend', action='store_true', dest='dontsend',
  138.                 default = False, help='Do not send the SMS, only print info about it')
  139.         parser.add_option('-u', '--valnums', action='store_true', dest='valnums',
  140.                 default = False, help='Prints the validated numbers')
  141.         parser.add_option('-d', '--debug', action='store_true', dest='debug',
  142.                 default = False, help='Deprecated, unused')
  143.     parser.add_option('-f', '--fromfile', action='store_true', dest='fromfile',
  144.         default = False, help='Read text from a file')
  145.  
  146.         options, args = parser.parse_args()
  147.         if len(args) < 2:
  148.                 parser.print_help()
  149.                 sys.exit(0)
  150.         phonenumbers = args[0] #.split(',')
  151.     if options.fromfile:
  152.         try:
  153.             fd = open(args[1])
  154.             text = fd.read()
  155.             fd.close()
  156.         except Exception, e:
  157.             print 'Could not read %s: %s' % (args[1], e)
  158.             sys.exit(1)
  159.     else:
  160.             text = ' '.join(args[1:])
  161.  
  162.         #print 'Phone numbers:', phonenumbers
  163.         #print 'Text:', text
  164.        
  165.         api = SkypeSMS()
  166.  
  167.         if options.valnums:
  168.                 api.print_val_numbers()
  169.  
  170.         if not api.create_sms(phonenumbers):
  171.                 print 'SMS could not be created'
  172.                 sys.exit(0)
  173.        
  174.         api.set_body(text)
  175.         api.print_chunking()
  176.  
  177.         if options.replyto:
  178.                 api.set_replyto(options.replyto)
  179.        
  180.         if not options.dontsend:
  181.                 api.send_sms()
  182.  
  183.                 api.wait_until_done()
  184.         else:
  185.                 # Since we didn't send it we delete it
  186.                 api.delete_sms()
RAW Paste Data
Top