2013-02-18 19:00:54 -05:00
|
|
|
#!/usr/bin/env python
|
2012-10-25 13:31:31 -04:00
|
|
|
import sys
|
2012-12-03 12:23:06 -05:00
|
|
|
from os import path
|
2013-01-04 13:54:49 -05:00
|
|
|
import logging
|
2012-10-25 13:31:31 -04:00
|
|
|
|
2013-05-03 11:47:30 -04:00
|
|
|
sys.path.append(path.join(path.dirname(__file__), '../api'))
|
2013-08-07 11:51:03 -04:00
|
|
|
import zulip
|
2013-01-04 13:54:49 -05:00
|
|
|
|
|
|
|
class StreamLogger(object):
|
2013-05-02 13:36:55 -04: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 13:54:49 -05:00
|
|
|
|
2013-05-02 13:36:55 -04:00
|
|
|
def write(self, message):
|
|
|
|
self.buffer += message
|
2013-01-04 13:54:49 -05:00
|
|
|
|
2013-05-02 13:36:55 -04:00
|
|
|
if message[-1] == "\n":
|
|
|
|
self.logger.log(self.log_level, self.buffer.rstrip())
|
|
|
|
self.buffer = ""
|
2013-01-04 13:54:49 -05:00
|
|
|
|
2013-09-25 14:11:33 -04:00
|
|
|
logging.basicConfig(filename="/var/log/zulip/feedback-bot.log",
|
2013-01-04 13:54:49 -05: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 11:51:03 -04:00
|
|
|
prod_client = zulip.Client(
|
2013-07-24 14:23:35 -04:00
|
|
|
email="feedback@zulip.com",
|
2012-12-03 12:23:06 -05:00
|
|
|
api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
|
|
|
verbose=True,
|
2013-07-24 17:53:39 -04:00
|
|
|
site="https://api.zulip.com")
|
2013-08-07 11:51:03 -04:00
|
|
|
staging_client = zulip.Client(
|
2013-07-24 14:23:35 -04:00
|
|
|
email="feedback@zulip.com",
|
2012-12-03 12:23:06 -05:00
|
|
|
api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
|
|
|
verbose=True,
|
2013-07-24 17:53:39 -04:00
|
|
|
site="https://staging.zulip.com/api")
|
2012-10-25 13:31:31 -04:00
|
|
|
|
|
|
|
def forward_message(message):
|
2012-12-11 10:00:20 -05:00
|
|
|
if message["type"] != "private" or len(message["display_recipient"]) != 2:
|
2012-11-06 15:17:53 -05:00
|
|
|
return
|
2013-05-02 13:41:09 -04: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() + "..."
|
2012-10-26 10:42:31 -04:00
|
|
|
forwarded_message = {
|
|
|
|
"type": "stream",
|
2012-11-14 17:21:46 -05:00
|
|
|
"to": "support",
|
2013-05-02 13:41:09 -04:00
|
|
|
"subject": subject,
|
2012-10-26 10:42:31 -04:00
|
|
|
"content": message["content"],
|
|
|
|
}
|
2012-11-06 14:29:40 -05:00
|
|
|
staging_client.send_message(forwarded_message)
|
2013-01-04 13:54:49 -05:00
|
|
|
logging.info("Forwarded feedback from %s" % (message["sender_email"],))
|
2012-10-25 13:31:31 -04:00
|
|
|
|
2012-11-06 14:29:40 -05:00
|
|
|
prod_client.call_on_each_message(forward_message)
|