I'm working on a script to automate sending SMS messages through a website. I am using Mechanize and BeautifulSoup 4 for doing this.
The program works by calling it from the command line and passing the number and message as arguments; for this I am using Optparse.
The message is passed to the program via the command line, but the website only accepts 444 characters per SMS message. So I am trying to do the following:
- determine the length of the message string (including whitespace) and IF greater than 444 then...
- iterate through a while loop which takes the temporary message string and appends the first 444 characters of the total message string from index 0 to a list object until the length of the temporary message string is no longer greater than 444
- and then by using the number of items in the list object I will iterate through a For Loop block which loops through the handling of sending the messages where each iteration corresponds to the index of a 444 character string (split of the total message) and I'll put that 444 character message slice in the appropriate HTML form field with Mechanize as the message to be sent (hopefully that is understandable!)
The code I have written so far is as follows:
message = "abcdefghijklmnopqrstuvwxyz..." # imagine it is > 444 characters
messageList = []
if len(message) > 444:
tmpMsgString = message
counter = 0
msgLength = len(message)
while msgLength > 444:
messageList.append(tmpMsgString[counter:counter+445]) # 2nd index needs to point to last character's position in the string, not "counter+445" because this would cause an error when there isn't enough characters in string?
tmpMsgString = tmpMsgString[counter+445:msgLength])
msgLength = msgLength-444
counter = counter + 444
else:
messageList.append(message)
I can manage the portion of the code to accept the arguments from the command line and I can also manage with looping through a for loop block and using each item within the list as the message to be sent, however I have little Python experience and I need an experienced pair of eyes to help me along with this part of the code! All help appreciated.