I'm building a simple bot to periodically generate charts as images and post to slack via the python slackclient package.
I've managed to send a bot message with the code below:
def post_message_to_channel(self, channel_id, message=DEFAULT_TEST_MESSAGE):
sc = SlackClient(TOKEN)
sc.api_call(
"chat.postMessage",
channel=channel_id,
username='mybot',
text='test text message'
)
But when I try to do the same with a file upload, the image is sent correctly, but it shows my name, and not the specified bot name:
def post_image_to_channel(self, channel_name, filepath, tile='Test Upload'):
sc = SlackClient(TOKEN)
with open(filepath, 'rb') as file_content:
sc.api_call(
"files.upload",
channels=channel_id,
file=file_content,
title=tile,
username='mybot',
)
Looking at the slack API documentation for files.upload, it appears that the username
is not available.
How can I send the files.upload image using the bot name?
You can send images as part of the attachments of a message. That can be either a full image or a thumbnail. Just add the image_url property for full images or the thumb_url property for a thumbnail image with a url to an image to your attachment and it will be displayed under your message.
Click Edit profile. Under Profile photo, click Upload an Image. Select a photo. Adjust the framing, then click Save.
import slack import json import os def pureimg(data1): data1 = '[{"text": "", "image_url": "'+data1+'"}]' data1 = [json. loads(data1[1:-1])] return data1 #This function will make the image url to correct format. slacker = slack. WebClient(token='your-token-here') payoff=os.
No, its is not possible to upload files through an incoming Webhook.
You have to send the file using a Bot User Token of a bot-user. You can find more info at the official documentation https://api.slack.com/bot-users
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With