Пример #1
0
        public async Task <IActionResult> LoginCallback(int id, string first_name, string username, string photo_url, string auth_date, string hash)
        {
            Dictionary <string, string> fields = new Dictionary <string, string>()
            {
                { "id", id.ToString() },
                { "first_name", first_name },
                { "username", username },
                { "photo_url", photo_url },
                { "auth_date", auth_date },
                { "hash", hash }
            };

            var loginWidget = new LoginWidget(AppSettingsProvider.BotApiKey);

            try
            {
                if (loginWidget.CheckAuthorization(fields) == Authorization.Valid)
                {
                    var aspnetuser = await _appUsersManagerService.GetUserAsync(fields["username"], long.Parse(fields["id"]));

                    _appUsersManagerService.Update_Photourl(aspnetuser.TelegramId, fields["photo_url"]);

                    //sign the user and go to home
                    await _signInManager.SignInAsync(aspnetuser, isPersistent : false);
                }
            }
            catch (Exception ex)
            {
                var log = new Log();
                log.LogMessage(ex.Message + " " + ex.StackTrace + " " + ex.InnerException);
                _appdb.Logger.Add(log);
                _appdb.SaveChangesAsync().Wait();
            }

            return(RedirectToAction("Index", "Home"));
        }
Пример #2
0
        public async Task EchoAsync(Update update)
        {
            // Check if we have a message.
            if (update == null || update.Type != UpdateType.Message || update.Message.Text.Length == 0)
            {
                return;
            }

            var message        = update.Message;
            var flagMsgUpdate  = message.Chat.Id == AppSettingsProvider.TelegramChannelId;
            var maxusers       = 5;
            var botcommands    = new List <string>();
            var lstDestUsers   = new List <string>();
            var lstDestAddress = new List <string>();
            var lstAmount      = new List <double>();
            var lstAppUsers    = new List <ApplicationUser>();

            // Get the user who sent message
            var appuser = await _appUsersManagerService.GetUserAsync(message.From.Username, message.From.Id, flagMsgUpdate);

            /* Configure Photo Url
             * try
             * {
             *  var file_id = _botService.Client.GetUserProfilePhotosAsync(message.From.Id).Result.Photos[0][0].FileId;
             *  var file = _botService.Client.GetFileAsync(file_id);
             *  var filepath = file.Result.FilePath;
             *  var photo_url = "https://api.telegram.org/file/bot" + AppSettingsProvider.BotApiKey + "/" + filepath;
             *  _appUsersManagerService.Update_Photourl(appuser.TelegramId, photo_url);
             * }
             * catch (Exception ex)
             * {
             *  var log = new Log();
             *  log.LogMessage(ex.Message + " " + ex.StackTrace + " " + ex.InnerException);
             *  _appdb.Logger.Add(log);
             *  _appdb.SaveChangesAsync().Wait();
             * }*/

            try
            {
                // Match !Command if present
                if (Regex.Matches(message.Text, @"!(\S+)\s?").Count > 0)
                {
                    botcommands = Regex.Matches(message.Text.ToUpper(), @"!(\S+)\s?").Select(m => m.Value).ToList();
                }

                // Match @username if present
                if (Regex.Matches(message.Text, @"@(\S+)\s?").Count > 0)
                {
                    lstDestUsers = Regex.Matches(message.Text, @"@(\S+)\s?").Select(m => m.Value.Replace("@", string.Empty).Trim()).ToList();
                }

                // Match any double Amount if present
                if (Regex.Matches(message.Text, @"\b[0-9]+(\.[0-9]*)?([ ]|($|\s))").Count > 0)
                {
                    lstAmount = Regex.Matches(message.Text, @"\b[0-9]+(\.[0-9]*)?([ ]|($|\s))", RegexOptions.Multiline).Select(m => double.Parse(m.Value.Trim())).ToList();
                }

                // Match any Rise Address if present
                if (Regex.Matches(message.Text, @"\d+[R,r]").Count > 0)
                {
                    lstDestAddress = Regex.Matches(message.Text, @"\d+[R,r]").Select(m => m.Value.Trim()).ToList();
                }

                // add any extra users
                if (lstAmount.Count > 1 && lstAmount[1] < 5000 && Math.Abs(lstAmount[1]) > 0)
                {
                    maxusers = int.Parse(lstAmount[1].ToString(CultureInfo.InvariantCulture));
                }
            }
            catch (Exception ex)
            {
                var log = new Log();
                log.LogMessage(ex.Message + " " + ex.StackTrace + " " + ex.InnerException);
                _appdb.Logger.Add(log);
                _appdb.SaveChangesAsync().Wait();

                return;
            }

            foreach (var command in botcommands)
            {
                switch (command.Trim())
                {
                // Show Pool
                case "!POOL":
                    await cmd_ShowPool(appuser, lstDestAddress);

                    break;

                // Info command
                case "!INFO":
                    await cmd_Info(message);

                    break;

                // Show Balance
                case "!BALANCE":
                    await cmd_ShowUserBalance(appuser);

                    break;


                // Vote for Dtpool
                case "!VOTE":
                    await cmd_Vote(appuser);

                    break;


                // Show Help
                case "!HELP":
                    await cmd_Help(appuser);

                    break;

                case "!CLEARLOGS":
                    await cmd_ClearLogs(appuser);

                    break;

                // Withdraw RISE to address
                case "!WITHDRAW":
                {
                    double amount = Math.Round(lstAmount.FirstOrDefault() - 0.1, 2);

                    if (await cmd_preSend(amount, command, 1, message.Chat.Id, appuser))
                    {
                        await cmd_Withdraw(appuser, amount, lstDestAddress.FirstOrDefault());
                    }

                    break;
                }

                //// Boom!
                case "!BOOM":
                {
                    try
                    {
                        double amount = 0;

                        //fetch the second number for the new max number of people.
                        if (lstAmount.Count > 1 && Math.Abs(lstAmount[1]) > 0)
                        {
                            maxusers = int.Parse(lstAmount[1].ToString(CultureInfo.InvariantCulture));
                        }
                        else if (lstAmount.Count == 0)
                        {
                            await _botService.Client.SendTextMessageAsync(message.Chat.Id, Emoji.Joy + " You forgot to enter a RISE amount!", ParseMode.Html);

                            return;
                        }

                        amount = Math.Round(lstAmount.FirstOrDefault() - (0.1 * maxusers), 2);

                        lstAppUsers = _appUsersManagerService.GetBoomUsers(appuser.UserName, maxusers);

                        if (await cmd_preSend(amount, command, lstAppUsers.Count, message.Chat.Id, appuser))
                        {
                            var txtmsg = string.Empty;

                            if (amount >= 90)
                            {
                                txtmsg = Emoji.Boom + Emoji.Boom + Emoji.Boom + " KABOOM!!! " + Emoji.Boom + Emoji.Boom + Emoji.Boom + " its a wonderful day for active users!";
                            }
                            else if (amount >= 10)
                            {
                                txtmsg = Emoji.Boom + Emoji.Boom + " BOOM!!! " + Emoji.Boom + Emoji.Boom + " its a nice day for active users!";
                            }
                            else
                            {
                                txtmsg = Emoji.Boom + " Hola!! " + Emoji.Boom + " its a good day for active users! ";
                            }

                            await cmd_Send(message, appuser, amount, lstAppUsers, txtmsg);
                        }
                    }
                    catch (Exception ex)
                    {
                        var log = new Log();
                        log.LogMessage(ex.Message + " " + ex.StackTrace + " " + ex.InnerException);
                        _appdb.Logger.Add(log);
                        _appdb.SaveChangesAsync().Wait();
                    }
                }
                break;

                // Let it Rain Rise
                case "!RAIN":
                {
                    double amount = 0;

                    //fetch the second number for the new max number of people.
                    if (lstAmount.Count > 1 && Math.Abs(lstAmount[1]) > 0)
                    {
                        maxusers = int.Parse(lstAmount[1].ToString(CultureInfo.InvariantCulture));
                    }
                    else if (lstAmount.Count == 0)
                    {
                        await _botService.Client.SendTextMessageAsync(message.Chat.Id, Emoji.Joy + " You forgot to enter a RISE amount!", ParseMode.Html);

                        return;
                    }

                    amount = Math.Round(lstAmount.FirstOrDefault() - (0.1 * maxusers), 2);

                    lstAppUsers = _appUsersManagerService.GetRainUsers(appuser.UserName, maxusers);

                    // Check before sending
                    if (await cmd_preSend(amount, command, lstAppUsers.Count, message.Chat.Id, appuser))
                    {
                        var txtmsg = string.Empty;

                        if (amount >= 90)
                        {
                            txtmsg = Emoji.Star + Emoji.Star + Emoji.Star + " RAIN RAIN!!!! " + Emoji.Star + Emoji.Star + Emoji.Star + " its a wonderful rainy day for last day active users!";
                        }
                        else if (amount >= 10)
                        {
                            txtmsg = Emoji.Star + Emoji.Star + " RAIN!!! " + Emoji.Star + Emoji.Star + " its a nice day for last day active users.!";
                        }
                        else
                        {
                            txtmsg = Emoji.Star + " RAIN!!! " + Emoji.Star + " its a good day for last day active users!";
                        }

                        await cmd_Send(message, appuser, amount, lstAppUsers, txtmsg);
                    }
                }
                break;

                // send coin to address
                case "!SEND":
                {
                    if (lstAmount.Count == 0)
                    {
                        await _botService.Client.SendTextMessageAsync(message.Chat.Id, "You forgot to enter a rise amount!", ParseMode.Html);

                        return;
                    }

                    double amount = Math.Round(lstAmount.FirstOrDefault() - 0.1, 2);

                    foreach (var user in lstDestUsers)
                    {
                        var e = await _appUsersManagerService.GetUserAsync(user);

                        if (e != null)
                        {
                            lstAppUsers.Add(e);
                        }
                    }

                    if (lstAppUsers.Count >= 1)
                    {
                        // Check before sending
                        if (await cmd_preSend(amount, command, lstAppUsers.Count, message.Chat.Id, appuser))
                        {
                            var txtmsg = string.Empty;

                            if (amount >= 90)
                            {
                                txtmsg = Emoji.Money_With_Wings + " wake up!!! its a wonderful day! " + Emoji.Money_With_Wings;
                            }
                            else if (amount >= 10)
                            {
                                txtmsg = Emoji.Moneybag + " wake up!!! its a nice day! " + Emoji.Moneybag;
                            }
                            else
                            {
                                txtmsg = Emoji.Star + " wake up!! its an good day!" + Emoji.Star;
                            }

                            await cmd_Send(message, appuser, amount, lstAppUsers, txtmsg);
                        }
                    }
                    else if (lstAppUsers.Count == 0)
                    {
                        await _botService.Client.SendTextMessageAsync(message.Chat.Id, "Please tell me a user to send to!.", ParseMode.Html);
                    }
                    else
                    {
                        await _botService.Client.SendTextMessageAsync(message.Chat.Id, "Please provide a valid user. Dont forget the @username", ParseMode.Html);
                    }
                }
                break;

                // Tell when last message from user
                case "!SEEN":
                {
                    lstAppUsers = new List <ApplicationUser>();

                    foreach (var user in lstDestUsers)
                    {
                        var e = await _appUsersManagerService.GetUserAsync(user);

                        if (e != null)
                        {
                            lstAppUsers.Add(e);
                        }
                    }

                    await cmd_Seen(appuser, message, lstAppUsers);
                }
                break;

                // Info Price
                case "!PRICE":
                    await cmd_Price(message, appuser);

                    break;

                // Return a  geek joke
                case "!HOPE":
                    await cmd_Hope(message);

                    break;

                // Show Rise Exchanges
                case "!EXCHANGES":
                    await cmd_Exchanges(message, appuser);

                    break;

                // Show Rise Exchanges
                case "!EXCHANGE":
                    await cmd_Exchanges(message, appuser);

                    break;
                }
            }
        }