forked from metajack/stenotype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stenotype.py
165 lines (125 loc) · 4.68 KB
/
stenotype.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
ACCOUNT_SID = 'XXX'
AUTH_TOKEN = 'XXX'
CALLER_ID = '555-555-5555'
from twisted.web.resource import Resource
from twisted.web.server import NOT_DONE_YET
import txilio
from twisted.python import log
from twisted.web import resource
from twisted.words.xish import domish, xpath
from wokkel.subprotocols import XMPPHandler
from wokkel.xmppim import AvailablePresence, Presence
from xml.dom.minidom import parse, parseString
import simplejson as json
NS_MUC = 'http://jabber.org/protocol/muc'
NS_XHTML_IM = 'http://jabber.org/protocols/xhtml-im'
NS_XHTML_W3C = 'http://www.w3.org/1999/xhtml'
class StenoBot(XMPPHandler):
messageTypes = ["chat", "groupchat"]
number = ""
def __init__(self, room, nick):
XMPPHandler.__init__(self)
self.room = room
self.nick = nick
def connectionMade(self):
self.send(AvailablePresence())
# add handlers
# join room
pres = Presence()
pres['to'] = self.room + '/' + self.nick
pres.addElement((NS_MUC, 'x'))
self.send(pres)
def connectionInitialized(self):
self.xmlstream.addObserver("/message[@type='groupchat']", self._onMessage)
# self.xmlstream.addObserver("/message/x[@xmlns!='jabber:x:delay']", self._onMessage)
def _onMessage(self, message):
if message.handled:
return
messageType = message.getAttribute("type")
if messageType == 'error':
return
if messageType not in self.messageTypes:
message["type"] = 'normal'
self.onMessage(message)
def onMessage(self, msg):
"""
Called when a message stanza was received.
"""
if xpath.matches("/message/x[@xmlns='jabber:x:delay']", msg) == 1:
print "skip"
return
print "process"
# if msg.x and msg.x.uri == "jabber:x:delay":
# print "DELAYED "+str(msg.x) + msg.x.ur
# if msg.x and msg.x.hasAttribute("xmlns") and msg.x['xmlns'] == "jabber:x:delay":
# return
if msg["type"] == 'groupchat' and hasattr(msg, "body"):
print (msg.body)
content = str(msg.body)
if(content[:5].lower() == "call "):
number = content[5:]
print "GOT A NUMBER!! "+ number
# if number != self.number:
# self.number = number
data = {
'Caller' : CALLER_ID,
'Called' : number,
'Url' : 'http://dmt.im/twilio/twilio.xml',
# 'SendDigits' : '965376#1'
}
account = txilio.Account(ACCOUNT_SID, AUTH_TOKEN)
d = account.request('Calls', 'POST', data)
self.notify("CALLING "+number)
def _done(res):
print str(res)
request.write(str(res))
request.finish()
d.addBoth(_done)
return NOT_DONE_YET
def notify(self, data):
# build the messages
text = []
html = []
link = r"<a href='%s' name='%s'>%s</a>"
text.append(data)
msg = domish.Element((None, 'message'))
msg['to'] = self.room
msg['type'] = 'groupchat'
msg.addElement('body', content=''.join(text))
wrap = msg.addElement((NS_XHTML_IM, 'html'))
body = wrap.addElement((NS_XHTML_W3C, 'body'))
body.addRawXml(''.join(html))
self.send(msg)
class StenotypeResource(Resource):
isLeaf = True
def __init__(self, bot):
resource.Resource.__init__(self)
self.bot = bot
def render_GET(self, request):
print 'got a get'
#start up the listener?
#
data = {
'Caller' : CALLER_ID,
'Called' : '505-920-6781',
'Url' : 'http://dmt.im/twilio/twilio.xml',
# 'SendDigits' : '965376#1'
}
account = txilio.Account(ACCOUNT_SID, AUTH_TOKEN)
d = account.request('Calls', 'POST', data)
def _done(res):
print str(res)
request.write(str(res))
request.finish()
d.addBoth(_done)
return NOT_DONE_YET
def getText(nodelist):
rc = []
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc.append(node.data)
return ''.join(rc)
def render_POST(self, req):
#data = req.content.readlines()
self.bot.notify(req.args['TranscriptionText'][0])
return '<?xml version="1.0" encoding="UTF-8"?><Response><Record transcribe="true" transcribeCallback="http://dmt.im:1234/" playBeep="false" maxLength="20" timeout="30" /> </Response>'