
You are welcome, The script was meant to just show how you can obtain parameters from the command line and access it from your program. I do not have the SMS utilities so I could not simulate their functionality or test them from my box. I suppose the script below should work on yours. Invoke the program with the same arguments as before, just for the kicks. ################# ################# import argparse import gammu def sms_en_masse(contacts, message): # Configure Gammu sm = gammu.StateMachine() sm.ReadConfig() sm.Init() # Open the file with mobile phone contacts and read its contents with open(contacts, 'r' ) as phone_numbers: for cellphone in phone_numbers: # Read the data obtained line by line message = {'Number': cellphone, 'Text': message, 'SMSC': {'Location': 1}} try: sm.SendSMS(message) except Exception, exc: print 'Sending text to %s failed. Stack-trace: %s' % (number, exc) # print u'Number: %s - Text: %s'%(cellphone, message) if __name__ == "__main__": parser = argparse.ArgumentParser(description='Read in phone numbers from a text file and send a single SMS to all of them.') parser.add_argument('-f', '--file', type=str, nargs='+', help='Path to file with the cellphone numbers') parser.add_argument('-m', '--message', type=str, help='A message to send to all receipents.') args = parser.parse_args() sms_en_masse (args.file[0], args.message) ########### ########### Martin. On Tue, Jun 26, 2012 at 11:52 AM, Thuo Wilson <lixton@gmail.com> wrote:
On 26 June 2012 10:17, Martin Chiteri <martin.chiteri@gmail.com> wrote:
Hello again,
I have conjured up some will power and come up with a sample solution:
Supposing you have a simple text file named contacts.txt with the cellphone numbers you intend to SMS in the following format:
# *contacts.txt * #+++++++++++ 0722123456 0733768909 0721987989 #===========
# I have maintained the same name for the source files to the program to send texts in mass, ### *mass_sms.py* ## ============
import argparse
def sms_en_masse(contacts_file, message): numbers_list = [] # A list data type to hold cellphone numbers from a file
# Open the file with mobile phone contacts and read its content with open(contacts_file, 'r' ) as phone_numbers: for cellphone in phone_numbers: # Read the data obtained line by line print u'Number: %s - Message: %s'%(cellphone, message)
if __name__ == "__main__": parser = argparse.ArgumentParser(description="Read in phone numbers from a text file and send a single SMSs to all of them.") parser.add_argument('-f', '--file', type=str, nargs='+', help='Path of file with the cellphone numbers') parser.add_argument('-m', '--message', type=str, help='A message to send to all receipents.')
args = parser.parse_args()
sms_en_masse (args.file[0], args.message)
######
You can invoke the program from the command line in the following manner:
$ *python mass_sms.py -f contacts.txt -m 'Hello world.' *
Give the correct path to the file with the phone numbers, am assuming they are in the same folder with your Python script.
*N.B:* You may also need to download and manually install argparse<http://pypi.python.org/pypi/argparse/1.2.1>from the Python packaging index, or use setup tools / pip to get it automatically from PyPI.
Martin.
Thanks Martin and Gisho and all others - The python script could read my file but not able to process the commands the way i wanted.... am not anywhere good in python - that could be the reason.
Meanwhile i had overlooked the power of awk and i finally found what i was looking for":
See below;
simple but to the point: Thats why i like bash scripting :)
#!/bin/sh for i in `awk -F ":" '{print $1}' contacts_file`; do echo `cat somemessage.txt`| /usr/bin/gammu --sendsms TEXT $i done
On Tue, Jun 26, 2012 at 9:50 AM, Thuo Wilson <lixton@gmail.com> wrote:
On 26 June 2012 09:27, Martin Chiteri <martin.chiteri@gmail.com> wrote:
Hello Wilson,
On Mon, Jun 25, 2012 at 5:14 PM, Thuo Wilson <lixton@gmail.com> wrote:
Hello Python coders,
Analyze this;
#!/usr/bin/env python # sample script to show how to same SMS to multiple recipients
import gammu import sys
# Check parameters count if len(sys.argv) < 3 or sys.argv[1] in ['--help', '-h', '-?']: print 'Usage: mass-sms <TEXT> [number]...' sys.exit(1)
# Configure Gammu sm = gammu.StateMachine() sm.ReadConfig() sm.Init()
# Prepare SMS template message = {'Text': sys.argv[1], 'SMSC': {'Location': 1}} # HELP ME HERE
# Send SMS to all recipients on command line for number in sys.argv[1:]: #and here i guess message['Number'] = number try: sm.SendSMS(message) except Exception, exc: print 'Sending to %s failed: %s' % (number, exc)
My issue is simple but not sure how to work on it;
I have some texts here;
/usr/bin/python mass-sms.py "*we are smart people*" `cat /root/numbers.txt`
I want to send the message "We are smart people" to the number in numbers.txt (some cellphone)
My problem is i can only read the first word "we" when i have sys.argv[1], which is OK, sys.argv[1:], i have anything later than and so on...
I think the problem is, as you have figured out, that the parameters collected from the command line are taken in as tuples and accessing them by indexes only gives a part of the string required and not the entire input.
I will suggest you try out the package *argparse* for the job [ http://docs.python.org/dev/library/argparse.html ]. I have used it before and it is pretty easy to specify the data types of arguments expected from the user terminal (ints, floats, strings). I can try it out later on in the day and see if it will work with your problem.
Martin.
Thanks, looking forward.
How can i read a file using this and output texts like "*we are smart
people"* to the above eg;
/usr/bin/python mass-sms.py `cat sometext` `cat /root/numbers.txt`
Hope am clear.
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke