You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.6 KiB

  1. import os
  2. from slackclient import SlackClient
  3. import argparse
  4. if __name__ == '__main__':
  5. parser = argparse.ArgumentParser(description="Slack notification script")
  6. parser.add_argument('--channel','-c', required=True, help="Slack channel.")
  7. parser.add_argument('--message','-m', required=True, help="The message to send.")
  8. parser.add_argument('--username', required=False, help="Username used on Slack")
  9. parser.add_argument('--api_token', required=False, help="Slack API token. Can be set in SLACK_API_TOKEN environment variable too.")
  10. parser.add_argument('--icon_emoji','-i', required=False, help="Icon for the message.")
  11. args = parser.parse_args()
  12. username = args.username
  13. api_token=args.api_token
  14. if api_token is None:
  15. api_token=os.getenv("SLACK_API_TOKEN")
  16. message = args.message
  17. channel = args.channel
  18. icon_emoji = args.icon_emoji
  19. slack_client = SlackClient(api_token)
  20. apitest = slack_client.api_call("api.test")
  21. if not apitest['ok']:
  22. raise ValueError("api.test error: {0}".format(apitest['error']))
  23. authtest = slack_client.api_call("auth.test")
  24. if not authtest['ok']:
  25. raise ValueError("auth.test error: {0}".format(authtest['error']))
  26. if username is None:
  27. username = authtest['user']
  28. if icon_emoji is None:
  29. result = slack_client.api_call("chat.postMessage", channel=channel, text=message, username=username, icon_emoji=icon_emoji)
  30. else:
  31. result = slack_client.api_call("chat.postMessage", channel=channel, text=message, username=username)
  32. if not result['ok']:
  33. raise ValueError("Message error: {0}".format(result['error']))