Skip to content

Instantly share code, notes, and snippets.

@vvalorous
Created March 31, 2018 01:45
Show Gist options
  • Save vvalorous/1ce2eda883649ba2b99ff804e1e1c791 to your computer and use it in GitHub Desktop.
Save vvalorous/1ce2eda883649ba2b99ff804e1e1c791 to your computer and use it in GitHub Desktop.
slack in python - message fields example
#!/usr/bin/env python
import slackweb
import argparse
def notify():
parser = argparse.ArgumentParser(description='slack notification script')
parser.add_argument('--webhook_url', help='incoming webhook url', required=True)
parser.add_argument('--verdict', help='pass or fail', default='pass')
parser.add_argument('--commit', help='commit of the build')
parser.add_argument('--agent'), help='name of xvci agent')
parser.add_argument('--device', help='device version')
parser.add_argument('--version', help='build version')
parser.add_argument('--dropbox_link', help='dropbox link')
parser.add_argument('--summary', help='summary')
parser.add_argument('--timestamp', help='timestamp')
args = parser.parse_args()
attachment_fields = []
footer_text = 'Tests started at: n/a'
if args.device is not None:
attachment_fields.append({"title": "device", "value": args.device, "short": True})
if args.agent is not None:
attachment_fields.append({"title": "xvci agent", "value": args.agent, "short": True})
if args.commit is not None:
attachment_fields.append({"title": "commit", "value": args.commit, "short": True})
if args.version is not None:
attachment_fields.append({"title": "version", "value": args.version, "short": True})
if args.dropbox_link is not None:
attachment_fields.append({"title": "dropbox link", "value": args.dropbox_link, "short": True})
if args.summary is not None:
attachment_fields.append({"title": "summary", "value": args.summary, "short": False})
if args.timestamp is not None:
footer_text = "triggered at: " + args.timestamp
# constructing attachments
attachments = []
attachment = {
"footer": footer_text,
"color": "36a64f" if args.verdict == "pass" else "#FF0000",
"fields": attachment_fields,
}
slack = slackweb.Slack(url=args.webhook_url);
attachments.append(attachment)
slack.notify(attachments=attachments)
if __name__ == '__main__':
notify()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment