Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Usage
- ## sp = SlackPost(token=app_token, channel='#random')
- ## await sp.post_update("Look at this cool graph", ['img1.png', 'img2.png'])
- ## await sp.post_update("The data has changed!", ['img3.png', 'img4.png'])
- class SlackPost():
- def __init__(self, token, channel):
- self.channel = channel
- self.client = client = slack.WebClient(
- token=token,
- run_async=True
- )
- self.ts = None
- async def post_update(self, message, fnames = []):
- attachments=[
- {
- 'fallback': 'graph',
- 'image_url': await self.upload_image(f)
- } for f in fnames
- ]
- await self.send_message(
- channel=self.channel,
- text=message,
- attachments=attachments
- )
- async def send_message(self, **kwargs):
- if self.ts:
- await (self.client.chat_update(ts=self.ts, **kwargs))
- else:
- response = await (self.client.chat_postMessage(**kwargs))
- self.ts = response.data['ts']
- self.channel = response.data['channel']
- async def upload_image(self, fname):
- img_response = await self.client.files_upload(
- file=fname,
- title="image"
- )
- file_response = await self.client.files_sharedPublicURL(
- file=img_response.data['file']['id'],
- )
- return file_response.data['file']['permalink_public']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement