Created
February 28, 2017 02:05
-
-
Save heejune/9d38fb0a71f3a78dc88d469e7cdc0251 to your computer and use it in GitHub Desktop.
how to upload a file with SlackClient library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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