From fe1961dfc64e5b86da64e105fc0a892164c0b07d Mon Sep 17 00:00:00 2001 From: Faelix Incident Handler Date: Fri, 15 Jun 2018 21:02:48 +0000 Subject: [PATCH] creating channels --- fih.py | 30 ++++++++++++++++++++++-------- incident.py | 18 +----------------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/fih.py b/fih.py index f206541..293415d 100644 --- a/fih.py +++ b/fih.py @@ -6,12 +6,11 @@ import incident app = Flask( __name__ ) app.config.from_envvar( 'FIH_SETTINGS' ) -mm = incident.Mattermost( app.config[ 'MATTERMOST_TEAM_ID' ], - url = app.config[ 'MATTERMOST_URL' ], - port = app.config[ 'MATTERMOST_PORT' ], - login_id = app.config[ 'MATTERMOST_USER_EMAIL' ], - password = app.config[ 'MATTERMOST_USER_PASSWORD' ], - ) +app.mm = incident.Mattermost( url = app.config[ 'MATTERMOST_URL' ], + port = app.config[ 'MATTERMOST_PORT' ], + login_id = app.config[ 'MATTERMOST_USER_EMAIL' ], + password = app.config[ 'MATTERMOST_USER_PASSWORD' ], + ).mm @app.route( '/mattermost/incident', methods = [ 'POST' ] ) def mattermost_incident(): @@ -28,8 +27,23 @@ def mattermost_incident(): if request.form[ 'token' ] in app.config[ 'MATTERMOST_COMMAND_TOKENS' ]: fi_number = incident.generate_incident_number() channel_name = fi_number.lower() - mm.create_channel( channel_name, request.form[ 'text' ] ) - mm.notify( request.form[ 'channel_id' ], fi_number, channel_name ) + 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", + } ) + app.mm.posts.create_post( options = { 'channel_id': channel[ 'id' ], + '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, ), + } ) + return "" # "Starting incident %s in channel #%s." % ( fi_number, channel_name ) else: diff --git a/incident.py b/incident.py index 34291cf..b017f13 100644 --- a/incident.py +++ b/incident.py @@ -10,22 +10,6 @@ def generate_incident_number(): return "FI" + datetime.datetime.now().strftime( "%Y%m%d" ) + ( "%4d" % int( random.random() * 10000 ) ) class Mattermost( object ): - def __init__( self, team_id, *args, **kwargs ): - self.team_id = team_id + def __init__( self, *args, **kwargs ): self.mm = mattermostdriver.Driver( kwargs ) self.mm.login() - - def create_channel( self, name, purpose = None ): - r = self.mm.channels.create_channel( options = { "team_id": self.team_id, - "name": name, - "purpose": "Incident " + name + ( purpose and " " + purpose or "" ), - "header": "", - "type": "P", - } ) - print( repr( r ) ) - - - def notify( self, notify_channel_id, incident_number, incident_channel_name ): - self.mm.posts.create_post( options = { 'channel_id': notify_channel_id, - 'message': 'Starting incident %s with discussion in channel -%s' % ( incident_number, incident_channel_name, ), - } )