Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

Commit c81bdfe

Browse files
author
Jeff Ammons
committed
Fix unicode error in py2.7 for example plugin
Strings should now be passed to the plugins as unicode values in py2.7 so we have to make sure that we don't convert those into ascii by using python str instead of u'' strings.
1 parent 2b9e9b7 commit c81bdfe

File tree

8 files changed

+41
-11
lines changed

8 files changed

+41
-11
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ This will print the incoming message json (dict) to the screen where the bot is
6767

6868
Plugins having a method defined as ```catch_all(data)``` will receive ALL events from the websocket. This is useful for learning the names of events and debugging.
6969

70+
Note: If you're using Python 2.x, the incoming data should be a unicode string, be careful you don't coerce it into a normal str object as it will cause errors on output. You can add `from __future__ import unicode_literals` to your plugin file to avoid this.
71+
7072
####Outgoing data
7173
Plugins can send messages back to any channel, including direct messages. This is done by appending a two item array to the outputs global array. The first item in the array is the channel ID and the second is the message text. Example that writes "hello world" when the plugin is started:
7274

doc/example-plugins/canary.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import unicode_literals
2+
# don't convert to ascii in py2.7 when creating string to return
3+
14
import time
25
outputs = []
36

doc/example-plugins/counter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import unicode_literals
2+
# don't convert to ascii in py2.7 when creating string to return
3+
14
import time
25
crontable = []
36
outputs = []

doc/example-plugins/repeat.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import unicode_literals
2+
# don't convert to ascii in py2.7 when creating string to return
3+
14
crontable = []
25
outputs = []
36

doc/example-plugins/todo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
from __future__ import print_function
2+
from __future__ import unicode_literals
3+
# don't convert to ascii in py2.7 when creating string to return
4+
25
import os
36
import pickle
47

rtmbot/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python
2+
from __future__ import unicode_literals
23
import sys
34
import glob
45
import os
@@ -101,8 +102,7 @@ def output(self):
101102
if limiter:
102103
time.sleep(.1)
103104
limiter = False
104-
message = output[1]
105-
channel.send_message("{}".format(message))
105+
channel.send_message(output[1])
106106
limiter = True
107107

108108
def crons(self):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
setup(
66
name='rtmbot',
7-
version='0.10',
7+
version='0.2.0',
88
description='A Slack bot written in python that connects via the RTM API.',
99
author='Ryan Huber',
1010
author_email='[email protected]',

tests/test_rtmbot_core.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# -*- coding: utf-8 -*-
22
try:
3-
from unittest.mock import Mock
3+
from unittest.mock import Mock, create_autospec
44
except ImportError:
5-
from mock import Mock
5+
from mock import Mock, create_autospec
66

77
from testfixtures import LogCapture
8-
from rtmbot.core import RtmBot
8+
from slackclient import SlackClient, _channel, _server, _util
9+
from rtmbot.core import RtmBot, Plugin
910

1011
def init_rtmbot():
1112
''' Initializes an instance of RTMBot with some default values '''
@@ -33,14 +34,21 @@ def test_output():
3334
''' Test that sending a message behaves as expected '''
3435
rtmbot = init_rtmbot()
3536

36-
# Mock the slack_client object
37-
slackclient_mock = Mock()
38-
channel_mock = Mock()
37+
# Mock the slack_client object with Server, Channel objects and needed methods
38+
slackclient_mock = create_autospec(SlackClient)
39+
server_mock = create_autospec(_server.Server)
40+
41+
# Mock Server with channels method and correct return value
42+
slackclient_mock.server = server_mock
43+
searchlist_mock = create_autospec(_util.SearchList)
44+
server_mock.channels = searchlist_mock
45+
channel_mock = create_autospec(_channel.Channel)
3946
slackclient_mock.server.channels.find.return_value = channel_mock
47+
4048
rtmbot.slack_client = slackclient_mock
4149

4250
# mock the plugin object to return a sample response
43-
plugin_mock = Mock()
51+
plugin_mock = create_autospec(Plugin)
4452
plugin_mock.do_output.return_value = [['C12345678', 'test message']]
4553
rtmbot.bot_plugins.append(plugin_mock)
4654

@@ -50,10 +58,18 @@ def test_output():
5058
# test that the output matches the expected value
5159
channel_mock.send_message.assert_called_with('test message')
5260

53-
# test that unicode messages work as expected
61+
# test that emoji messages work as expected
5462
channel_mock.reset_mock()
5563
plugin_mock.reset_mock()
5664
plugin_mock.do_output.return_value = [['C12345678', '🚀 testing']]
5765
rtmbot.output()
5866

5967
channel_mock.send_message.assert_called_with('🚀 testing')
68+
69+
# test that unicode messages work as expected
70+
channel_mock.reset_mock()
71+
plugin_mock.reset_mock()
72+
plugin_mock.do_output.return_value = [['C12345678', 'ù hœø3ö']]
73+
rtmbot.output()
74+
75+
channel_mock.send_message.assert_called_with('ù hœø3ö')

0 commit comments

Comments
 (0)