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); }
public void Cache(BaseBotClient botClient) { _cached.Add(botClient.Id, botClient); }
public BaseBotClient GetCached(BaseBotClient botClient) { return(_cached[botClient.Id]); }
public bool IsCached(BaseBotClient botClient) { return(_cached.ContainsKey(botClient.Id)); }
public void SetBaseClient(BaseBotClient baseBotClient) { _client = baseBotClient; }
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); }