From b7fa001c8ff134631b982c3daa42c4a7db9c8516 Mon Sep 17 00:00:00 2001 From: Faelix Incident Handler Date: Fri, 15 Jun 2018 21:43:53 +0000 Subject: [PATCH] creating and referencing channels properly now --- fih.py | 29 +++++++++++++++++++---------- incident.py | 2 +- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/fih.py b/fih.py index 293415d..fe2184a 100644 --- a/fih.py +++ b/fih.py @@ -25,29 +25,38 @@ def mattermost_incident(): # token: XXXXXXXXXXXXXXXXXXXXXXXXXX if request.form[ 'token' ] in app.config[ 'MATTERMOST_COMMAND_TOKENS' ]: - fi_number = incident.generate_incident_number() - channel_name = fi_number.lower() + incident_number = incident.generate_incident_number() + fi_number = "FI#" + incident_number + channel_name = "incident" + incident_number purpose = request.form[ 'text' ] channel = app.mm.channels.create_channel( options = { "team_id": request.form[ 'team_id' ], "name": channel_name, - "purpose": "Incident " + fi_number + ( purpose and " " + purpose or "" ), - "header": "", ## Incident #" + fi_number, - "type": "P", + "display_name": fi_number, + "purpose": "Internal discussion about incident " + fi_number, + "header": purpose, + "type": "O", } ) + + # add user to the channel we just created + app.mm.client.make_request( 'post', '/channels/' + channel[ 'id' ] + '/members', options = { 'user_id': request.form[ 'user_id' ], + } ) app.mm.posts.create_post( options = { 'channel_id': channel[ 'id' ], - 'message': "Incident Channel for #%s created by @%s" % ( fi_number, request.form[ 'user_name' ] ), + 'message': "Incident Channel for %s created by @%s" % ( fi_number, request.form[ 'user_name' ] ), } ) - app.mm.posts.create_post( options = { 'channel_id': request.form[ 'channel_id' ], - 'message': 'Starting incident %s with discussion tagged as #%s' % ( fi_number, fi_number, ), + 'message': 'Starting incident %s with discussion in channel ~%s' % ( fi_number, channel_name, ), } ) - - return "" # "Starting incident %s in channel #%s." % ( fi_number, channel_name ) + return "" else: return "Invalid command token." + +@app.route( '/mattermost/fih', methods = [ 'POST' ] ) +def mattermost_fig(): + print( repr( request.form ) ) + return "Whaaat?" @app.route( '/' ) def hello_world(): diff --git a/incident.py b/incident.py index b017f13..577446b 100644 --- a/incident.py +++ b/incident.py @@ -7,7 +7,7 @@ import mattermostdriver random.seed( os.urandom( 8 ) ) def generate_incident_number(): - return "FI" + datetime.datetime.now().strftime( "%Y%m%d" ) + ( "%4d" % int( random.random() * 10000 ) ) + return datetime.datetime.now().strftime( "%Y%m%d" ) + ( "%4d" % int( random.random() * 10000 ) ) class Mattermost( object ): def __init__( self, *args, **kwargs ):