majmccloud / telegrambotframework Goto Github PK
View Code? Open in Web Editor NEWThis is a context based application framework for the C# TelegramBot library.
Home Page: https://www.t.me/tgbotbase
License: MIT License
This is a context based application framework for the C# TelegramBot library.
Home Page: https://www.t.me/tgbotbase
License: MIT License
So, what different between Init and Opened events in the Form? I didn't find explation in documentation
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);
Although one could handle it by catching an exception but I think better way is to null check the text.
How session serializer works?
I want to develop my own session serializer with mongodb.
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.
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);
}
}
[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;
}
}
[BotCommand(nameof(Greeting), "Greets user")]
public class Greeting : FormBase
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.
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:
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;
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? For example CalendarPicker?
I create autoclean messaged bot and when i restart the app i can't restore message of CalendarPicker for clean it
Hi
Is it possible to provide an example for a asp.net core web application?
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.
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.
How can i handle message when press on keyboard button?
TelegramBotFramework/TelegramBotBase/Form/Navigation/NavigationController.cs
Lines 146 to 147 in b32347f
Because when there is no "userevent" ur.RawData
is null and hence update.Type
is null and the code throws an exception on Invoking the Message Loop manually without a user event
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.