From 5f77301bffc8d8bcd549f0858cba6dbdf3c6645f Mon Sep 17 00:00:00 2001 From: Randall Barnhart Date: Thu, 15 Sep 2016 06:16:39 -0600 Subject: [PATCH] Handling to provide echo of user message and handling unicode chars in message strings. (#13) --- .gitignore | 62 ++++++++++++++++++++++++++++++++++++++++++++ bot/event_handler.py | 2 ++ bot/messenger.py | 6 +++-- 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2d8f0f5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,62 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +venv/ +.idea/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +.DS_Store +plot.png \ No newline at end of file diff --git a/bot/event_handler.py b/bot/event_handler.py index f059622..0922501 100644 --- a/bot/event_handler.py +++ b/bot/event_handler.py @@ -48,5 +48,7 @@ def _handle_message(self, event): self.msg_writer.write_joke(event['channel']) elif 'attachment' in msg_txt: self.msg_writer.demo_attachment(event['channel']) + elif 'echo' in msg_txt: + self.msg_writer.send_message(event['channel'], msg_txt) else: self.msg_writer.write_prompt(event['channel']) diff --git a/bot/messenger.py b/bot/messenger.py index 8522bc8..6cacf9e 100644 --- a/bot/messenger.py +++ b/bot/messenger.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + import logging import random @@ -12,9 +14,9 @@ def send_message(self, channel_id, msg): # in the case of Group and Private channels, RTM channel payload is a complex dictionary if isinstance(channel_id, dict): channel_id = channel_id['id'] - logger.debug('Sending msg: {} to channel: {}'.format(msg, channel_id)) + logger.debug('Sending msg: %s to channel: %s' % (msg, channel_id)) channel = self.clients.rtm.server.channels.find(channel_id) - channel.send_message("{}".format(msg.encode('ascii', 'ignore'))) + channel.send_message(msg) def write_help_message(self, channel_id): bot_uid = self.clients.bot_user_id()