]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/CommandSendInfCoinTo.py
[somebot] DialogManagedFSInfCoin + /send-inf-coin-to <username> <amount>
[pub/jan/mattermost-bot.git] / modules / CommandSendInfCoinTo.py
1 # Mattermost Bot module.
2 #  Copyright (c) 2016-2021 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
3 #  published under MIT-License
4
5 import json
6 import os
7 import requests
8
9
10 from AbstractCommand import AbstractCommand
11 class CommandSendInfCoinTo(AbstractCommand):
12     TRIGGER = "send-inf-coin-to"
13     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
14               "auto_complete_hint": "<username> <amount>",
15              }
16     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Helper to send Inf-Coins to somebody."
17
18
19     def __init__(self, team_id, datadir, infcoin_req_url):
20         super().__init__(team_id)
21
22         self.datadir = datadir
23         self.infcoin_req_url = infcoin_req_url
24
25
26     def on_POST(self, request, data):
27         msg_text = data['text'].strip().split(" ")
28
29         if len(msg_text) != 2:
30             request.cmd_respond_text_temp("``/send-inf-coin-to`` failed: The arguments are: <username> <amount>. :(")
31             return
32
33         user = self.bot.api.get_user_by_username(msg_text[0].replace("@","").strip(), exc=False)
34         infcoin_wallet = json.loads(self._wallet_load(self.datadir+"wallet-"+user["id"], ".json", '{"wallet":""}'))
35         if infcoin_wallet["wallet"] != "":
36             msg = "### [Complete the transaction with neoline via webbrowser]("+self.infcoin_req_url+"?addr="+infcoin_wallet["wallet"]+"&amount="+msg_text[1]+")"
37         else:
38             msg = "### ``/send-inf-coin-to`` failed: @"+msg_text[0].replace("@","").strip()+" hasnt told @bot their wallet address yet (by using ``/inf-coin``)"
39
40         request.cmd_respond_text_temp(msg)
41
42
43
44     def _wallet_load(self, path, filename, default=""):
45         path = path+filename
46         ret = default
47         if os.path.isfile(path):
48             with open(path, "r") as f:
49                 ret = f.read()
50         return ret