Slack¶
Integrate your code with Slack to send messages, files and more.
Installation¶
Setting up the API Token¶
To use this pugin you need a Slack API token.
Follow the instructions here to learn how to obtain an API token.
Importing the Plugin¶
After you installed this package, the next step is to import the package into your code and start using the functions.
Sending a Simple Message¶
Here is the expected output:
Send a more elaborate message¶
You can compose and send complex messages with the Message
class.
from botcity.plugins.slack import BotSlackPlugin
client = BotSlackPlugin(slack_token='<your_token>', channel='<channel_to_interact>')
message = Message(
text="A more elaborate message",
title="title test",
color=Color.RED
)
# Sets author
message.author = Author(author_name="Author test",
author_icon="https://placeimg.com/16/16/people",
author_link="http://flickr.com/bobby/")
# Sets thumb_url
message.thumb_url = "http://placekitten.com/g/200/200"
# Sets Footer
message.footer = Footer(footer="Footer test",
footer_icon="https://platform.slack-edge.com/img/default_application_icon.png")
# Sets fields
message.fields = [Field(title="Field test", value="values test", short=False),
Field(title="Field test 2", value="values test 2", short=True),
Field(title="Field test 3", value="values test 3", short=True)]
#Send message
response_message = client.send_message(message=message)
Here is the expected output:
Updating a Message¶
Here is the expected output:
Retrieving messages¶
New in version 0.1.1
You can retrieve messages based on the timestamp of any message obtained through the message response. And It is also possible to reply to messages. See the examples.
Replying a Message¶
from botcity.plugins.slack import BotSlackPlugin
client = BotSlackPlugin(slack_token='<your_token>', channel='<channel_to_interact>')
response_message = client.send_simple_message(text="This is a test to reply the message in the thread")
reply = client.reply(response=client.get_replies(response_message),
msg="This is a reply to message")
Waiting for new messages¶
Using wait for reply, it is possible to wait for a message to be received. This method keeps looking for answers until it finds them, within a time limit determined via the timeout parameter.
from botcity.plugins.slack import BotSlackPlugin
client = BotSlackPlugin(slack_token='<your_token>', channel='<channel_to_interact>')
#Send message
response_message = client.send_simple_message(text="This is a test to retrieving messages in the thread")
# Waiting for someone to reply to the message sent earlier (In this case it was answered in the slack "Test Retrieving messages")
reply = client.wait_for_reply(response=response_message)
print(reply)
Here is the expected output:
Get Replies and Get Text Replies¶
Nothing prevents to get the replies separately either. If you have the response of one or more messages.
from botcity.plugins.slack import BotSlackPlugin
client = BotSlackPlugin(slack_token='<your_token>', channel='<channel_to_interact>')
messages = client.get_replies(response_message)
message_text = client.get_text_replies(messages)
Note
Remembering that 'response_message' can be obtained through the 'send_ message' and 'send_simple_message' method, which returns 'response_message'.
Uploading a File¶
Here is the expected output:
Deleting a Message¶
from botcity.plugins.slack import BotSlackPlugin
client = BotSlackPlugin(slack_token='<your_token>', channel='<channel_to_interact>')
#response_message = client.send_message(message=Message(....))
response_message = client.send_simple_message(text="text to delete")
client.delete_message(message=response_message)
Deleting a File¶
from botcity.plugins.slack import BotSlackPlugin
client = BotSlackPlugin(slack_token='<your_token>', channel='<channel_to_interact>')
load_file_response = client.load_file(file="your-path-file", text_for_file="text for precedes file", title_for_file="the title of file")
client.delete_file(file=load_file_response)