]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandDrinkBindAccount.py
core/MMBot.py
[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 from AbstractCommand import AbstractCommand
11 class CommandDrinkBindAccount(AbstractCommand):
12     TRIGGER = "drink-bind-account"
13     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
14               "auto_complete_hint": "<drink-terminal-username> <drink-terminal-password>",
15              }
16     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Bind your drink-terminal-account to your mattermost username."
17
18
19     def on_POST(self, request, data):
20         data['text'] = data['text'].strip()
21         mc_credentials = data['text'].split(" ", 2)
22
23         if data["text"] == "" or data["text"].lower() == "help" or len(mc_credentials) != 2:
24             request.cmd_respond_text_temp(cleandoc("""This command takes exactly 2 parameters: ``<drink-terminal-username> <drink-terminal-password``.
25                 Once you passed the correct drink-terminal credentials your Mattermost account will be bound to that account.
26                 The binding can be undone at the drink-terminal @ fsinf or via debug weg-front-end.
27                 Once sucessfully bound you will be able to use ``/drink <drink-terminal-input-string>`` to pay for your drink.
28                 """))
29
30         try:
31             handle = MCAPI.API("KEYBOARD", mc_credentials[0], mc_credentials[1])
32             handle.manage_token('add', 'MM_'+data["user_id"])
33         except MCAPI.APIException as e:
34             if e.message == 'CERR_AUTH_FAIL_ACCPWDRATE':
35                 request.cmd_respond_text_temp(":stop_sign: Failed to bind Mattermost account to drink-terminal account: Invalid drink-terminal credentials :(")
36             elif e.message == 'CERR_FAILED_TO_ADD_TOKEN_EXISTING':
37                 request.cmd_respond_text_temp(":warning: Failed to bind Mattermost account to drink-terminal account: Mattermost username is already bound to an account.")
38             else:
39                 request.cmd_respond_text_temp(":warning: Failed to bind Mattermost account to drink-terminal account: Unknown error: "+ e.message)
40             return
41
42         request.cmd_respond_text_temp(":white_check_mark: Sucessfully bound Mattermost account and drink-terminal account :)")
43         self.bot.debug_chan("``/drink-bind-account success: @"+data["user_name"]+'``')