From f7a9eb37ba947c77c0a650499b20463f52fc62ba Mon Sep 17 00:00:00 2001 From: Someone Date: Fri, 19 Jun 2020 01:32:13 +0200 Subject: [PATCH] [somebot] /mate* --- somebot/modules/CommandMate.py | 92 +++++++++++++++++++++++ somebot/modules/CommandMateBindAccount.py | 39 ++++++++++ 2 files changed, 131 insertions(+) create mode 100644 somebot/modules/CommandMate.py create mode 100644 somebot/modules/CommandMateBindAccount.py diff --git a/somebot/modules/CommandMate.py b/somebot/modules/CommandMate.py new file mode 100644 index 0000000..7f89f92 --- /dev/null +++ b/somebot/modules/CommandMate.py @@ -0,0 +1,92 @@ +# Mattermost Bot. +# Copyright (c) 2016-2020 by Someone (aka. Jan Vales ) +# published under MIT-License + +import MCAPI + + +from AbstractCommand import * +class CommandMate(AbstractCommand): + TRIGGER = "mate" + CONFIG = {"display_name": "somebot-command", "auto_complete": True, + "auto_complete_hint": "['help'|<[count]shortcut>]", + } + USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Buy a drink." + + + def __init__(self, team_id, mate_terminal_user, mate_terminal_pass): + super().__init__(team_id) + + self.mate_terminal_user = mate_terminal_user + self.mate_terminal_pass = mate_terminal_pass + + + def on_POST(self, request, data): + try: + apirequest = dict() + apirequest["act"] = "create_token_secret" + handle = MCAPI.API("KEYBOARD", self.mate_terminal_user, self.mate_terminal_pass) + token_secret = handle.api_request(apirequest) + handle = MCAPI.API("MM", data["user_id"]) + MCAPI.API.token_secret = token_secret["token_secret"] + account_data = handle.get_account_info() + msg = ":arrow_right: Mate-terminal account:``"+str(account_data["account_info"]["accname"])+"``, available credits:``"+str(account_data["account_info"]["balance"])+"``" + except MCAPI.APIException as e: + if "CERR_AUTH_FAIL_ACCTOK" == e.message: + request.cmd_respond_text_temp(":stop_sign: Mattermost account seems to not be bound to any mate-terminal account. :(\n"+ + "Use ``/mate-bind-account `` to bind this Mattermost account to your mate-terminal account.") + + try: + cmd = data["text"] = data["text"].strip() + if data["text"] == "" or data["text"].lower() == "help": + msg += "\nUse ``/mate [count]`` and one of the following shortcuts to pay for ``$count * $shortcut``.\n\n|shortcut|price|Description|\n|---|---|---|" + + for s in account_data["shortcuts"]: + if int(s["vislevel"]) >= 2: + msg += "\n|"+s["short"]+"|"+s["amount"]+"|"+s["description"]+"|" + request.cmd_respond_text_temp(msg) + return + + count = None + shortcut = None + + import re + mat = re.match(r"(\d*)\s*(\w+)", cmd) + if mat is None: + request.cmd_respond_text_temp(msg+"\n:stop_sign: Invalid input: Nothing useful matched.") + return + + (count, sc) = mat.groups() + + if count == "": + count = 1 + + count = int(count) + if count < 1: + request.cmd_respond_text_temp(msg+"\n:stop_sign: Invalid input: count < 1") + return + + for s in account_data["shortcuts"]: + if s["short"] == sc: + shortcut = s + break + + if shortcut == None: + request.cmd_respond_text_temp(msg+"\n:stop_sign: Invalid input: no such shortcut :(\nUse ``/mate-shortcuts`` for a list of shortcuts.") + return + else: + result = handle.do_transaction(shortcut["target"], str(int(shortcut["amount"])*count), shortcut["reason"]) + account_data = handle.get_account_info(True) + msg = (":arrow_right: Mate-terminal account:``"+str(account_data["account_info"]["accname"])+"``, available credits:``"+str(account_data["account_info"]["balance"])+"``"+ + "\n:white_check_mark: Sucessfully paid **``"+str(int(shortcut["amount"])*count)+"``** TO **``"+str(shortcut["target"])+"``** FOR **``"+str(shortcut["reason"])+"``** :beers:") + self.bot.debug_chan("``/mate success: @"+data["user_name"]+' --'+str(int(shortcut["amount"])*count)+'--> '+str(shortcut["target"])+'``') + + except MCAPI.APIException as e: + if "CERR_AUTH_FAIL_ACCTOK" == e.message: + msg += "\n:stop_sign: Mattermost account seems to not be bound to any mate-terminal account. :(" + + elif "CERR_FAILED_TO_SEND" == e.message: + msg += "\n:stop_sign: Failed to send mate-credits. Balance exceeded? Sending too much? :(\nTried to send **``"+str(int(shortcut["amount"])*count)+"``** TO **``"+str(shortcut["target"])+"``** FOR **``"+str(shortcut["reason"])+"``**" + + request.cmd_respond_text_temp(msg) + diff --git a/somebot/modules/CommandMateBindAccount.py b/somebot/modules/CommandMateBindAccount.py new file mode 100644 index 0000000..d0f4c88 --- /dev/null +++ b/somebot/modules/CommandMateBindAccount.py @@ -0,0 +1,39 @@ +# Mattermost Bot. +# Copyright (c) 2016-2020 by Someone (aka. Jan Vales ) +# published under MIT-License + +import MCAPI + + +from AbstractCommand import * +class CommandMateBindAccount(AbstractCommand): + TRIGGER = "mate-bind-account" + CONFIG = {"display_name": "somebot-command", "auto_complete": True, + "auto_complete_hint": " ", + } + USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Bind your mate-terminal-account to your mattermost username." + + + def on_POST(self, request, data): + data['text'] = data['text'].strip() + mc_credentials = data['text'].split(" ") + + if data["text"] == "" or data["text"].lower() == "help" or len(mc_credentials) != 2: + request.cmd_respond_text_temp("This command takes exactly 2 parameters: `` `` to pay for your drink.") + + try: + handle = MCAPI.API("KEYBOARD", mc_credentials[0], mc_credentials[1]) + result = handle.manage_token('add', 'MM_'+data["user_id"]) + except MCAPI.APIException as e: + if 'CERR_AUTH_FAIL_ACCPWDRATE' == e.message: + request.cmd_respond_text_temp(":stop_sign: Failed to bind Mattermost account to mate-terminal account: Invalid mate-terminal credentials :(") + return + elif 'CERR_FAILED_TO_ADD_TOKEN_EXISTING' == e.message: + request.cmd_respond_text_temp(":warning: Failed to bind Mattermost account to mate-terminal account: Mattermost username is already bound to an account.") + return + + request.cmd_respond_text_temp(":white_check_mark: Sucessfully bound Mattermost account and mate-terminal account :)") + self.bot.debug_chan("``/mate-bind-account success: @"+data["user_name"]+'``') -- 2.43.0