-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathsendsms.py
More file actions
executable file
·93 lines (83 loc) · 4.44 KB
/
sendsms.py
File metadata and controls
executable file
·93 lines (83 loc) · 4.44 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
"""\
Simple script to send an SMS message
@author: Francois Aucamp <francois.aucamp@gmail.com>
"""
from __future__ import print_function
import sys, logging
from gsmmodem.modem import GsmModem, SentSms
from gsmmodem.exceptions import TimeoutException, PinRequiredError, IncorrectPinError
def parseArgs():
""" Argument parser for Python 2.7 and above """
from argparse import ArgumentParser
parser = ArgumentParser(description='Simple script for sending SMS messages')
parser.add_argument('-i', '--port', metavar='PORT', help='port to which the GSM modem is connected; a number or a device name.')
parser.add_argument('-b', '--baud', metavar='BAUDRATE', default=115200, help='set baud rate')
parser.add_argument('-p', '--pin', metavar='PIN', default=None, help='SIM card PIN')
parser.add_argument('-d', '--deliver', action='store_true', help='wait for SMS delivery report')
parser.add_argument('-w', '--wait', type=int, default=0, help='Wait for modem to start, in seconds')
parser.add_argument('--CNMI', default='', help='Set the CNMI of the modem, used for message notifications')
parser.add_argument('destination', metavar='DESTINATION', help='destination mobile number')
return parser.parse_args()
def parseArgsPy26():
""" Argument parser for Python 2.6 """
from gsmtermlib.posoptparse import PosOptionParser, Option
parser = PosOptionParser(description='Simple script for sending SMS messages')
parser.add_option('-i', '--port', metavar='PORT', help='port to which the GSM modem is connected; a number or a device name.')
parser.add_option('-b', '--baud', metavar='BAUDRATE', default=115200, help='set baud rate')
parser.add_option('-p', '--pin', metavar='PIN', default=None, help='SIM card PIN')
parser.add_option('-d', '--deliver', action='store_true', help='wait for SMS delivery report')
parser.add_option('-w', '--wait', type=int, default=0, help='Wait for modem to start, in seconds')
parser.add_option('--CNMI', default='', help='Set the CNMI of the modem, used for message notifications')
parser.add_positional_argument(Option('--destination', metavar='DESTINATION', help='destination mobile number'))
options, args = parser.parse_args()
if len(args) != 1:
parser.error('Incorrect number of arguments - please specify a DESTINATION to send to, e.g. {0} 012789456'.format(sys.argv[0]))
else:
options.destination = args[0]
return options
def main():
args = parseArgsPy26() if sys.version_info[0] == 2 and sys.version_info[1] < 7 else parseArgs()
if args.port == None:
sys.stderr.write('Error: No port specified. Please specify the port to which the GSM modem is connected using the -i argument.\n')
sys.exit(1)
modem = GsmModem(args.port, args.baud, AT_CNMI=args.CNMI)
# Uncomment the following line to see what the modem is doing:
#logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
print('Connecting to GSM modem on {0}...'.format(args.port))
try:
modem.connect(args.pin, waitingForModemToStartInSeconds=args.wait)
except PinRequiredError:
sys.stderr.write('Error: SIM card PIN required. Please specify a PIN with the -p argument.\n')
sys.exit(1)
except IncorrectPinError:
sys.stderr.write('Error: Incorrect SIM card PIN entered.\n')
sys.exit(1)
print('Checking for network coverage...')
try:
modem.waitForNetworkCoverage(5)
except TimeoutException:
print('Network signal strength is not sufficient, please adjust modem position/antenna and try again.')
modem.close()
sys.exit(1)
else:
print('\nPlease type your message and press enter to send it:')
text = raw_input('> ')
if args.deliver:
print ('\nSending SMS and waiting for delivery report...')
else:
print('\nSending SMS message...')
try:
sms = modem.sendSms(args.destination, text, waitForDeliveryReport=args.deliver)
except TimeoutException:
print('Failed to send message: the send operation timed out')
modem.close()
sys.exit(1)
else:
modem.close()
if sms.report:
print('Message sent{0}'.format(' and delivered OK.' if sms.status == SentSms.DELIVERED else ', but delivery failed.'))
else:
print('Message sent.')
if __name__ == '__main__':
main()