2013-02-19 01:00:54 +01:00
|
|
|
#!/usr/bin/env python
|
2012-10-25 19:31:31 +02:00
|
|
|
import sys
|
2012-12-03 18:23:06 +01:00
|
|
|
from os import path
|
2013-01-04 19:54:49 +01:00
|
|
|
import logging
|
2012-10-25 19:31:31 +02:00
|
|
|
|
2013-05-03 17:47:30 +02:00
|
|
|
sys.path.append(path.join(path.dirname(__file__), '../api'))
|
2013-08-07 17:51:03 +02:00
|
|
|
import zulip
|
2013-10-10 02:16:51 +02:00
|
|
|
sys.path.append(path.join(path.dirname(__file__), '../zproject'))
|
|
|
|
import local_settings
|
2013-01-04 19:54:49 +01:00
|
|
|
|
|
|
|
class StreamLogger(object):
|
2013-05-02 19:36:55 +02:00
|
|
|
"""
|
|
|
|
Give a file-like interface to a logger instance.
|
|
|
|
"""
|
|
|
|
def __init__(self, logger, log_level=logging.INFO):
|
|
|
|
self.logger = logger
|
|
|
|
self.log_level = log_level
|
|
|
|
self.buffer = ""
|
2013-01-04 19:54:49 +01:00
|
|
|
|
2013-05-02 19:36:55 +02:00
|
|
|
def write(self, message):
|
|
|
|
self.buffer += message
|
2013-01-04 19:54:49 +01:00
|
|
|
|
2013-05-02 19:36:55 +02:00
|
|
|
if message[-1] == "\n":
|
|
|
|
self.logger.log(self.log_level, self.buffer.rstrip())
|
|
|
|
self.buffer = ""
|
2013-01-04 19:54:49 +01:00
|
|
|
|
2013-09-25 20:11:33 +02:00
|
|
|
logging.basicConfig(filename="/var/log/zulip/feedback-bot.log",
|
2013-01-04 19:54:49 +01:00
|
|
|
level=logging.DEBUG, format="%(asctime)s %(levelname)s\t%(message)s")
|
|
|
|
|
|
|
|
# The API prints to stdout, so capture and format those messages as well.
|
|
|
|
stdout_logger = StreamLogger(logging.getLogger("stdout"), logging.INFO)
|
|
|
|
sys.stdout = stdout_logger
|
|
|
|
|
|
|
|
stderr_logger = StreamLogger(logging.getLogger("stderr"), logging.ERROR)
|
|
|
|
sys.stderr = stderr_logger
|
|
|
|
|
2013-08-07 17:51:03 +02:00
|
|
|
prod_client = zulip.Client(
|
2013-07-24 20:23:35 +02:00
|
|
|
email="feedback@zulip.com",
|
2013-10-10 02:16:51 +02:00
|
|
|
api_key=local_settings.FEEDBACK_BOT_KEY,
|
2012-12-03 18:23:06 +01:00
|
|
|
verbose=True,
|
2013-07-24 23:53:39 +02:00
|
|
|
site="https://api.zulip.com")
|
2013-08-07 17:51:03 +02:00
|
|
|
staging_client = zulip.Client(
|
2013-07-24 20:23:35 +02:00
|
|
|
email="feedback@zulip.com",
|
2013-10-10 02:16:51 +02:00
|
|
|
api_key=local_settings.FEEDBACK_BOT_KEY,
|
2012-12-03 18:23:06 +01:00
|
|
|
verbose=True,
|
2013-07-24 23:53:39 +02:00
|
|
|
site="https://staging.zulip.com/api")
|
2012-10-25 19:31:31 +02:00
|
|
|
|
2013-10-06 19:27:15 +02:00
|
|
|
def get_ticket_number():
|
|
|
|
fn = '/var/tmp/.feedback-bot-ticket-number'
|
|
|
|
try:
|
|
|
|
ticket_number = int(open(fn).read()) + 1
|
|
|
|
except:
|
|
|
|
ticket_number = 1
|
|
|
|
open(fn, 'w').write('%d' % ticket_number)
|
|
|
|
return ticket_number
|
|
|
|
|
2012-10-25 19:31:31 +02:00
|
|
|
def forward_message(message):
|
2012-12-11 16:00:20 +01:00
|
|
|
if message["type"] != "private" or len(message["display_recipient"]) != 2:
|
2012-11-06 21:17:53 +01:00
|
|
|
return
|
2013-10-06 19:27:15 +02:00
|
|
|
|
2013-05-02 19:41:09 +02:00
|
|
|
if "sender_domain" in message:
|
|
|
|
subject = "feedback: %s (%s)" % (message["sender_email"], message["sender_domain"])
|
|
|
|
else:
|
|
|
|
subject = "feedback from %s" % message["sender_email"],
|
|
|
|
if len(subject) > 60:
|
|
|
|
subject = subject[:57].rstrip() + "..."
|
2013-10-06 19:27:15 +02:00
|
|
|
|
|
|
|
ticket_number = get_ticket_number()
|
|
|
|
content = '@support, Please ack this new request.'
|
|
|
|
content += '\n~~~'
|
|
|
|
content += '\nticket Z%03d' % (ticket_number,)
|
|
|
|
content += '\nsender: %s' % (message['sender_full_name'],)
|
|
|
|
content += '\nemail: %s' % (message['sender_email'],)
|
|
|
|
if 'sender_domain' in message:
|
|
|
|
content += '\nrealm: %s' % (message['sender_domain'],)
|
|
|
|
content += '\n~~~'
|
|
|
|
|
|
|
|
content += '\n\n'
|
|
|
|
content += message['content']
|
|
|
|
|
2012-10-26 16:42:31 +02:00
|
|
|
forwarded_message = {
|
|
|
|
"type": "stream",
|
2012-11-14 23:21:46 +01:00
|
|
|
"to": "support",
|
2013-05-02 19:41:09 +02:00
|
|
|
"subject": subject,
|
2013-10-06 19:27:15 +02:00
|
|
|
"content": content,
|
2012-10-26 16:42:31 +02:00
|
|
|
}
|
2012-11-06 20:29:40 +01:00
|
|
|
staging_client.send_message(forwarded_message)
|
2013-01-04 19:54:49 +01:00
|
|
|
logging.info("Forwarded feedback from %s" % (message["sender_email"],))
|
2012-10-25 19:31:31 +02:00
|
|
|
|
2012-11-06 20:29:40 +01:00
|
|
|
prod_client.call_on_each_message(forward_message)
|