def regex_in_command(): '''Command that uses regex to define structure''' base_command = command.message.text.split(" ")[0] if slackbot.config['prefix']: base_command = base_command[1:] if base_command == "center": return Response(text="Yeehaw! You're a Yank", channel=command.channel) elif base_command == "centre": return Response(text="I say! You appear to be a Brit", channel=command.channel) else: return Response(text="Well this is awkward... this isn't meant to \ happen", channel=command.channel)
def test_send_reaction(self): self.bot.send_reaction = MagicMock() response = Response(reaction='x', channel='channel_id', original_ts='timestamp') self.bot._execute_response(response) self.bot.send_reaction.assert_called_with(response)
def test_send_message_attachments(self): self.bot.send_message = MagicMock() response = Response( channel="channel_id", attachments=[ MessageAttachment( fallback="fallback", author_name="John Doe", author_link="https://example.com/author", author_icon="https://example.com/author.jpg", color="#36a64f", title="Title", title_link="https://example.com", image_url="https://example.com/image.jpg", text="Go to Example Website", footer="Footer text", footer_icon="https://example.com/footer.jpg", thumb_url="https://example.com/thumb.jpg", fields=[ MessageAttachmentField(title="Established", value="2008", short=False), MessageAttachmentField(title="Users", value="27 Million", short=True) ]) ]) self.bot._execute_response(response) self.bot.send_message.assert_called_with(response)
def test_send_reply(self): self.bot.send_message = MagicMock() response = Response(text='Hi test', channel='channel_id', original_ts='timestamp') self.bot._execute_response(response) self.bot.send_message.assert_called_with(response)
def fire_and_forget(channel): ''' Example function used by background_processing().Sends a message outside of a command context. ''' sleep(3) slackbot.send_message( Response(text="Background Process Message", channel=channel))
def test_ephemeral_defaults_to_false(self): self.bot.slack_client = MagicMock() message = Response(channel="channel_id", text="Test text") self.bot.send_message(message) self.bot.slack_client.api_call.assert_called_with('chat.postMessage', channel='channel_id', as_user=True, attachments='null', text='Test text', user=None)
def get_message_with_attachment(): ''' A command that posts a message with a Slack attachment Read more: https://api.slack.com/docs/message-attachments ''' attachments = [{ "title": "Here's the title of the attachment", "text": "...and here's the text", "footer": "Teeny tiny footer text" }] return Response(channel=command.channel, attachments=attachments)
def test_send_message(self): self.bot.slack_client = MagicMock() self.bot.slack_client.api_call = MagicMock(return_value="test") message = Response(text='Hi test', channel='channel_id') self.bot.send_message(message) self.bot.slack_client.api_call.assert_called_with('chat.postMessage', channel='channel_id', text='Hi test', as_user=True, attachments='null')
def test_errors_with_reaction_and_reply(self): response = Response(reaction='x', text='test', channel='channel_id', original_ts='timestamp') with self.assertRaises(ValueError) as context: self.bot._execute_response(response) error_msg = 'Response objects with an original timestamp can ' \ + 'only have one of the attributes: Reaction, ' \ + 'Text' self.assertTrue(error_msg in str(context.exception))
def test_ephemeral(self): self.bot.slack_client = MagicMock() message = Response(channel="channel_id", ephemeral=True, text="Test text", user="******") self.bot.send_message(message) self.bot.slack_client.api_call.assert_called_with('chat.postEphemeral', channel='channel_id', as_user=True, attachments='null', text='Test text', user='******')
def test_basic_functionality(self): self.bot.slack_client = MagicMock() self.bot.slack_client.api_call = MagicMock() response = Response(reaction='x', channel='channel_id', original_ts='timestamp') self.bot.send_reaction(response) self.bot.slack_client.api_call \ .assert_called_with("reactions.add", channel=response.channel, timestamp=response.original_ts, name=response.reaction, as_user=True)
def test_send_reply(self): self.bot.slack_client = MagicMock() self.bot.slack_client.api_call = MagicMock(return_value="test") message = Response(text='Hi test', channel='channel_id', original_ts='timestamp') self.bot.send_message(message) self.bot.slack_client.api_call \ .assert_called_with('chat.postMessage', channel='channel_id', text='Hi test', thread_ts='timestamp', as_user=True, attachments='null', user=None)
def test_send_string(self): self.bot.send_message = MagicMock() message = phial.wrappers.Message(text="!test", channel="channel_id", user="******", timestamp="timestamp") command_instance = phial.wrappers.Command(command_pattern="base", channel="channel_id", args={}, user="******", message=message) phial.globals._command_ctx_stack.push(command_instance) self.bot._execute_response("string") expected_response = Response(text='string', channel='channel_id') self.bot.send_message.assert_called_with(expected_response) phial.globals._command_ctx_stack.pop()
def get_message_with_attachment(): '''A command that posts a message with a Slack attachment''' attachments = [ MessageAttachment( title="Here's a message, it has 2 attachment fields", title_link="https://api.slack.com/docs/message-attachments", text="This message has some text!", fields=[ MessageAttachmentField( title="Here's the first attachment field", value="And here's it's body", short=True), MessageAttachmentField(title="...And here's the second", value="And here's it's body", short=True) ]) ] return Response(channel=command.channel, attachments=attachments)
def reply(): '''Simple command that replies to the original message in a thread''' return Response(text="this is a thread", channel=command.channel, original_ts=command.message_ts)
def react(): '''Simple command that reacts to the original message''' return Response(reaction="x", channel=command.channel, original_ts=command.message_ts)
def hello(name, from_): '''Simple command with two arguments which replies to a message''' return Response(text="Hi {0}, from {1}".format(name, from_), channel=command.channel)
def hello(name): '''Simple command with argument which replies to a message''' return Response(text="Hi {0}".format(name), channel=command.channel)
def shceduled_function(): slackbot.send_message( Response(text="Hey! Hey Listen!", channel=SCHEDULED_CHANNEL))
def test_send_message(self): self.bot.slack_client = MagicMock() message = Response(channel="channel_id", attachments=[{ "fallback": "fallback", "author_name": "John Doe", "author_link": "https://example.com/author", "author_icon": "https://example.com/author.jpg", "color": "#36a64f", "title": "Title", "title_link": "https://example.com", "image_url": "https://example.com/image.jpg", "text": "Go to Example Website", "footer": "Footer text", "footer_icon": "https://example.com/footer.jpg", "thumb_url": "https://example.com/thumb.jpg", "fields": [{ "title": "Established", "value": "2008", "short": False }, { "title": "Users", "value": "27 Million", "short": True }] }]) self.bot.send_message(message) expected_attachments = """ [ { "fallback":"fallback", "author_name":"John Doe", "author_link":"https://example.com/author", "author_icon":"https://example.com/author.jpg", "color":"#36a64f", "title":"Title", "title_link":"https://example.com", "image_url":"https://example.com/image.jpg", "text":"Go to Example Website", "footer":"Footer text", "footer_icon":"https://example.com/footer.jpg", "thumb_url":"https://example.com/thumb.jpg", "fields":[ { "title":"Established", "value":"2008", "short":false }, { "title":"Users", "value":"27 Million", "short":true } ] } ] """ self.bot.slack_client.api_call.assert_called_with( 'chat.postMessage', channel='channel_id', as_user=True, attachments=json.dumps(json.loads(expected_attachments)), text=None, user=None)
def regex_in_command_with_arg(arg): '''Command that uses regex to define structure with an arg''' base_command = command.message.text.split(" ")[0] return Response(text="My favourite {0} is {1}".format(base_command, arg), channel=command.channel)
def test_send_message(self): self.bot.send_message = MagicMock() response = Response(text='Hi test', channel='channel_id') self.bot._execute_response(response) self.bot.send_message.assert_called_with(response)