Python CLI wrapper script for smstools


This post is an update to Sending SMS via USB modem.
Here is the CLI script you can use to send messages over the sms modem:

 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
#!/usr/bin/env python

"""CLI wrapper for smstools"""

import argparse
import os.path
import time

SPOOL_DIR = "/var/spool/sms/outgoing/"


def create_sms(number, message):
    if len(message) > 160:
        print("Warning: message has more than 160 characters!")
        return

    if not number.startswith("49"):
        print("Wrong number format! Use 'XXYYYZZZZZZZZ' where XX is the country code, YYY the provider and ZZZZZ the client")
        return

    print("Creating sms file for %s with the following text:" % number)
    print(message)

    with open(SPOOL_DIR + str(int(time.time())) + ".sms", 'w') as fh:
        fh.write("To: %s\n\n" % number)
        fh.write(message)


parser = argparse.ArgumentParser()
parser.add_argument('-m', '--message', dest='message', type=str)
parser.add_argument('-n', '--number', dest='number', type=str)
args = parser.parse_args()

create_sms(args.number, args.message)

Usage

./cli -n 49555123456789 -m "Apache is down again!"