예제 #1
0
        public async Task Invoke(HttpContext context, BotPool pool, BaseBotClient baseBotClient, BotClient botClient)
        {
            var token = context.Request.Headers.FirstOrDefault(x => x.Key == "Token").Value.ToString();

            if (!string.IsNullOrEmpty(token))
            {
                baseBotClient = BaseBotClient.FromToken(token);

                var isCached = pool.IsCached(baseBotClient);

                context.Response.Headers.Add("X-IS-FIRST-TIME", isCached.ToString());

                if (isCached)
                {
                    baseBotClient = pool.GetCached(baseBotClient);
                }
                else
                {
                    baseBotClient.Start();
                    pool.Cache(baseBotClient);
                }

                botClient.SetBaseClient(baseBotClient);
            }
            else
            {
                if (!context.Request.Path.Value.Contains("swagger"))
                {
                    context.Response.StatusCode = 401;
                    return;
                }
            }

            await _next(context);
        }
예제 #2
0
 public void Cache(BaseBotClient botClient)
 {
     _cached.Add(botClient.Id, botClient);
 }
예제 #3
0
 public BaseBotClient GetCached(BaseBotClient botClient)
 {
     return(_cached[botClient.Id]);
 }
예제 #4
0
 public bool IsCached(BaseBotClient botClient)
 {
     return(_cached.ContainsKey(botClient.Id));
 }
예제 #5
0
 public void SetBaseClient(BaseBotClient baseBotClient)
 {
     _client = baseBotClient;
 }
예제 #6
0
        public static BaseBotClient FromToken(string token)
        {
            var tdClient = new TdClient();
            var bot      = new BaseBotClient
            {
                Token  = token,
                Client = tdClient,
            };

            tdClient.Bindings.SetLogVerbosityLevel(0);

            tdClient.UpdateReceived += async(sender, update) =>
            {
                switch (update)
                {
                case TdApi.Update.UpdateOption option:
                    /*await tdClient.ExecuteAsync(new TdApi.SetOption
                     * {
                     *  DataType = option.DataType,
                     *  Extra = option.Extra,
                     *  Name = option.Name,
                     *  Value = option.Value,
                     * });*/
                    break;

                case TdApi.Update.UpdateAuthorizationState updateAuthorizationState when updateAuthorizationState.AuthorizationState.GetType() == typeof(TdApi.AuthorizationState.AuthorizationStateWaitTdlibParameters):
                    Directory.CreateDirectory($"bots/{bot.RawId}");

                    await tdClient.ExecuteAsync(new TdApi.SetTdlibParameters
                    {
                        Parameters = new TdApi.TdlibParameters
                        {
                            ApiId              = 94575,
                            ApiHash            = "a3406de8d171bb422bb6ddf3bbd800e2",
                            ApplicationVersion = "1.3.0",
                            DeviceModel        = "PC",
                            SystemLanguageCode = "en",
                            SystemVersion      = "Win 10.0",
                            DatabaseDirectory  = $"bots/{bot.RawId}",
                        }
                    });

                    bot._initialized.Set();
                    break;

                case TdApi.Update.UpdateAuthorizationState updateAuthorizationState when updateAuthorizationState.AuthorizationState.GetType() == typeof(TdApi.AuthorizationState.AuthorizationStateWaitEncryptionKey):
                    await tdClient.ExecuteAsync(new TdApi.CheckDatabaseEncryptionKey());

                    break;

                case TdApi.Update.UpdateAuthorizationState updateAuthorizationState when updateAuthorizationState.AuthorizationState.GetType() == typeof(TdApi.AuthorizationState.AuthorizationStateWaitPhoneNumber):
                    await tdClient.ExecuteAsync(new TdApi.CheckAuthenticationBotToken {
                        Token = token
                    });

                    break;

                case TdApi.Update.UpdateUser updateUser:
                    break;

                case TdApi.Update.UpdateConnectionState updateConnectionState when updateConnectionState.State.GetType() == typeof(TdApi.ConnectionState.ConnectionStateReady):
                    break;

                case TdApi.Update.UpdateNewMessage newMessage:
                    if (bot.LastUpdateId.TryGetValue(UpdateTypeEnum.NewMessage, out var updateId))
                    {
                        if (bot.Updates[UpdateTypeEnum.NewMessage].Count > MaxStoredUpdatesCount)
                        {
                            bot.Updates[UpdateTypeEnum.NewMessage].RemoveAt(0);
                        }

                        bot.Updates[UpdateTypeEnum.NewMessage].Add(updateId, newMessage);
                        bot.LastUpdateId.TryUpdate(UpdateTypeEnum.NewMessage, updateId + 1, updateId);
                    }
                    break;

                default:
                    ;     // add a breakpoint here to see other events
                    break;
                }
            };

            return(bot);
        }