-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrt_bot.py
231 lines (184 loc) · 7.44 KB
/
rt_bot.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# -*- coding: utf-8 -*-
# This file is part of rt_bot.
#
# rt_bot is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# rt_bot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with rt_bot. If not, see <http://www.gnu.org/licenses/>.
from twisted.application import service
from twisted.python import log
from twisted.words.protocols.jabber.jid import JID
from wokkel.client import XMPPClient
from wokkel.muc import MUCClient
from base_bot import BaseMUCBot
import re
import ConfigParser
from rtkit.resource import RTResource
from rtkit.authenticators import BasicAuthenticator, CookieAuthenticator
from rtkit.errors import RTResourceError
from rtkit import set_logging
import urllib
import logging
#set_logging('debug')
logger = logging.getLogger('rtkit')
class NoResponse(Exception):
pass
class RTBot(BaseMUCBot):
def __init__(self, roomJID, nick, rt_url, rt_user, rt_pwd, roomPassword, rt_display_url=None, output_format="extended"):
BaseMUCBot.__init__(self, roomJID, nick, roomPassword)
self.resource = RTResource(rt_url + 'REST/1.0/', rt_user, rt_pwd, CookieAuthenticator)
self.rt_url = rt_url
self.output_format = output_format
if rt_display_url is None:
self.rt_display_url = rt_url
else:
self.rt_display_url = rt_display_url
def handleGroupChat(self, room, user, message):
if self.nick in message.body:
if 'hi' in message.body or 'hello' in message.body:
body = u"%s: greetings stranger arrr arrr!" % (user.nick)
self.groupChat(self.roomJID, body)
elif 'open' in message.body:
queue = rt_default_queue
m = re.search(r'open\s([^\s]+)', message.body)
if m:
queue = m.group(1)
if queue == 'all':
queue_query = ''
else:
queue_query = " AND QUEUE='" + queue + "'"
query = "Owner = 'Nobody' AND ( Status = 'new' OR Status = 'open' )" + queue_query
query = urllib.quote(query)
prepend_string = 'Queue: ' + queue + '\n\n'
try:
ret = self.rtquery(query, prepend_string)
self.groupChat(self.roomJID, ret)
except NoResponse as e:
self.groupChat(self.roomJID, e.message)
elif 'search' in message.body:
m = re.search(r'search\s([^\s]+)', message.body)
if m:
search = m.group(1)
if len(search) >= 3:
query = "Subject LIKE '" + search + "'"
query = urllib.quote(query)
try:
ret = self.rtquery(query)
self.groupChat(self.roomJID, ret)
except NoResponse as e:
self.groupChat(self.roomJID, e.message)
else:
self.groupChat(self.roomJID, 'Please enter at least 3 characters!')
else:
self.groupChat(self.roomJID, 'Nothing to search for!')
elif 'ticket' in message.body:
m = re.search(r'ticket\s([0-9]+)', message.body)
if m:
ticket_id = m.group(1)
try:
ret = self.rtticket(ticket_id)
self.groupChat(self.roomJID, ret)
except NoResponse as e:
self.groupChat(self.roomJID, e.message)
else:
self.groupChat(self.roomJID, 'Nothing to search for!')
elif 'help' in message.body:
self.groupChat(self.roomJID, 'Usage:')
self.groupChat(self.roomJID, 'open <queue name> - display all unassigned open and new tickets')
self.groupChat(self.roomJID, ' queue name defaults to '+rt_default_queue)
self.groupChat(self.roomJID, 'search <subject> - search for a subject')
self.groupChat(self.roomJID, 'ticket <ticket-id> - display ticket information')
def rtquery(self, query, prepend_text=''):
ret = '\n'
if self.output_format == "extended":
ret += prepend_text
try:
response = self.resource.get(path='search/ticket?query=' + query)
if len(response.parsed) > 0:
first = True
for r in response.parsed:
for t in r:
if not first and self.output_format == "extended":
ret += '\n'
else:
first = False
logger.info(t)
t_id = t[0]
t_display = self.rt_display_url + 'Ticket/Display.html?id=' + t_id
t_title = unicode(t[1], errors='replace')
if self.output_format == "extended":
ret += 'Ticket#: ' + t_id + '\n'
ret += 'URL: ' + t_display + '\n'
ret += 'Title: ' + t_title + '\n'
elif self.output_format == "compact":
ret += '#'+t_id+","+t_title+","+t_display+"\n"
else:
raise NoResponse('Nothing found!')
except RTResourceError as e:
logger.error(e.response.status_int)
logger.error(e.response.status)
logger.error(e.response.parsed)
return ret
def rtticket(self, ticket_id):
ret = '\n'
try:
response = self.resource.get(path='ticket/' + ticket_id + '/show')
if len(response.parsed) > 0:
rsp_dict = {}
for r in response.parsed:
for t in r:
rsp_dict[t[0]] = unicode(t[1], 'utf-8')
ret += 'Ticket#: ' + ticket_id + '\n'
t_display = self.rt_url + 'Ticket/Display.html?id=' + ticket_id
ret += 'URL: ' + t_display + '\n'
ret += 'Title: ' + rsp_dict['Subject'] + '\n'
ret += 'Queue: ' + rsp_dict['Queue'] + '\n'
ret += 'Owner: ' + rsp_dict['Owner'] + '\n'
ret += 'Creator: ' + rsp_dict['Creator'] + '\n'
ret += 'Status: ' + rsp_dict['Status'] + '\n'
ret += 'Requestors: ' + rsp_dict['Requestors'] + '\n'
ret += 'Created: ' + rsp_dict['Created'] + '\n'
ret += 'LastUpdated: ' + rsp_dict['LastUpdated'] + '\n'
#ret += ': ' + rsp_dict[''] + '\n'
if 'Resolved' in rsp_dict:
ret += 'Resolved: ' + rsp_dict['Resolved'] + '\n'
else:
raise NoResponse('Nothing found!')
except RTResourceError as e:
logger.error(e.response.status_int)
logger.error(e.response.status)
logger.error(e.response.parsed)
#return unicode(ret, 'utf-8')
return ret
# Output format - extended, compact
output_format = "extended"
# Configuration parameters
config = ConfigParser.RawConfigParser()
config.read('bot.conf')
myJID = JID(config.get('Connection', 'my_jid'))
roomJID = JID(config.get('Connection', 'room_jid'))
roomPassword = config.get('Connection', 'room_password')
my_nick = config.get('Connection', 'my_nick')
my_secret = config.get('Connection', 'my_secret')
rt_url = config.get('RT', 'url')
rt_display_url = config.get('RT','display_url')
rt_user = config.get('RT', 'user')
rt_pwd = config.get('RT', 'pwd')
rt_default_queue = config.get('RT','default_queue')
LOG_TRAFFIC = False
#LOG_TRAFFIC = True
# Set up the Twisted application
application = service.Application("MUC Client")
client = XMPPClient(myJID, my_secret)
client.logTraffic = LOG_TRAFFIC
client.setServiceParent(application)
mucHandler = RTBot(roomJID, my_nick, rt_url, rt_user, rt_pwd, roomPassword, rt_display_url, output_format)
mucHandler.setHandlerParent(client)