Esempio n. 1
0
        private async void BotOnCallbackQueryReceived(object sender, CallbackQueryEventArgs callbackQueryEventArgs)
        {
            long   userId   = callbackQueryEventArgs.CallbackQuery.From.Id;
            string userName = (callbackQueryEventArgs.CallbackQuery.From.FirstName + " " + callbackQueryEventArgs.CallbackQuery.From.LastName).Replace(" ", "\\ ").Replace("=", "\\=");

            InfluxDBLiteClient.Query($"bots,botname=wsizbusbot,user_id={userId},user_name={userName},actiontype=callback action=true");

            var callbackQuery = callbackQueryEventArgs.CallbackQuery;

            if (callbackQuery.Message.Chat.Type != ChatType.Private)
            {
                return;
            }

            // Handle stats, access, filters
            if (!CheckMessage(callbackQueryEventArgs: callbackQueryEventArgs))
            {
                return;
            }

            await Bot.AnswerCallbackQueryAsync(callbackQuery.Id);

            Invoker(callbackQueryEventArgs: callbackQueryEventArgs);

            // Debug inline commands
            // string methodName = ArgParser.ParseCallbackData(callbackQuery.Data).GetValueOrDefault(Commands.MethodName);
            // Console.WriteLine($"{methodName} - {callbackQuery.Data}");
        }
Esempio n. 2
0
        // Handlers
        private async void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs)
        {
            var message = messageEventArgs.Message;

            if (message.Chat.Type != ChatType.Private)
            {
                return;
            }

            await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.Typing);

            // Handle stats, access, filters
            if (!CheckMessage(messageEventArgs))
            {
                return;
            }

            if (message.Text != null && message.Text[0] == '/') // handle command
            {
                long   userId   = message.From.Id;
                string userName = (message.From.FirstName + " " + message.From.LastName).Replace(" ", "\\ ").Replace("=", "\\=");

                InfluxDBLiteClient.Query($"bots,botname=wsizbusbot,user_id={userId},user_name={userName},actiontype=command action=true");
                Invoker(messageEventArgs);
            }
            else
            {
                InfluxDBLiteClient.Query("bots,botname=wsizbusbot,actiontype=message action=true");
            }
            // HandleMessage(messageEventArgs); // handle simple message */
        }
Esempio n. 3
0
        /*private async void HandleMessage(MessageEventArgs messageEventArgs)
         * {
         *  // Get message data
         *  var chatId = messageEventArgs.Message.Chat.Id;
         *  var messageId = messageEventArgs.Message.MessageId;
         *  var sender = messageEventArgs.Message.From;
         *
         *  var user = ApplicationData.GetUser(sender.Id);
         *
         *  // Create controller
         *  ConversationController controller = new ConversationController() { ChatId = chatId, MessageId = messageId, User = user };
         *
         *  MethodInfo method = typeof(ConversationController).GetMethod("Start");
         *
         *  // Get and send chatAction from attributes
         *  var chatAction = BaseController.GetChatActionAttributes(method);
         *  if (chatAction.HasValue)
         *      await Bot.SendChatActionAsync(chatId, chatAction.Value);
         *
         *  controller.Start(messageEventArgs);
         * }*/

        private void BotOnReceiveError(object sender, ReceiveErrorEventArgs receiveErrorEventArgs)
        {
            InfluxDBLiteClient.Query("bots,botname=wsizbusbot,actiontype=error action=true");

            // ToDo logging to file
            Log.Error("Received error: {0} — {1}", receiveErrorEventArgs.ApiRequestException.ErrorCode, receiveErrorEventArgs.ApiRequestException.Message);

            // Send log to admin
            long?adminId = ApplicationData.Users.Set().FirstOrDefault(x => x.UserAccess == UserAccess.Admin)?.Id;

            if (adminId.HasValue)
            {
                SendMessageAsync(adminId.Value, $"Error {receiveErrorEventArgs.ApiRequestException.ErrorCode} : {receiveErrorEventArgs.ApiRequestException.Message}");
            }
        }
Esempio n. 4
0
        private async void BotOnInlineQueryReceived(object sender, InlineQueryEventArgs inlineQueryEventArgs)
        {
            var userfrom = inlineQueryEventArgs.InlineQuery.From;
            var user     = ApplicationData.GetUser(userfrom.Id);

            long   userId   = inlineQueryEventArgs.InlineQuery.From.Id;
            string userName = (inlineQueryEventArgs.InlineQuery.From.FirstName + " " + inlineQueryEventArgs.InlineQuery.From.LastName).Replace(" ", "\\ ").Replace("=", "\\=");

            InfluxDBLiteClient.Query($"bots,botname=wsizbusbot,user_id={userId},user_name={userName},actiontype=inline action=true");

            var handler = new InlineQueryController();

            handler.Bot  = Bot;
            handler.User = user;
            handler.InlineQueryEventArgs = inlineQueryEventArgs;

            await handler.Execute();
        }
Esempio n. 5
0
        private async void Invoker(MessageEventArgs messageEventArgs = null, CallbackQueryEventArgs callbackQueryEventArgs = null)
        {
            // Get message data
            var chatId    = messageEventArgs != null ? messageEventArgs.Message.Chat.Id : callbackQueryEventArgs.CallbackQuery.Message.Chat.Id;
            var messageId = messageEventArgs != null ? messageEventArgs.Message.MessageId : callbackQueryEventArgs.CallbackQuery.Message.MessageId;
            var sender    = messageEventArgs != null ? messageEventArgs.Message.From : callbackQueryEventArgs.CallbackQuery.From;

            var user = ApplicationData.GetUser(sender.Id);

            // Get controller type
            Type controllerType = messageEventArgs != null ? typeof(CommandController) : typeof(CallbackController);

            // Get method name from message
            string methodName = "";

            if (messageEventArgs != null)
            {
                methodName = (string)ArgParser.ParseCommand(messageEventArgs.Message.Text).GetValueOrDefault(Commands.MethodName);
            }
            else
            {
                methodName = (string)ArgParser.ParseCallbackData(callbackQueryEventArgs.CallbackQuery.Data).GetValueOrDefault(Commands.MethodName);
            }

            // Find method in controller
            MethodInfo method = controllerType.GetMethod(methodName);

            if (method != null)
            {
                // Check user access by role
                if (!BaseController.ValidateAccess(method, user))
                {
                    return;
                }
                try
                {
                    // Get and send chatAction from attributes
                    var chatAction = BaseController.GetChatActionAttributes(method);
                    if (chatAction.HasValue)
                    {
                        await Bot.SendChatActionAsync(chatId, chatAction.Value);
                    }

                    // Cast controller object
                    var controller = Activator.CreateInstance(controllerType);

                    // Set params
                    ((BaseController)controller).Bot       = Bot;
                    ((BaseController)controller).ChatId    = chatId;
                    ((BaseController)controller).MessageId = messageId;
                    ((BaseController)controller).User      = user;

                    // Invoke method
                    if (messageEventArgs != null)
                    {
                        method.Invoke(controller, parameters: new object[] { messageEventArgs });
                    }
                    else
                    {
                        method.Invoke(controller, parameters: new object[] { callbackQueryEventArgs });
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex, "Invoker error");

                    // Send log to admin
                    long?adminId = ApplicationData.Users.Set().FirstOrDefault(x => x.UserAccess == UserAccess.Admin)?.Id;

                    if (adminId.HasValue)
                    {
                        SendMessageAsync(adminId.Value, ex.ToString());
                        SendMessageAsync(adminId.Value, callbackQueryEventArgs?.CallbackQuery.Data ?? "F");
                    }
                }
            }
            else
            {
                // Cant method did not exists
                InfluxDBLiteClient.Query("bots,botname=wsizbusbot,actiontype=backenderror action=true");

                // Send log to admin
                long?adminId = ApplicationData.Users.Set().FirstOrDefault(x => x.UserAccess == UserAccess.Admin)?.Id;

                if (adminId.HasValue)
                {
                    SendMessageAsync(adminId.Value, callbackQueryEventArgs != null ? $"Cant find method for: {callbackQueryEventArgs.CallbackQuery.Data}" : $"Cant find method for: {messageEventArgs.Message.Text}");
                }

                SendMessageAsync(chatId, Local.ErrorMessage[user.GetLanguage], ParseMode.Markdown, replyMarkup: TemplateModelsBuilder.BuildStartMenuMarkup());

                // todo
                // delete callback keyboard under the message?
            }
        }