Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
how to upload a file with SlackClient library
from slackclient import SlackClient
class SlackBot(object):
def __init__(self, logger, slackclient):
self.logger = logger or logging.getLogger(__name__)
self.slack_client = slackclient or SlackClient(os.environ.get('SLACK_BOT_TOKEN'))
def send_message(self, msg, attachment=None, channel="#general"):
Plain send text method
res = self.slack_client.api_call("chat.postMessage", channel=channel, text=msg, attachments=attachment, as_user=True)
if not res.get('ok'):
self.logger.error('error: {}', res.get('error'))
self.logger.exception('send_message failed')
def upload_file(self, filename, content, channel):
upload a long text as a file
ret = self.slack_client.api_call("files.upload", filename=filename, channels=channel, file= io.BytesIO(str.encode(content)))
if not 'ok' in ret or not ret['ok']:
# error
self.logger.error('fileUpload failed %s', ret['error'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment