Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send Image to Slack as slack bot via slackclient?

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?

like image 257
monkut Avatar asked Sep 20 '18 04:09

monkut


People also ask

How do I send a picture in Slack API?

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.

How do I upload an image to Slack?

Click Edit profile. Under Profile photo, click Upload an Image. Select a photo. Adjust the framing, then click Save.

How do I send an image to Slack in Python?

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.

Is it possible to post files to Slack using the incoming Webhook?

No, its is not possible to upload files through an incoming Webhook.


1 Answers

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

like image 174
Guillem Castro Avatar answered Oct 26 '22 22:10

Guillem Castro