]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandDrinkBindAccount.py
requirements.txt
[pub/jan/mattermost-bot.git] / modules / CommandDrinkBindAccount.py
1 # Mattermost Bot module.
2 #  Copyright (c) 2016-2022 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 #  published under MIT-License
4
5 from inspect import cleandoc
6
7 import MCAPI
8
9
10 # pylint: disable=wrong-import-position
11 from AbstractCommand import AbstractCommand
12 class CommandDrinkBindAccount(AbstractCommand):
13     TRIGGER = "drink-bind-account"
14     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
15               "auto_complete_hint": "<drink-terminal-username> <drink-terminal-password>",
16              }
17     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = AbstractCommand.ICON_PRIVATE+"Bind your drink-terminal-account to your mattermost username."
18
19
20     def on_POST(self, request, data):
21         data['text'] = data['text'].strip()
22         mc_credentials = data['text'].split(" ", 2)
23
24         if data["text"] == "" or data["text"].lower() == "help" or len(mc_credentials) != 2:
25             request.respond_cmd_err(cleandoc("""This command takes exactly 2 parameters: ``<drink-terminal-username> <drink-terminal-password``.
26                 Once you passed the correct drink-terminal credentials your Mattermost account will be bound to that account.
27                 The binding can be undone at the drink-terminal @ fsinf or via debug weg-front-end.
28                 Once sucessfully bound you will be able to use ``/drink <drink-terminal-input-string>`` to pay for your drink.
29                 """))
30
31         try:
32             handle = MCAPI.API("KEYBOARD", mc_credentials[0], mc_credentials[1])
33             handle.manage_token('add', 'MM_'+data["user_id"])
34         except MCAPI.APIException as exc:
35             if exc.message == 'CERR_AUTH_FAIL_ACCPWDRATE':
36                 request.respond_cmd_err("Failed to bind Mattermost account to drink-terminal account: Invalid drink-terminal credentials.")
37             elif exc.message == 'CERR_FAILED_TO_ADD_TOKEN_EXISTING':
38                 request.respond_cmd_err("Failed to bind Mattermost account to drink-terminal account: Mattermost username is already bound to an account.")
39             else:
40                 request.respond_cmd_err("Failed to bind Mattermost account to drink-terminal account: Unknown error: "+repr(exc))
41             return
42
43         self.bot.debug_chan("``/drink-bind-account success: @"+data["user_name"]+'``')
44         request.respond_cmd_temp("## :white_check_mark: Success! :)\n####Sucessfully bound Mattermost account and drink-terminal account :)")