This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomoyo-sendsms.py
executable file
·83 lines (65 loc) · 2.71 KB
/
comoyo-sendsms.py
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
#!/usr/bin/python
"""comoyo-sendsms.py: Sends SMS via Telenor/Comoyo."""
__author__ = "Havard Sorbo"
__copyright__ = "Copyright 2013"
__license__ = "GPL2"
import socket
import os
import argparse
import threading
import logging
from comoyo import *
SESSION_FILE = os.path.expanduser("~/.sendsms.json")
def load_settings():
f = open(SESSION_FILE, 'r')
settings = json.loads(f.read())
f.close()
return settings
def save_settings(settings):
f = open(SESSION_FILE, 'w+')
f.write(json.dumps(settings))
f.close()
def monitor(transport, sms):
sms.enable_smsplus()
sms.enable_subscription()
def conv_update(conversations):
for conversation in conversations:
message = conversation["latestMessage"]
if not message["viewed"] and message.has_key("messageSender"):
print message["messageSender"]
print message["body"]["richTextElements"][0]["richTextString"]["text"]
sms.register_delta_conversations_handler(conv_update)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Sends SMS via Telenor/Comoyo API')
parser.add_argument('--debug', action = 'store_true')
subparsers = parser.add_subparsers(title='subcommands', description='valid subcommands', help='additional help')
send_parser = subparsers.add_parser('send', help='send number "message"')
send_parser.add_argument('send', nargs=2)
login_parser = subparsers.add_parser('login', help='login username password')
login_parser.add_argument('login', nargs=2)
monitor_parser = subparsers.add_parser('monitor', help='monitor')
monitor_parser.add_argument('monitor', action="store_true")
args = parser.parse_args()
if(args.debug): logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
transport = ComoyoTransport()
login = ComoyoLogin(transport)
sms = ComoyoSMS(transport)
transport.connect()
login.activate()
arg_dict = vars(args)
if arg_dict.has_key("send"):
settings = load_settings()
login.authenticate(settings["sessionKey"], settings["userId"], settings["clientId"])
sms.send_sms(arg_dict["send"][0], arg_dict["send"][1])
elif arg_dict.has_key("login"):
(username, password) = (arg_dict["login"][0], arg_dict["login"][1])
register = login.register()
settings = login.login(username, password, register)
settings["clientId"] = register
save_settings(settings)
elif args.monitor:
settings = load_settings()
login.authenticate(settings["sessionKey"], settings["userId"], settings["clientId"])
monitor(transport, sms)
transport.rxThread.join(2**31)
transport.disconnect()