public override async Task HandleAsync(IUpdateContext context, UpdateDelegate next, string[] args,
                                               CancellationToken cancellationToken)
        {
            _chatProcessor = new ChatProcessor(context);
            var msg = context.Update.Message;

            _settingsService = new SettingsService(msg);


            ConsoleHelper.WriteLine($"Args: {string.Join(" ", args)}");
            var sendText = "Perintah /welcome hanya untuk grup saja";

            if (msg.Chat.Type != ChatType.Private)
            {
                var chatTitle = msg.Chat.Title;
                var settings  = await _settingsService.GetSettingByGroup();

                var welcomeMessage     = settings.WelcomeMessage;
                var welcomeButton      = settings.WelcomeButton;
                var welcomeMedia       = settings.WelcomeMedia;
                var welcomeMediaType   = settings.WelcomeMediaType;
                var splitWelcomeButton = welcomeButton.Split(',').ToList <string>();

                var keyboard = welcomeButton.ToReplyMarkup(2);
                sendText = $"👥 <b>{chatTitle}</b>\n";
                if (welcomeMessage == "")
                {
                    sendText += "Tidak ada konfigurasi pesan welcome, pesan default akan di terapkan";
                }
                else
                {
                    sendText += welcomeMessage;
                }

//                if (args[0] == "anu")
//                {
//                    sendText += " anu";
//                }

//                sendText += " " + string.Join(", ",args);
                if (welcomeMediaType != "")
                {
                    await _chatProcessor.SendMediaAsync(welcomeMedia, welcomeMediaType, welcomeMessage, keyboard);
                }
                else
                {
                    await _chatProcessor.SendAsync(sendText, keyboard);
                }
            }
            else
            {
                await _chatProcessor.SendAsync(sendText);
            }
        }
Example #2
0
        public override async Task HandleAsync(IUpdateContext context, UpdateDelegate next, string[] args,
                                               CancellationToken cancellationToken)
        {
            _chatProcessor = new ChatProcessor(context);
            var     msg    = context.Update.Message;
            Message repMsg = null;
            var     data   = msg.Text.GetTextWithoutCmd();

            if (msg.ReplyToMessage != null)
            {
                repMsg = msg.ReplyToMessage;
                data   = repMsg.Text ?? repMsg.Caption;
            }

            if (data == "")
            {
                var sendText = "<b>Generate QR from text or caption media</b>" +
                               "\n<b>Usage : </b><code>/qr</code> (In-Reply)" +
                               "\n                <code>/qr your text here</code> (In-Message)";
                await _chatProcessor.SendAsync(sendText);

                return;
            }

            InlineKeyboardMarkup keyboard = null;

            if (repMsg != null)
            {
                keyboard = new InlineKeyboardMarkup(
                    InlineKeyboardButton.WithUrl("Sumber", repMsg.GetMessageLink())
                    );
            }

            var urlQr    = data.GenerateUrlQrApi();
            var fileName = $"{msg.Chat.Id}_{msg.MessageId}.jpg";

            // urlQr.SaveUrlTo(fileName);
            await _chatProcessor.SendMediaAsync(urlQr, "photo", replyMarkup : keyboard);
        }
        public async Task HandleAsync(IUpdateContext context, UpdateDelegate next, CancellationToken cancellationToken)
        {
            Message msg = context.Update.Message;

            _chatProcessor          = new ChatProcessor(context);
            _settingsService        = new SettingsService(msg);
            _elasticSecurityService = new ElasticSecurityService(context.Update.Message);

            ConsoleHelper.WriteLine("New Chat Members...");

            var newMembers  = msg.NewChatMembers;
            var isBootAdded = await newMembers.IsBotAdded();

            if (isBootAdded)
            {
                var botName  = Bot.GlobalConfiguration["Engines:ProductName"];
                var sendText = $"Hai, perkenalkan saya {botName}" +
                               $"\nFYI saya di bangun ulang menggunakan .NET Core, tepatnya ASP .NET Core." +
                               $"\n\nAku adalah bot pendebug dan grup manajemen yang di lengkapi dengan alat keamanan. " +
                               $"Agar saya berfungsi penuh, jadikan saya admin dengan level standard. " +
                               $"\n\nAku akan menerapkan konfigurasi standard jika aku baru pertama kali masuk kesini. " +
                               $"\n\nUntuk melihat daftar perintah bisa ketikkan /help";

                await _chatProcessor.SendAsync(sendText);

                await _settingsService.SaveSettingsAsync(new Dictionary <string, object>()
                {
                    { "chat_id", msg.Chat.Id },
                    { "chat_title", msg.Chat.Title }
                });

                if (newMembers.Length == 1)
                {
                    return;
                }
            }

            var parsedNewMember = await ParseMemberCategory(newMembers);

            var allNewMember  = parsedNewMember.AllNewMember;
            var allNoUsername = parsedNewMember.AllNoUsername;
            var allNewBot     = parsedNewMember.AllNewBot;

            if (allNewMember.Length > 0)
            {
                var chatSettings = await _settingsService.GetSettingByGroup();

                var chatTitle   = msg.Chat.Title;
                var memberCount = await _chatProcessor.GetMemberCount();

                var newMemberCount = newMembers.Length;

                ConsoleHelper.WriteLine("Preparing send Welcome..");

                if (chatSettings.WelcomeMessage == "")
                {
                    chatSettings.WelcomeMessage = $"Hai {allNewMember}" +
                                                  $"\nSelamat datang di kontrakan {chatTitle}" +
                                                  $"\nKamu adalah anggota ke-{memberCount}";
                }

                var sendText = chatSettings.WelcomeMessage.ResolveVariable(new
                {
                    allNewMember,
                    allNoUsername,
                    allNewBot,
                    newMemberCount,
                    chatTitle,
                    memberCount
                });

                IReplyMarkup keyboard = null;
                if (chatSettings.WelcomeButton != "")
                {
                    keyboard = chatSettings.WelcomeButton.ToReplyMarkup(2);
                }


                if (chatSettings.WelcomeMediaType != "")
                {
                    await _chatProcessor.SendMediaAsync(
                        chatSettings.WelcomeMedia,
                        chatSettings.WelcomeMediaType,
                        sendText,
                        keyboard);
                }
                else
                {
                    await _chatProcessor.SendAsync(sendText, keyboard);
                }

                await _settingsService.SaveSettingsAsync(new Dictionary <string, object>()
                {
                    { "chat_id", msg.Chat.Id },
                    { "chat_title", msg.Chat.Title },
                    { "members_count", memberCount }
                });
            }
            else
            {
                ConsoleHelper.WriteLine("Welcome Message ignored because User is Global Banned.");
            }
        }