]> git.somenet.org - pub/jan/mattermost-bot.git/blob - modules/DialogManagedInfCoin.py
core/CoreCommandUse.py
[pub/jan/mattermost-bot.git] / modules / DialogManagedInfCoin.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 import json
7 import os
8 import requests
9
10
11 from AbstractCommand import *
12 class DialogManagedInfCoin(AbstractCommand):
13     TRIGGER = "inf-coin"
14     CONFIG = {"display_name": "somebot-command", "auto_complete": True,
15 #              "auto_complete_hint": "Manage your Inf-Coins",
16              }
17     USEINFO = CONFIG["auto_complete_desc"] = CONFIG["description"] = "Will make @bot send you a Inf-Coin managing post."
18
19
20     def __init__(self, team_id, datadir, infcoin_req_url, infcoin_req_user, infcoin_req_pw, infcoin_req_amount=100):
21         super().__init__(team_id)
22
23         self.datadir = datadir
24         self.infcoin_req_url = infcoin_req_url
25         self.infcoin_req_user = infcoin_req_user
26         self.infcoin_req_pw = infcoin_req_pw
27         self.infcoin_req_amount = infcoin_req_amount
28
29
30     def on_POST(self, request, data):
31 #        self._require_bot_admin(data) # will throw an exception if not. (Dont try-except: Its handled up the stack.)
32
33         msg = cleandoc("""
34             ``AUTODELETE-DAY``
35             ## ``(FS)Inf``:coin: ``Management`` :rocket:
36             **Inf-Coins, previously FSInf-Coins are tokens of gratitude and appreciation without any inherent value.**
37             For more details about Inf-Coin, [visit its professional website!](https://infcoin.jvales.net)
38
39             You will be rewarded 1 Inf-Coin for setting your wallet address for the first time. :rocket:
40
41             To send Inf-Coins to somebody, use the command ``/send-inf-coin @<ACCOUNTNAME>``
42
43             #### How to proceed
44             + Get yourself a NEO wallet which supports NEO2-Testnet. At least these should do:
45               + https://testnet.neotracker.io/wallet (Web)
46               + https://neoline.io/en/ (Extension)
47
48             + Once setup, start tracking the [$Inf (f26c60f640091e2fa8cd15b30bb5f51cd4d2048b) asset on NEO2-testnet](https://testnet.neotube.io/nep5/f26c60f640091e2fa8cd15b30bb5f51cd4d2048b/page/1) in your wallet.
49             + As soon as you get Inf-Coins your wallet should display them to you and you should be able to transfer them.
50
51             :information_source: *Coin minting cannot be done automatically is a secure manner so it might take a while for you to receive your Inf-Coins.*
52             :warning:**If you need to update your addess, because you gave a wrong one, contact @someone so your coins dont get send on the old address. Once sent, there is no undo.**
53             """)
54
55         att = [{
56             "text": "Self-Management",
57             "actions": [
58                 {"name": ":pencil: Set your Inf-Coin wallet address", "integration": {"url": self.URL+"/interactive"}},
59                 #{"name": ":rocket: Claim newly received Inf-Coins", "integration": {"url": self.URL+"/interactive"}}
60             ]
61             #},{
62             #"text": "Off-Chain",
63             #"actions": [{"name": ":package: Send unclaimed coins to somebody", "integration": {"url": self.URL+"/interactive"}}]
64             }]
65
66         c = self.bot.api.create_dm_channel_with(data['user_id'])
67         p = self.bot.api.create_post(c['id'], msg, props={"from_webhook":"true", "attachments":att})
68         request.cmd_respond_text_temp("@bot has written you a direct message.\n:arrow_right: https://mattermost.fsinf.at/"+data["team_domain"]+"/pl/"+p["id"])
69
70
71     def on_POST_interactive(self, request, data):
72         #import pprint
73         #print("on_POST_interactive")
74         #pprint.pprint(data)
75
76         callback_id = "wallet-"+data["user_id"]
77         infcoin_wallet = json.loads(self._wallet_load(self.datadir+callback_id, ".json", '{"wallet":""}'))
78
79         dialog = {
80             "callback_id": callback_id,
81             "title": "Set your Inf-Coin/NEO2-Testnet wallet address",
82             "submit_label":"Submit",
83             "elements":[{
84                 "display_name": "Your Inf-Coin/NEO2-Testnet wallet address",
85                 "placeholder": "Paste here.",
86                 "name": "infcoin_wallet",
87                 "type": "text",
88                 "help_text": "The wallet address is alphanumeric and 34 characters long.",
89                 "optional": True,
90                 "default": infcoin_wallet['wallet']
91             }]
92         }
93
94         self.bot.api.open_dialog(data["trigger_id"], self.URL+"/dialog", dialog)
95         request.respond(200, {})
96
97
98
99     def on_POST_dialog(self, request, data):
100         #import pprint
101         #print("on_POST_dialog")
102         #pprint.pprint(data)
103
104         # Validate address.
105         if data["submission"]["infcoin_wallet"] == "":
106             data["submission"]["infcoin_wallet"] = None
107         if data["submission"]["infcoin_wallet"] is not None and not (data["submission"]["infcoin_wallet"].isascii() and data["submission"]["infcoin_wallet"].isalnum() and len(data["submission"]["infcoin_wallet"]) == 34):
108             request.respond(200, {"errors": {"infcoin_wallet": "This doesnt seem like a valid address."}})
109         else:
110             c = self.bot.api.create_dm_channel_with(data['user_id'])
111             existed = self._wallet_store(self.datadir+data["callback_id"], ".json", json.dumps({"wallet":data["submission"]["infcoin_wallet"]}))
112             if not existed and data["submission"]["infcoin_wallet"] is not None:
113                 self.award_infcoins(data["user_id"], self.infcoin_req_amount, "wallet-set")
114                 p = self.bot.api.create_post(c['id'], "``AUTODELETE-DAY`` Wallet address set. Inf-Coins requested.", props={"from_webhook":"true"})
115             elif not existed:
116                 p = self.bot.api.create_post(c['id'], "``AUTODELETE-DAY`` Did nothing.", props={"from_webhook":"true"})
117             else:
118                 p = self.bot.api.create_post(c['id'], "``AUTODELETE-DAY`` Wallet address updated. No Inf-Coins requested.", props={"from_webhook":"true"})
119
120
121
122     def award_infcoins(self, user_id, amount, reason_part):
123         infcoin_wallet = json.loads(self._wallet_load(self.datadir+"wallet-"+user_id, ".json", '{"wallet":""}'))
124         if infcoin_wallet["wallet"] != "":
125             r = requests.get(self.infcoin_req_url, {"auth_name":self.infcoin_req_user, "auth_secret":self.infcoin_req_pw, "addr":infcoin_wallet["wallet"], "amount":amount, "reason_uniq":"mm-"+reason_part+", uid="+user_id})
126             return True
127         return False
128
129
130
131     def _wallet_load(self, path, filename, default=""):
132         path = path+filename
133         ret = default
134         if os.path.isfile(path):
135             with open(path, "r") as f:
136                 ret = f.read()
137         return ret
138
139
140     def _wallet_store(self, path, filename, data):
141         path = path+filename
142         data = str(data).strip()
143         existed = os.path.isfile(path)
144         if not (not existed and data == json.dumps({"wallet":None})):
145             with open(path, "w") as f:
146                 f.write(data)
147         return existed
148