The Slack connector can be used to send messages to channels or individuals in Slack from your scripts in Peliqan. For example, you can implement data monitoring apps that send alerts to Slack in case of data problems such as missing data.
In Peliqan, go to Connections, click “Add connection”, and select Slack from the list.
Complete the authorization flow by granting the Peliqan app access to your Slack account.
Here’s an example on how to send a message to a Slack channel:
slack = pq.connect("Slack") # use your name of the connection
slack.add("message", channel = "QA", text = "Data quality alert")
Example with a custom bot name:
slack.add("message",
channel = "QA",
text = "Data quality alert",
username = "my bot"
)
Example sending a message to an individual on Slack (not a channel):
slack.add("message",
channel = "**@**some_user_name", # don't forget the "@" sign
text = "Data quality alert",
username = "my bot"
)
Sending an image (file) to Slack is done in 3 steps:
slack_api.get('uploadurl', { 'length': file_size, 'filename': "chart.png" })
requests.post(upload_url, files = {'file': open('chart.png', 'rb')})
slack_api.add('uploadcomplete', { 'file_id': file_id, 'title': 'My chart', 'channel_id': channel_id })
# Send chart to Slack
# Using pyplot to create the chart image
# Make sure to invite your private app (bot) to the channel first, in the channel type: /invite @Private app for testing
import matplotlib.pyplot as plt
import base64
import io
import requests
import os
# Example data
fruits = ['apple', 'blueberry', 'cherry', 'orange']
counts = [40, 100, 30, 55]
bar_labels = ['green', 'blue', 'red', 'orange']
bar_colors = ['tab:green', 'tab:blue', 'tab:red', 'tab:orange']
fig, ax = plt.subplots()
ax.bar(fruits, counts, label=bar_labels, color=bar_colors)
ax.set_ylabel('fruit supply')
ax.set_title('Fruit supply by kind and color')
ax.legend(title='Fruit color')
st.pyplot(fig)
plt.savefig('out.png', format='jpg')
st.image('out.png')
slack_api = pq.connect('Slack private app')
# Find channel id (or see in Slack channel > View Channel Details)
# channels = slack_api.get('channels')
# st.write(channels)
file_size = os.path.getsize('out.png')
st.write(f"File size: %s" % file_size)
# Get upload URL from Slack
result = slack_api.get('uploadurl', { 'length': file_size, 'filename': "chart.png" })
st.json(result)
upload_url = result['upload_url']
file_id = result['file_id']
channel_id = 'C037T8XSGGM' # Make sure to invite your private app (bot) to the channel first, in channel type: /invite @Private app for testing
# Upload binary file
response = requests.post(upload_url, files = {'file': open('out.png', 'rb')})
st.write("Upload file response: " + response.text)
# Complete upload and share file in channel
result = slack_api.add('uploadcomplete', { 'file_id': file_id, 'title': 'My chart', 'channel_id': channel_id })
st.json(result)