-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.py
73 lines (56 loc) · 2.09 KB
/
message.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
"""
sparkles.message
~~~~~~~~~~~~~~~~
Message service integration.
http://web.sarathlakshman.com/Articles/XMPP.pdf
"""
import logging
import xmpp
from django.conf import settings
from twilio.rest import TwilioRestClient
from sparkles import models as db
_client = None
logger = logging.getLogger(__name__)
def xmpp_client():
"""Lazily connect/auth XMPP client"""
global _client
if _client and _client.isConnected():
return _client
jid = xmpp.JID(settings.XMPP_JID)
client = xmpp.Client(jid.domain)
client.connect(server=settings.XMPP_SERVER)
client.auth(jid.node, settings.XMPP_PASSWORD, jid.resource)
client.RegisterHandler('message', xmpp_on_message)
client.sendInitPresence(requestRoster=0)
_client = client
client.jid = jid
return client
def xmpp_send(to, content):
from_ = settings.XMPP_JID
logger.info("xmpp %s -> %s: %s", from_, to, content)
if not getattr(settings, 'TEST_MODE', False):
xmpp_client().send(xmpp.Message(to, content))
db.XmppMessage.objects.create(to_addr=to,
from_addr=from_,
content=content,
is_egress=True)
def xmpp_on_message(client, message):
to = message.getTo()
from_ = message.getFrom()
content = message.getBody()
db.XmppMessage.objects.create(to_addr=to,
from_addr=from_,
content=content,
is_egress=False)
def sms_send(to, content):
"""Send an sms text message"""
assert len(content) <= 160
from_ = settings.TWILIO_PHONE
logger.info("sms %s -> %s: %s", from_, to, content)
if not getattr(settings, 'TEST_MODE', False):
twil = TwilioRestClient(settings.TWILIO_SID, settings.TWILIO_TOKEN)
twil.sms.messages.create(to=to, from_=from_, body=content)
db.SmsMessage.objects.create(to_addr=to,
from_addr=from_,
content=content,
is_egress=True)