示例#1
0
        /// <inheritdoc />
        public override async Task HandleAsync(InlineQuery query)
        {
            _log.Information("Handling a new inline query: {query}", query.Query);

            if (string.IsNullOrWhiteSpace(query.Query))
            {
                return;
            }

            var me = await Bot.GetMeAsync();

            // log user query
            _storage.AddUserQuery(new UserQuery(query.From, query.Query));

            var filtered = _storage.Find(query.Query, null, 10)
                           .Where(p => p.RetailPrice > 0m)
                           .ToArray();

            if (filtered.Length == 0)
            {
                await Bot.AnswerInlineQueryAsync(query.Id, Enumerable.Empty <InlineQueryResultBase>());
            }
            else
            {
                var rate   = filtered.FirstOrDefault()?.CurrencyRate ?? 1.0m;
                var result = filtered
                             .Select((p, i) => p.ToInlineResult(i, query.Query, rate, me.Username));
                await Bot.AnswerInlineQueryAsync(query.Id, result);
            }
        }
        /// <inheritdoc />
        public override async Task HandleAsync(Message message)
        {
            _log.Information("Handling a new text message: {message}", message.Text);

            // log user query
            _storage.AddUserQuery(new UserQuery(message.From, message.Text));

            var splitted = message.Text.Split(' ', StringSplitOptions.RemoveEmptyEntries);

            string commandName;

            if (splitted.Length > 0)
            {
                var botNameStartIdx = splitted[0].IndexOf('@');
                commandName = botNameStartIdx > 0
                    ? splitted[0].Substring(0, botNameStartIdx)
                    : splitted[0];
            }
            else
            {
                commandName = string.Empty;
            }

            var commandLine = splitted.Length > 1 ? string.Join(' ', splitted.Skip(1)) : string.Empty;

            // getting user info
            var member = await Bot.GetChatMemberAsync(message.Chat.Id, message.From.Id);

            var args = new CommandEventArgs
            {
                Bot         = Bot,
                CommandLine = commandLine,
                ChatId      = message.Chat.Id,
                MessageId   = message.MessageId,
                IsBotOwner  = member.IsBotOwner(),
                IsAdmin     = member.IsAdmin()
            };

            _log.Information("Mapping a command '{command}'", commandName);

            if (_commandsDic.TryGetValue(commandName, out var command))
            {
                _log.Information("Command {command} found. Executing...", commandName);
                await command.ExecuteAsync(args);
            }
            else
            {
                // skip non-commands
                if (!commandName.StartsWith("/"))
                {
                    return;
                }

                _log.Information("Executing the default command...");
                await _defaultCmd.ExecuteAsync(args);
            }
        }