Skip to content

Instantly share code, notes, and snippets.

@heejune
Created February 28, 2017 02:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heejune/9d38fb0a71f3a78dc88d469e7cdc0251 to your computer and use it in GitHub Desktop.
Save heejune/9d38fb0a71f3a78dc88d469e7cdc0251 to your computer and use it in GitHub Desktop.
how to upload a file with SlackClient library
from slackclient import SlackClient
class SlackBot(object):
'''
SlackBot:
'''
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
'''
try:
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'))
except:
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