Example #1
0
    private static IApplicationBuilder UseTelegramBotPoolingMode(this IApplicationBuilder app)
    {
        var configureBot = CommandBuilderExtension.ConfigureBot();

        Log.Information("Starting Bot in Pooling mode..");

        // get bot updates from Telegram via long-polling approach during development
        // this will disable Telegram webhooks
        app.UseTelegramBotLongPolling <BotClient>(configureBot, TimeSpan.FromSeconds(1));

        Log.Information("Bot is ready in Pooling mode..");

        return(app);
    }
Example #2
0
    private static IApplicationBuilder UseTelegramBotWebHookMode(this IApplicationBuilder app)
    {
        var configureBot  = CommandBuilderExtension.ConfigureBot();
        var tgBotConfig   = app.GetRequiredService <IOptions <TgBotConfig> >().Value;
        var tunnelService = app.GetRequiredService <LocalTunnelService>();

        var configuration = app.GetRequiredService <IConfiguration>();

        Log.Information("Starting Bot in WebHook mode..");

        // use Telegram bot webhook middleware in higher environments
        app.UseTelegramBotWebhook <BotClient>(configureBot);

        if (tgBotConfig.EnableLocalTunnel)
        {
            var tunnelSubdomain = tgBotConfig.LocalTunnelSubdomain;

            var urlHost  = configuration.GetValue <string>("Kestrel:Endpoints:Http:Url");
            var parted   = urlHost.Split(":");
            var portHost = parted.ElementAtOrDefault(2);

            var tunnel      = tunnelService.CreateTunnel(tunnelSubdomain, portHost);
            var tunnelUrl   = tunnel.Information.Url;
            var webhookPath = tgBotConfig.WebhookPath;
            var webHookUrl  = Url.Combine(tunnelUrl.ToString(), webhookPath);

            Log.Information("Setting WebHook to {TunnelUrl}", webHookUrl);

            var botClient = app.GetRequiredService <ITelegramBotClient>();
            botClient.DeleteWebhookAsync().WaitAndUnwrapException();
            botClient.SetWebhookAsync(webHookUrl).WaitAndUnwrapException();

            var webhookInfo = botClient.GetWebhookInfoAsync().WaitAndUnwrapException();
            Log.Information("Updated WebHook info {@WebhookInfo}", webhookInfo);
        }
        else
        {
            // and make sure webhook is enabled
            app.ApplicationServices.EnsureWebhookSet <BotClient>();
        }

        Log.Information("Bot is ready in WebHook mode..");

        return(app);
    }
Example #3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            var botBuilder = CommandBuilderExtension.ConfigureBot();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            // else
            // {
            // app.UseTelegramBotWebhook<ZiziMirror>(botBuilder);
            // }

            app.UseTelegramBotLongPolling <ZiziMirror>(botBuilder, TimeSpan.FromSeconds(2));

            // app.UseHangfireDashboardAndServer();

            app.UseRouting();

            app.UseEndpoints(endpoints => { endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello World!"); }); });
        }