Skip to content

Instantly share code, notes, and snippets.

@reachlin
Last active August 4, 2022 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reachlin/84a16c679764a500ef326a10c153fd81 to your computer and use it in GitHub Desktop.
Save reachlin/84a16c679764a500ef326a10c153fd81 to your computer and use it in GitHub Desktop.
python verify slack bot message
def verify_slack_msg(msg):
try:
slack_signature = msg['headers']['X-Slack-Signature']
slack_signing_secret = all_secrets.get('SLACK_SIGNING_SECRET', '')
sig_basestring = f"v0:{msg['headers']['X-Slack-Request-Timestamp']}:{msg['body']}"
my_signature = 'v0=' + hmac.new(
bytes(slack_signing_secret, 'latin-1'),
msg=bytes(sig_basestring, 'latin-1'),
digestmod=hashlib.sha256
).hexdigest()
print(f"verify_slack_msg: {my_signature} - {slack_signature}")
return my_signature.lower() == slack_signature.lower()
except Exception as e:
print(e)
return False
@reachlin
Copy link
Author

reachlin commented Aug 4, 2022

have to verify every message from slack, except the init. challenge msg. and the slack doc. sample code is out of date, https://api.slack.com/authentication/verifying-requests-from-slack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment