コード例 #1
0
 public TelegramListener(ILifetimeScope scope, ITelegramClientService telegramFactory)
 {
     _scope           = scope;
     _telegramFactory = telegramFactory;
     _cache           = _telegramFactory.GetCache();
     _bot             = _telegramFactory.GetTelegramBot();
     _allowUser       = new List <string> {
         "Ars4m", "eliakbari"
     };
 }
コード例 #2
0
ファイル: CommandBase.cs プロジェクト: AmarPotki/TelegramBot
        public async Task <CommandResult> Execute(TDto dto)
        {
            var commandResult = new CommandResult();
            var bot           = _telegramFactory.GetTelegramBot();
            var cache         = _telegramFactory.GetCache();
            await bot.SendChatActionAsync(dto.Message.Chat.Id, ChatAction.Typing);

            if (dto is SaveCommandBaseDto)
            {
                var userMessageData = (UserMessageDataDto)cache.Get(dto.Message.Chat.Id.ToString());
                if (userMessageData == null)
                {
                    userMessageData = new UserMessageDataDto
                    {
                        ChatId      = dto.Message.Chat.Id,
                        LastCommand = typeof(TDto).Name,
                        UserName    = dto.Message.Chat.Username
                    };
                    var policy = new CacheItemPolicy {
                        SlidingExpiration = TimeSpan.FromMinutes(5)
                    };
                    cache.Add(dto.Message.Chat.Id.ToString(), userMessageData, policy);
                }
                else
                {
                    userMessageData.LastCommand = typeof(TDto).Name;
                    var policy = new CacheItemPolicy {
                        SlidingExpiration = TimeSpan.FromMinutes(5)
                    };
                    cache.Add(dto.Message.Chat.Id.ToString(), userMessageData, policy);
                }
                var user = await _telegramUserRepo.QueryAsync(async f => await f.FirstOrDefaultAsync(x => x.UserId == dto.Message.Chat.Id));

                user.LastCommand = typeof(TDto).Name;
                await _telegramUserRepo.SaveAsync(user);
            }
            try
            {
                commandResult = await InternalExecute(dto);
            }
            catch (Exception exp) {
                _logger.Error(new FaultDto("TelegramCommandBase", exp.GetMessage(), exp.StackTrace, FaultSource.Endpoint));
                commandResult.AddError(exp.GetMessage());
            }
            return(commandResult);
        }
コード例 #3
0
        protected override async Task <CommandResult> InternalExecute(CheckUserDtc tdDto)
        {
            var user = await _telegramUserRepository.QueryAsync(async f => await f.FirstOrDefaultAsync(x => x.UserId == tdDto.Message.Chat.Id));

            if (user != null)
            {
                var cache = _telegramFactory.GetCache();

                var userMessageData = new UserMessageDataDto
                {
                    ChatId      = tdDto.Message.Chat.Id,
                    LastCommand = user.LastCommand,
                    UserName    = tdDto.Message.Chat.Username
                };
                var policy = new CacheItemPolicy {
                    SlidingExpiration = TimeSpan.FromMinutes(5)
                };
                cache.Add(tdDto.Message.Chat.Id.ToString(), userMessageData, policy);
            }
            else
            {
                var bot   = _telegramFactory.GetTelegramBot();
                var usage = @"به دکتر سلام خوش امدید";
                await bot.SendTextMessageAsync(tdDto.Message.Chat.Id, usage,
                                               replyMarkup : new ReplyKeyboardHide());

                var keyboard = new ReplyKeyboardMarkup(new[]
                {
                    new[] // first row
                    {
                        new KeyboardButton("\U0001F4A1 درباره ما")
                    },
                    new[] // last row
                    {
                        new KeyboardButton("شناخت طبع مواد خوراکی"),
                        new KeyboardButton("درمان بیماری ها با طب سنتی"),
                        //new KeyboardButton("\U0001F4F7 کد عکس"),
                    }
                }, resizeKeyboard: true);
                await bot.SendTextMessageAsync(tdDto.Message.Chat.Id, "انتخاب کنید",
                                               replyMarkup : keyboard);
            }

            return(new CommandResult());
        }
コード例 #4
0
        protected override async Task <CommandResult> InternalExecute(BackToMenuDtc dtc)
        {
            var cache = _telegramFactory.GetCache();
            var bot   = _telegramFactory.GetTelegramBot();

            cache.Remove(dtc.Message.Chat.Id.ToString());
            var keyboard = new ReplyKeyboardMarkup(new[]
            {
                new[]     // first row
                {
                    new KeyboardButton("\U0001F4A1 درباره ما")
                },
                new[]     // last row
                {
                    new KeyboardButton("شناخت طبع مواد خوراکی"),
                    new KeyboardButton("درمان بیماری ها با طب سنتی"),
                    //new KeyboardButton("\U0001F4F7 کد عکس"),
                }
            }, resizeKeyboard: true);
            await bot.SendTextMessageAsync(dtc.Message.Chat.Id, "انتخاب کنید",
                                           replyMarkup : keyboard);

            return(new CommandResult());
        }