Last active
December 10, 2015 16:48
-
-
Save adw0rd/4463243 to your computer and use it in GitHub Desktop.
Specially designed for Nagios, but can be used universally. For convenience can make symlink to ``/usr/local/bin/sendsms``.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Simple sms-sender via LittleSMS | |
--------------------------------- | |
You must specify your ``USER_EMAIL`` and ``USER_APIKEY`` below. | |
Examples:: | |
$ sendsms.py --recipients=79117654321,79317654321 --message="Some message" | |
... or so ... | |
$ echo "Some message" | sendsms.py --recipients=79117654321,79317654321 | |
""" | |
DEFAULT_SENDER = "MONITOR" # Name of sender | |
SEND_URL = "https://littlesms.ru/api/message/send" | |
USER_EMAIL = "" | |
USER_APIKEY = "" | |
import sys | |
import json | |
import argparse | |
import urllib | |
import urllib2 | |
def sendsms(recipients, sender, message, read_bytes, debug=False): | |
if not message: | |
message = sys.stdin.read(read_bytes) | |
params = { | |
'user': USER_EMAIL, | |
'apikey': USER_APIKEY, | |
'recipients': recipients, | |
'sender': sender, | |
'message': message, | |
'test': int(debug), | |
} | |
request = urllib2.urlopen(urllib2.Request(SEND_URL + "?" + urllib.urlencode(params))) | |
result = json.loads(request.read()) | |
if result['status'] != "success": | |
error_message = "ServiceError: {0}".format(result['message']) if result['status'] == "error" else "Service is not responsible" | |
raise Exception(error_message) | |
return True | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Simple sms-sender. ' | |
'Send sms via service LittleSMS') | |
parser.add_argument('-R', '--recipients', dest='recipients', type=str, required=True, | |
help='Comma-separated numbers, an example of "79117654321" ' | |
'or "79117654321,79317654321", etc.') | |
parser.add_argument('-S', '--sender', dest='sender', type=str, default=DEFAULT_SENDER, | |
help='Sender\'s name or phone number, etc.') | |
parser.add_argument('-M', '--message', dest='message', type=unicode, | |
help='The some message. If you omit this argument, ' | |
'read from stdin.') | |
parser.add_argument('--read-bytes', dest='read_bytes', type=int, default=256, | |
help='Defaults to read the first 256 bytes of message, ' | |
'but this can be changed. Example "--read-bytes=1024".') | |
parser.add_argument('--debug', dest='debug', action='store_true', | |
help='Debug mode. If True, then simulates the function, ' | |
'sms send to service, but send to final recipients ' | |
'is not performed.') | |
args = parser.parse_args() | |
sendsms(args.recipients, args.sender, args.message, args.read_bytes, args.debug) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment