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'))
|
2012-12-03 18:23:06 +01:00
|
|
|
import humbug
|
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
|
|
|
|
|
|
|
logging.basicConfig(filename="/var/log/humbug/feedback-bot.log",
|
|
|
|
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
|
|
|
|
|
2012-12-03 18:24:49 +01:00
|
|
|
prod_client = humbug.Client(
|
2013-07-24 20:23:35 +02:00
|
|
|
email="feedback@zulip.com",
|
2012-12-03 18:23:06 +01:00
|
|
|
api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
|
|
|
verbose=True,
|
2013-06-24 21:48:32 +02:00
|
|
|
site="https://api.humbughq.com")
|
2012-12-03 18:24:49 +01:00
|
|
|
staging_client = humbug.Client(
|
2013-07-24 20:23:35 +02:00
|
|
|
email="feedback@zulip.com",
|
2012-12-03 18:23:06 +01:00
|
|
|
api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
|
|
|
verbose=True,
|
2013-06-24 21:48:32 +02:00
|
|
|
site="https://staging.humbughq.com/api")
|
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-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() + "..."
|
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,
|
2012-10-26 16:42:31 +02:00
|
|
|
"content": message["content"],
|
|
|
|
}
|
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)
|