Exemple #1
0
        public void Run(BotManager botManager, bool isDebug)
        {
            botManager.ConfigureServices((servicesWrap) =>
            {
                var serv = servicesWrap.Services;
                LoggerStarter.InitLogger(servicesWrap);
                servicesWrap.AddMvc(new MvcOptions()
                {
                    CheckEqualsRouteInfo = true
                });

                serv.AddSingleton <ITelegramBotClient>((sp) =>
                {
                    return(botManager.BotContext.Bot);
                });
                serv.AddScoped <ISomeScopedService, SomeScopedService>();


                //Telegram storage test
                var opt = new TelegramStorageOptions()
                {
                    SaveResourcesChatId = BotTokenResolver.GetSaveResChatId(),
                    SaveOnSet           = true
                };
                serv.AddSingleton(opt);
                serv.AddSingleton <TelegramStorage>();
                serv.AddSingleton <IKeyValueStorage>((sp) =>
                {
                    return(sp.GetRequiredService <TelegramStorage>());
                });
                serv.AddSingleton <TelegramFilesCloud>();
            });

            botManager.ConfigureBuilder((builder) =>
            {
                if (isDebug)
                {
                    builder.UseDevEceptionMessage();
                }

                builder.UseExceptionHandler(async(ctx, ex) =>
                {
                    //Throw exception if false. False mean 'not handled'.
                    return(false);
                });

                builder.UseMvc(mvcBuilder =>
                {
                    mvcBuilder.UseDebugInfo();
                    mvcBuilder.MapRouteAction(async(actionCtx) =>
                    {
                        await actionCtx.UpdateContext.SendTextMessageAsync("Mvc works.");
                    }, template: "/mvc");
                });
            });
            botManager.Start();
        }
        public void Run(BotManager botManager, bool isDebug)
        {
            botManager.ConfigureServices((servicesWrap) =>
            {
                LoggerStarter.InitLogger(servicesWrap);
            });

            botManager.ConfigureBuilder((builder) =>
            {
                builder.AddBotExtGlobalValidator(async(upd, origCtx) =>
                {
                    if (upd.Message?.Text.StartsWith("/") == true)
                    {
                        return(UpdateValidatorResult.AbortWaiter);
                    }
                    return(UpdateValidatorResult.Valid);
                });
                builder.Use(async(ctx, next) =>
                {
                    if (ctx.Message?.Text == null)
                    {
                        await ctx.SendTextMessageAsync("Not text message.");
                        ctx.ForceExit();
                        return;
                    }

                    var ctxTrimmedText = ctx.Message.Text.Trim();
                    Message msg        = null;
                    if (ctxTrimmedText.StartsWith("/help"))
                    {
                        await ctx.SendTextMessageAsync("Commands:\n" +
                                                       "/current_user_reply\n" +
                                                       "/current_user\n" +
                                                       "/any_user\n" +
                                                       "/any_user_reply");
                        ctx.Processed();
                    }
                    else if (ctxTrimmedText.StartsWith("/any_user_reply"))
                    {
                        await ctx.SendTextMessageAsync("Reply to bot to process message.");
                        msg = await ctx.BotExt.ReadMessageAsync(ReadCallbackFromType.AnyUserReply);
                    }
                    else if (ctxTrimmedText.StartsWith("/current_user_reply"))
                    {
                        await ctx.SendTextMessageAsync("Reply to bot to process message.");
                        msg = await ctx.BotExt.ReadMessageAsync(ReadCallbackFromType.CurrentUserReply);
                    }
                    else if (ctxTrimmedText.StartsWith("/current_user"))
                    {
                        msg = await ctx.BotExt.ReadMessageAsync();
                    }
                    else if (ctxTrimmedText.StartsWith("/any_user"))
                    {
                        msg = await ctx.BotExt.ReadMessageAsync(ReadCallbackFromType.AnyUser);
                    }

                    if (msg != null)
                    {
                        ctx.Processed();
                        var msgText = msg.Text;
                        await ctx.SendTextMessageAsync($"Command : '{ctxTrimmedText}'.\n" +
                                                       $"Awaited msg: '{msgText}'.");
                    }


                    await next();
                });
                builder.Use(async(ctx, next) =>
                {
                    //if (!ctx.IsProcessed)
                    //    await ctx.SendTextMessageAsync($"Not processed '{ctx.Message.Text}'.");
                    await next();
                });

                builder.UseExceptionHandler(async(ctx, ex) =>
                {
                    return(false);
                });
                builder.UseDevEceptionMessage();
            });

            botManager.Start();
        }