Code Monkey home page Code Monkey logo

telegrambotframework's People

Contributors

alex6dj avatar dependabot[bot] avatar jtone123 avatar kataane avatar majmccloud avatar mottaghipour avatar ntdv avatar palashjhabak avatar ramax495 avatar victorrar avatar xilo14 avatar zavarukitsu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

telegrambotframework's Issues

Keyboard Markup always is hided when navigate to next form on phone but not on desktop

I use grid to show buttons as keyboard in AutoCleanForm.(If form is derived from FormBase everything is ok)
On telegram desktop everything is ok, but on iphone when form is shown keyboard is hided.

ButtonGrid grid = new ButtonGrid(btns);

grid.KeyboardType = TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard;
grid.Enabled = true;
grid.HideKeyboardOnCleanup = false;
grid.ResizeKeyboard = true;
 
AddControl(grid);

Improve navigation use DI and IServiceProvider

Hi. I am currently working on implementing automatic form call using IServiceProvider. Since in the new .NET 8 they added the ability to get services by key.

The first thing that came to mind is to create a scope at the user session level.
That is, the session started, created a start form via IServiceProvider. And then there is automatic navigation by parsing the command from the user and getting the form using the IServiceProvider and the command as a key.
But it is difficult to implement it now, because SessionManager and DeviceSession are closed for extension.

At the moment i have the following working implementation.

  1. Since the whole BotBase and session creation is not extensible. Making the wrapper / facade. Which will mimic BotBase.
  2. Intercept the message to see if the message is a command.
public class BotBaseInjectable : IBotBaseInjectable
{

    public BotBaseInjectable(IServiceProvider serviceProvider, BotBaseBuilder builder)
    {
    }
	
    private async Task Bb_BotCommand(object sender, BotCommandEventArgs e)
    {
        BotCommand?.Invoke(sender, e);
        if (!botBase.IsKnownBotCommand(e.Command))
        {
            return;
        }
        await using var scope = serviceProvider.CreateAsyncScope();
        var type = scope.ServiceProvider.GetRequiredKeyedService<FormBase>(e.Command).GetType();
        await DependencyInjection.Extensions.NavigateTo(e.Device.ActiveForm, type);
    }
}
  1. Add a command attribute that specifies the command and its description
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class BotCommandAttribute : Attribute
{
    public string Command { get; }
    public string Description { get; }

    public BotCommandAttribute(string command, string description)
    {
        Command = command.ToLower();
        Description = description;
    }
}
  1. Create a command-form with a new attribute
[BotCommand(nameof(Greeting), "Greets user")]
public class Greeting : FormBase
  1. All that's left to do is register it
public static void BuildBotBase<TStartForm>(this IHostApplicationBuilder builder, Assembly assembly, Func<IServiceProvider, BotBaseBuilder> botBaseFactory)
	where TStartForm : FormBase
{
	var botCommands = assembly.GetTypes()
		.Where(t => t.IsAssignableTo(typeof(FormBase)) && t.IsClass && t.GetCustomAttribute<BotCommandAttribute>() != null)
		.ToList();

	var botCommandScopes = new Dictionary<BotCommandScope, List<BotCommand>>();

	foreach (var botCommand in botCommands)
	{
		var command = botCommand.GetCustomAttribute<BotCommandAttribute>() ?? throw new ArgumentNullException(nameof(BotCommandAttribute));
		if (string.IsNullOrEmpty(command.Command) || string.IsNullOrEmpty(command.Description)) continue;
		var fullCommand = '/' + command.Command;
		builder.Services.AddKeyedScoped(typeof(FormBase), fullCommand, botCommand);

		Commands.Extensions.Add(botCommandScopes, command.Command, command.Description);
	}

	builder.Services.AddSingleton(typeof(IBotBaseInjectable), serviceProvider =>
	{
		var factory = botBaseFactory(serviceProvider);
		((IStartFormSelectionStage)factory).WithServiceProvider<TStartForm>(serviceProvider);
		((IBotCommandsStage)factory).CustomCommands(a =>
		{
			foreach (var botCommand in botCommandScopes)
			{
				a.Add(botCommand.Key, botCommand.Value);
			}
		});
		return new BotBaseInjectable(serviceProvider, botCommandScopes, factory);
	});
}

It looks great and work, but it's not.

BotBaseInjectable is kludge doing this kind of navigation looks rather strange. Besides, it is hard to extend many things, for example SessionManager and DeviceSession.
Maybe there are some ideas for improvement? Ready to assist in implementation.

Each step in the BotBase factory is bound to the next step, which makes it necessary to cast BotBaseBuilder to the required step.

Also, the problem with this solution is that you can't ignore StartFormFactory. And every time you intercept commands, StartForm will be displayed.

Best regards, Kataane.

Exception when use Calendar/Month picker

I have a simple StartForm with method OnInit - handler of Init event. In OnInit method i create calendar/month picker:

    private async Task OnInit(object sender, InitEventArgs ev)
    {
        MonthPicker monthPicker = new MonthPicker();
        AddControl(monthPicker);
    }

Also in StartForm i have overriden Render method with empty body.

When i started the bot, i got exception in Render method when i tried move to next/previous year:
photo_2023-01-20_18-22-01

How can i use NavigationController

How can i use it?
I set NavigationController property in my StartForm in Init event handler:
NavigationController = new NavigationController(this);
Then in some case i invoke transition:

var form1 = new SetFiltersForm();
await NavigationController.PushAsync(form1);

and nothing changes...

Because in PushAsync method we have a strange condition:

            if (Index < 2)
                return;

How to use the repo to chat with some chat bot?

Hello:
I have a number of json format text in foreign language, which I don’t understand. I found that there are some translation bot in Telegram, it can translate the text into English. I did some test by copying and pasting some Json format text to the chat bot, and I got reply with translation very fast. The Json format message looks like this:
{
"id": 123,
"type": "message",
"date": "2022-06-22T01:00:22",
"date_unixtime": "1655859622 ",
"edited": "2022-06-22T01:22:36",
"edited_unixtime": "1655868575",
"from": "Україна 24",
"from_id": "channel12345",
"text": [
"⚡️",
{
"type": "bold",
"text": "Термінове Звернення Президента України Володимира Зеленського!"
},
"Дивіться онлайн: ",
{
"type": "link",
"text": "https://youtu.be/aLOh7uyk2-w"
},
""
]
},

I want to know how I can write C# program to send json format text to the translation bot, let’s say its ID# 123456789.

I have used other C# Telegram framework, I can start my bot and join some public channel. But I don’t know how to chat with a translate bot or chat bot.
I want to know how to send json format text to chat bot and read reply from it. What is the speed limit to send json format text? If I send too quick, I will be banned. So I want to set some kind of speed limit, like 10 json format data per second? Is this speed too fast or it is OK?
Please show me some code samples on this topic. I also need some code samples on how to reuse session data after I got verification code.
By the way, I am using Visual Studio 2022 Version 17.2.5 on Windows 10 Version 21H2.
Thanks,

How can i serialize state of controls?

How can i serialize state of controls? For example CalendarPicker?
I create autoclean messaged bot and when i restart the app i can't restore message of CalendarPicker for clean it

Incorrect overwriting of an error message during resolving dependencies

If we register a dependency incorrectly, so BotBase can't resolve it, we won't get a correct error message.

The problem happens at
SessionManager.cs / StartSession Method 62 line

var start = BotBase.StartFormFactory.CreateForm();

and then at ServiceProviderStartFormFactory.cs / 27 line

var fb = (FormBase)ActivatorUtilities.CreateInstance(_serviceProvider, _startFormClass);

We should recieve "Cannot resolve scoped service 'X' from root provider." but insted we get System.NullReferenceException: Object reference not set to an instance of an object.

Proactively Send a Form to User

Hi,

First of all thanks for an excellent framework.

I wanted to know if I want to send user a Form proactively without user asking for it, how do I do? All I have is User's device/chat Id stored in my database from a previous conversation and do not have any open session with user currently.

Method not found: 'Void Telegram.Bot.TelegramBotClient..ctor

Hi,

Thanks so much for this project, Im really looking forward to migrating to this from the "low level" telegram.bot - hopefully it provides us with more flexibility. I am running into the following issue during the execution of the AsyncFormUpdates project

bot = new TelegramBotBase.BotBase<forms.Start>(apiKey);

I am getting the following error. Using this nuget package within my application I am getting the same error.

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.