Skip to content

Instantly share code, notes, and snippets.

@smd877
Created January 29, 2021 14:18
Show Gist options
  • Save smd877/9bb63475135a426a81ad8b3ecb0dd697 to your computer and use it in GitHub Desktop.
Save smd877/9bb63475135a426a81ad8b3ecb0dd697 to your computer and use it in GitHub Desktop.
Python3系でSlackにメッセージをポストするサンプル
import os
import urllib.request, urllib.parse
MESSAGE = 'sample message.'
POST_SLACK_TOKEN_ID = os.environ.get('POST_SLACK_TOKEN_ID')
POST_SLACK_CHANNEL_ID = os.environ.get('POST_SLACK_CHANNEL_ID')
URL = 'https://slack.com/api/chat.postMessage'
headers = {
'Authorization': 'Bearer ' + POST_SLACK_TOKEN_ID
}
message = {
'text' : MESSAGE,
'channel' : POST_SLACK_CHANNEL_ID
}
data = urllib.parse.urlencode(message).encode()
req = urllib.request.Request(URL,data=data,headers=headers,method='POST')
urllib.request.urlopen(req)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment