Ejemplo n.º 1
0
        private async Task Balance(SlackUser slackUser, SlackIMSession imSession)
        {
            var account = await walletRepository.GetAccount(slackUser.Id);

            if (account == null)
            {
                // This could be improved with a fancy "do you want to create new account" - button which exists in the Slack API.
                await SlackConnector.SendMessage(imSession.Id, MessageConstants.NoAccount);

                return;
            }
            string message  = "";
            var    balances = await nxtConnector.GetBalances(account.NxtAccountRs, transferables.NxtTransferables);

            foreach (var balance in balances)
            {
                if (balance.Value > 0 || balance.Key == Nxt.Singleton)
                {
                    if (transferables.ContainsTransferable(balance.Key))
                    {
                        message += MessageConstants.CurrentBalance(balance.Value, balance.Key, false) + "\n";
                    }
                    else
                    {
                        message += MessageConstants.CurrentBalance(balance.Value, balance.Key, true) + "\n";
                    }
                }
            }
            await SlackConnector.SendMessage(imSession.Id, message.TrimEnd());
        }
Ejemplo n.º 2
0
        private async Task UserSettings(SlackUser slackUser, SlackIMSession imSession)
        {
            var message            = $"{MessageConstants.UserSettingsHeader}";
            var reactionTipSetting = await walletRepository.GetUserReactionTipSetting(slackUser.Id) ? "on" : "off";

            message += $"*reactiontip {reactionTipSetting}* - valid values: _on_ and _off_\n"
                       + $"Description: {MessageConstants.UserSettingsReactionSettingDescription}\n\n";
            message += MessageConstants.UserSettingsFooter;
            await SlackConnector.SendMessage(imSession.Id, message);
        }
Ejemplo n.º 3
0
        private async Task List(SlackIMSession imSession)
        {
            var message = MessageConstants.ListCommandHeader;

            foreach (var transferable in transferables.NxtTransferables)
            {
                message += MessageConstants.ListCommandForTransferable(transferable);
            }
            await SlackConnector.SendMessage(imSession.Id, message.TrimEnd(), false);
        }
Ejemplo n.º 4
0
        private async Task UpdateUserSetting(SlackUser slackUser, SlackIMSession imSession, Match match)
        {
            var key   = match.Groups["key"].Value.ToString().ToLower();
            var value = match.Groups["value"].Value.ToString().ToLower();

            if (key.Equals("reactiontip"))
            {
                var reactionTipValue = value.Equals("on");
                await walletRepository.SetUserReactionTipSetting(slackUser.Id, reactionTipValue);

                await SlackConnector.SendMessage(imSession.Id, MessageConstants.UserReactionSettingUpdated(reactionTipValue));
            }
        }
Ejemplo n.º 5
0
        private async Task Deposit(SlackUser slackUser, SlackIMSession imSession)
        {
            var account = await walletRepository.GetAccount(slackUser.Id);

            if (account == null)
            {
                account = await CreateNxtAccount(slackUser.Id);

                await SlackConnector.SendMessage(imSession.Id, MessageConstants.AccountCreated(account.NxtAccountRs));
            }
            else
            {
                await SlackConnector.SendMessage(imSession.Id, MessageConstants.DepositAddress(account.NxtAccountRs));
            }
        }
Ejemplo n.º 6
0
        private void HandleIMSessionCreated(JObject jObject)
        {
            var imSession = new SlackIMSession
            {
                Id     = (string)jObject["channel"]["id"],
                UserId = (string)jObject["user"]
            };

            if (imSessions.All(im => im.Id != imSession.Id))
            {
                imSessions.Add(imSession);
                var slackUser = slackUsers.Single(u => u.Id == imSession.UserId);
                logger.LogTrace($"IM session with user {slackUser.Name} was created.");
            }
        }
Ejemplo n.º 7
0
        public async Task InstantMessageCommand(string message, SlackUser slackUser, SlackIMSession imSession)
        {
            var   messageText = message.Trim();
            Match match       = null;

            if (string.IsNullOrEmpty(messageText))
            {
                await UnknownCommand(imSession);
            }
            else if (IsSingleWordCommand(messageText, "help"))
            {
                await Help(imSession);
            }
            else if (IsSingleWordCommand(messageText, "balance"))
            {
                await Balance(slackUser, imSession);
            }
            else if (IsSingleWordCommand(messageText, "deposit"))
            {
                await Deposit(slackUser, imSession);
            }
            else if (IsSingleWordCommand(messageText, "list"))
            {
                await List(imSession);
            }
            else if (IsSingleWordCommand(messageText, "settings"))
            {
                await UserSettings(slackUser, imSession);
            }
            else if ((match = IsWithdrawCommand(messageText)).Success)
            {
                await Withdraw(slackUser, imSession, match);
            }
            else if ((match = IsUserSettingCommand(messageText)).Success)
            {
                await UpdateUserSetting(slackUser, imSession, match);
            }
            else
            {
                await UnknownCommand(imSession);
            }
        }
Ejemplo n.º 8
0
        private async Task Withdraw(SlackUser slackUser, SlackIMSession imSession, Match match)
        {
            var address          = match.Groups[1].Value;
            var amountToWithdraw = decimal.Parse(match.Groups[2].Value, CultureInfo.InvariantCulture);
            var unit             = string.IsNullOrEmpty(match.Groups[3].Value) ? Nxt.Singleton.Name : match.Groups[3].Value;
            var transferable     = transferables.GetTransferable(unit);
            var account          = await walletRepository.GetAccount(slackUser.Id);

            if (account == null)
            {
                await SlackConnector.SendMessage(imSession.Id, MessageConstants.NoAccount);

                return;
            }
            if (transferable == null && unit.IsNumeric())
            {
                var id = ulong.Parse(unit);
                try
                {
                    var asset = await nxtConnector.GetAsset(new TransferableConfig(id, "", "", new List <string>(), new List <TipReaction>()));

                    transferable = asset;
                }
                catch (Exception)
                {
                    try
                    {
                        var currency = await nxtConnector.GetCurrency(new TransferableConfig(id, "", "", new List <string>(), new List <TipReaction>()));

                        transferable = currency;
                    }
                    catch (Exception) { }
                }
            }
            if (!(await VerifyParameters(transferable, unit, account, imSession.Id, amountToWithdraw)))
            {
                return;
            }
            try
            {
                var txId = await nxtConnector.Transfer(account, address, transferable, amountToWithdraw, "withdraw from slack tipper");

                await SlackConnector.SendMessage(imSession.Id, MessageConstants.Withdraw(amountToWithdraw, transferable.Name, txId), false);
            }
            catch (ArgumentException e)
            {
                if (e.Message.Contains("not a valid reed solomon address"))
                {
                    await SlackConnector.SendMessage(imSession.Id, MessageConstants.InvalidAddress);
                }
                else
                {
                    logger.LogError(0, e, e.Message);
                    throw;
                }
            }
            catch (NxtException e)
            {
                logger.LogError(0, e, e.Message);
                throw;
            }
        }
Ejemplo n.º 9
0
 private async Task Help(SlackIMSession imSession)
 {
     await SlackConnector.SendMessage(imSession.Id, MessageConstants.GetHelpText(SlackConnector.SelfName));
 }
Ejemplo n.º 10
0
 private async Task UnknownCommand(SlackIMSession imSession)
 {
     await SlackConnector.SendMessage(imSession.Id, MessageConstants.UnknownCommandReply);
 }