python-zulip-api/bots/feedback-bot
Tim Abbott 9e1c407ec3 [manual] Rename feedback bot to zulip.com domain.
Just before this is pushed to prod, we need to rename the Humbug feedback
bot in the database using:

./manage.py change_user_email feedback@humbughq.com feedback@zulip.com
/etc/init.d/memcached restart

and we also need to update and restart feedback-bot in its deployed
location.

No action is required on pushing this to staging, but in between when
this is pushed to staging and when it is pushed to prod (and that
transition performed), feedback will not work on staging.

(imported from commit 73fc36f680b978f3aebae5df1822918ae4d4e952)
2013-07-25 17:16:54 -04:00

65 lines
2 KiB
Python
Executable file

#!/usr/bin/env python
import sys
from os import path
import logging
sys.path.append(path.join(path.dirname(__file__), '../api'))
import humbug
class StreamLogger(object):
"""
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 = ""
def write(self, message):
self.buffer += message
if message[-1] == "\n":
self.logger.log(self.log_level, self.buffer.rstrip())
self.buffer = ""
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
prod_client = humbug.Client(
email="feedback@zulip.com",
api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
verbose=True,
site="https://api.humbughq.com")
staging_client = humbug.Client(
email="feedback@zulip.com",
api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
verbose=True,
site="https://staging.humbughq.com/api")
def forward_message(message):
if message["type"] != "private" or len(message["display_recipient"]) != 2:
return
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() + "..."
forwarded_message = {
"type": "stream",
"to": "support",
"subject": subject,
"content": message["content"],
}
staging_client.send_message(forwarded_message)
logging.info("Forwarded feedback from %s" % (message["sender_email"],))
prod_client.call_on_each_message(forward_message)