Compare commits

...
313 Commits
Author SHA1 Message Date
Florian Dahn 3340ef7b76 Update README.md 2022-01-10 13:59:26 +01:00
Florian Dahn 211424569b Merge pull request #18 from MajMcCloud/New-Version-v5
New version v5
2022-01-10 13:58:50 +01:00
FlorianDahn 8303b0c87f Adding private constructor
- Adding private constructor
- some spacing for regions
2022-01-06 16:54:50 +01:00
FlorianDahn 19856ab044 Update Solution File 2022-01-06 15:56:16 +01:00
FlorianDahn 5453add956 Renaming TelegramBotBaseTest -> TelegramBotBase.Test 2022-01-06 15:53:41 +01:00
FlorianDahn ff3c22566a Comment Cleanup 2022-01-05 18:40:44 +01:00
FlorianDahn 50fd5255c8 Adding SourceLink to Image Extension 2022-01-05 18:39:35 +01:00
FlorianDahn 4f85677deb Comment Cleanup 2022-01-05 18:39:06 +01:00
FlorianDahn 7d2703138c Readme fix to "generics" like in earlier versions. 2022-01-05 18:31:30 +01:00
FlorianDahn d0d93a07c0 Adding QuickStart to readme. 2022-01-05 18:30:25 +01:00
FlorianDahn f03e67f345 Adding QuickStart Option for BotBaseBuilder 2022-01-05 18:26:10 +01:00
FlorianDahn 18bb6584f4 Adding OnlyStart BotBaseBuilder command option 2022-01-05 18:25:33 +01:00
FlorianDahn d5a89a4ed8 Adding regions to BotBaseBuilder 2022-01-05 18:25:04 +01:00
FlorianDahn 09d7164385 Update README.md 2022-01-05 14:22:21 +01:00
FlorianDahn 2b914f6f58 Changing CustomMessageLoop parameter 2022-01-05 01:17:44 +02:00
FlorianDahn b65ec13d8b Simplifying BotBuilder BotCommands
- Simplifying BotBuilder BotCommands
- updating test project
2022-01-05 01:16:22 +02:00
FlorianDahn ca1c382924 Removing old commented stuff 2022-01-04 23:44:43 +02:00
FlorianDahn 584bb76e0f Adding comments 2022-01-04 23:42:13 +02:00
FlorianDahn c5fa153d54 Replace KickChatMemberAsync/KickUser 2022-01-04 23:40:33 +02:00
FlorianDahn c6106f17ec Removing .Net 4.7.2 and adding .Net Standard 2.0 instead of 2.1
- removing .Net 4.7.2 compilation target
- changing .Net Standard from 2.1 to 2.0 for backwards compatibility
2021-12-25 17:52:17 +01:00
FlorianDahn 4f97e2a919 Changing debugtype to portable to allow snupkg file to be attached. 2021-12-25 17:47:11 +01:00
FlorianDahn 21c6b2fde5 removing .NET 472 compilation target from test project 2021-12-25 17:34:47 +01:00
FlorianDahn caf9b3cc8e Change version of polling library to 1.0.1 2021-12-25 17:34:20 +01:00
FlorianDahn 27e45b397d Changing debugtype to portable to allow snupkg file to be attached.
- removing double Releae config in project settings
- change debugtype to portable to allow correct snupkg package upload
2021-12-25 17:32:21 +01:00
FlorianDahn b8bdc756d4 Rename file to class name 2021-12-25 16:13:31 +01:00
FlorianDahn e5d8e403a0 Update readme with new BotBaseBuilder 2021-12-25 16:12:26 +01:00
FlorianDahn 2c58f86524 Adding localization selection step to BotBaseBuilder
- Adding localization step
- Updating test project
2021-12-25 16:10:32 +01:00
Florian Dahn 4bd43de0fe Update README.md 2021-12-22 15:40:14 +01:00
Florian Dahn 871d25b95b Create README.md 2021-12-22 15:38:46 +01:00
Florian Dahn b508a7e3ce Update README.md 2021-12-20 14:16:28 +01:00
Florian Dahn 0ebba5e204 Update README.md 2021-12-20 14:15:52 +01:00
FlorianDahn 1cc0a2bdf7 Ignore Callback queries with original image attached to message, no SentData event 2021-12-19 14:55:23 +01:00
FlorianDahn fc33da0496 Fixing empty message field 2021-12-17 15:15:58 +01:00
FlorianDahn 35045f3aeb Removing Newtonsoft Dependency 2021-12-16 15:08:56 +01:00
FlorianDahn 3b8b24593f Dependency Update
- downgrade NetStandard from 2.1 to 2.0 to allow .NetFramework usage from 4.6.1 to 4.8
- remove .Net Framework 4.7.2 compilation target
- replacing conditioned compilation for .Net Framework with .Net Standard
- removing System.Net.Http dependency (was only necessary for .Net Framework)
2021-12-16 14:57:28 +01:00
FlorianDahn 03565aba98 Replacing Nuget packages with project references 2021-12-06 02:41:55 +01:00
FlorianDahn bbc436d1d6 Merge branch 'New-Version-v5' of https://github.com/MajMcCloud/TelegramBotFramework into New-Version-v5 2021-12-06 02:37:01 +01:00
FlorianDahn 4026f9ba4e Update for easier serialization during BotBaseBuilder 2021-12-06 02:36:59 +01:00
Florian Dahn fb38d32e9e Update README.md 2021-11-29 15:57:49 +01:00
FlorianDahn 077bd1384f Removing System.ComponentModel dependency 2021-11-28 23:58:23 +01:00
FlorianDahn e28663138b Integrating Images Project 2021-11-28 23:20:56 +01:00
FlorianDahn 65ec7f1c2b Update TelegramBotBase.csproj 2021-11-26 17:59:58 +01:00
FlorianDahn 1fc581b959 V17 - Testproject Update 2021-11-26 17:58:49 +01:00
FlorianDahn 31e52887ba V17 - Big Update
- Adding a message loop interface to build custom message loops
- extracting default message loop from BotBase into seperate class
- updates to BotBaseBuilder for integration of custom message loops
- updating all result classes for using the new Update object of V17
- improving MessageResult and UpdateResult classes
- BotBase has been prepared for cleanup (a lot of comments)
- Comment cleanup at the next build
- updating Readme
2021-11-26 17:57:49 +01:00
FlorianDahn e9c25ea9e4 New ConfirmAction method 2021-11-26 17:49:33 +01:00
FlorianDahn 2aee486370 V17 - Replacing default enum values 2021-11-26 17:49:17 +01:00
FlorianDahn c1a0b12a94 Replace Configure with CustomCommands 2021-11-26 17:47:42 +01:00
FlorianDahn 2bd4634624 V17 - Remove default selection for enums 2021-11-26 17:45:24 +01:00
FlorianDahn 0bc77ca79c Update README.md 2021-11-26 17:43:38 +01:00
FlorianDahn d0edf8531c Replacing Configure method in BBB with CustomCommands 2021-11-22 22:07:55 +01:00
FlorianDahn b001d5e9c4 Adding LambdaStartFormFactory 2021-10-17 18:38:35 +02:00
FlorianDahn e1d5051288 Update README.md 2021-10-17 18:31:28 +02:00
FlorianDahn d00c5e1799 Updating BotBaseBuilder with more functionality 2021-10-17 18:21:05 +02:00
FlorianDahn c4a31e987b Replacing BotBase instance with BotBaseBuilder 2021-10-17 18:07:53 +02:00
Florian Dahn eafb81bf7b Merge pull request #17 from MajMcCloud/master
PR for master changes branch
2021-10-17 17:35:13 +02:00
FlorianDahn 1dff9d5ffe Update IAPIKeySelectionStage.cs 2021-10-17 17:33:49 +02:00
FlorianDahn 755f5a245b Renaming 2021-10-17 17:27:06 +02:00
FlorianDahn a22ede0f4f Changing BotBase behaviour to fluent api
- removing unecessary constructors from BotBase
- removing generics from BotBase
- removing generics from SessionBase
- adding StartFormFactory interface
- adding DefaultStartFormFactory
- adding multiple methods to BotBaseBuilder
2021-10-17 17:25:17 +02:00
FlorianDahn 8e29652148 Fixing possible Deadlock in AutoCleanForm 2021-10-17 15:39:31 +02:00
Florian Dahn 5bb316d874 Merge pull request #15 from victorrar/f/update-readme
Fix documentation inaccuracy

Thanks !
2021-10-04 21:35:59 +02:00
Victor d2ae08771e Fix documentation inaccuracy 2021-10-04 21:55:41 +03:00
Florian Dahn 9162c0894c Merge pull request #14 from MajMcCloud/development
AutoCleanForm FIX
2021-10-01 18:36:24 +02:00
FlorianDahn e0ec133209 AutoCleanForm FIX
- adding MessageDeleted event to AutoCleanForm for catching messages getting deleted outside of AutoCleanForm
- added OnMessageDelete invoke to DeleteMessage in DeviceSession
2021-10-01 18:35:05 +02:00
Florian Dahn 7004ee5963 Merge pull request #13 from MajMcCloud/development
Development
2021-08-13 15:33:12 +03:00
FlorianDahn ef8ca3c988 Adding basic example for different types of ConfirmAction 2021-08-01 13:50:18 +03:00
FlorianDahn f021f12486 Update Localization.cs
- change to abstract so it can't be used accidentally
2021-07-27 17:00:16 +02:00
FlorianDahn d06e1eaa52 Update README.md 2021-07-26 15:52:59 +02:00
FlorianDahn 23969881c1 Update README.md 2021-07-26 15:44:06 +02:00
FlorianDahn 9fa3df2889 Update README.md 2021-07-26 15:42:04 +02:00
FlorianDahn 6ed91224eb Update README.md 2021-07-26 15:39:47 +02:00
FlorianDahn aabc7f04b7 Adding documentation about NavigationController 2021-07-26 15:38:47 +02:00
FlorianDahn 71433c0e4e MAJOR CHANGE for ButtonGrids, Dynamic data sources, etc
- introducing a dynamic data source class (IDataSource)
- introducing a ButtonRow class for better managability
- replacing that List<ButtonBase> with ButtonRow object
- introducing ButtonFormDataSource with special methods for ButtonGrid controls
- updating ButtonGrid and refactoring of the Load/Action methods
- updating CheckButtonList and refactoring of the Load/Action methods
- updating TaggedButtonGrid and refactoring of the Load/Action methods
- adding example to the Test project
2021-07-26 15:10:10 +02:00
FlorianDahn 894d0799c8 Localizations updated
- moved German Localization to its seperate file
- cleaned up Localization class
2021-07-26 14:55:29 +02:00
FlorianDahn 6c2543630d Adding .NET 4.7.2 back into with conditional compilation
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives
2021-07-26 13:32:32 +02:00
FlorianDahn 84d18f6f45 Small readability improvements 2021-07-26 01:26:33 +02:00
FlorianDahn 5f49e25458 Adding CheckAll and UncheckAllTags methods 2021-07-26 01:23:31 +02:00
FlorianDahn f28c9b1e22 Add DeleteReplyMessage property 2021-07-26 01:22:58 +02:00
FlorianDahn 0db0c00cd0 Update example project 2021-07-25 22:25:29 +02:00
FlorianDahn 2158f53cde Fixing ReplyKeyboard flicker on TaggedButtonGrid
- fixing ReplyKeyboard flicker
- updating message handling on existing messages
2021-07-25 17:44:43 +02:00
FlorianDahn fd7d72b986 Update NavigationController.cs 2021-07-25 16:40:09 +02:00
FlorianDahn 47a5b502b9 Updating package targetFramework to .NET 5 2021-07-25 16:38:53 +02:00
FlorianDahn ce4c7b6f1c Update TelegramBotBase.csproj 2021-07-25 16:36:43 +02:00
FlorianDahn 5bae650edc Updating compilation targets
- Removing .NET 4.7.2 and  and replacing it with .NET 5
2021-07-25 16:35:15 +02:00
FlorianDahn 9eae63dae6 Updating Testproject
- adding example for NavigationController
- readability improvement in Menu
2021-07-25 16:34:29 +02:00
FlorianDahn 6f005114bc Update NavigationController.cs
- Adding PopToRootAsync method
- small comment change
2021-07-25 16:33:05 +02:00
Florian Dahn 00440a50e8 Merge pull request #11 from MajMcCloud/master
Move Master changes to development branch
2021-07-25 05:07:31 +03:00
FlorianDahn ae9dd17ae3 Adding NavigationController for better and optional Push/Pop navigation 2021-07-25 01:54:16 +02:00
FlorianDahn be2660239a Creating DeviceSession and other stuff slight earlier in the LoadSessionStates methods 2021-07-25 01:53:55 +02:00
Florian Dahn e88114049e Merge pull request #8 from Xilosof/feat/QuickDeletionOfMsgs
Thanks for the added change. I will approve it now.
2021-07-25 02:10:30 +03:00
Xilosof 177c198916 Added event call 2021-07-23 16:47:42 +03:00
FlorianDahn 1f7a9900c4 Updating TelegramBotBaseTest project
- adding new simple AddStartCommand, AddHelpCommand and AddSettingsCommand to Program.cs to make it easier
2021-07-17 18:36:56 +02:00
Florian Dahn d6051ce607 Merge pull request #10 from MajMcCloud/development
Updating to TelegramBotFramework 16.0.2
2021-07-17 19:35:17 +03:00
FlorianDahn 5613e84bc3 Updating to TelegramBotFramework 16.0.2
ATTENTION: UserIds will no longer be "int", instead it has to be of the long datatype
- updating DeviceSession methods for new datatype
- updating LinkReplaceTest class example
- adding .NET 5 as target framework
2021-07-17 18:34:27 +02:00
Florian Dahn d52fd41467 Merge pull request #9 from MajMcCloud/development
Development
2021-07-14 23:11:32 +03:00
Florian Dahn 8df6e4ce55 Merge pull request #7 from NTDV/master
CalendarPicker fixes
2021-07-14 22:35:13 +03:00
Xilosof 9012ec76b1 feat(AutoCleanForm): change deletion of msgs
- made faster deletion of old messages;
- added server error handling
2021-07-13 22:48:01 +03:00
Danich Volk 36fb40b6bd CalendarPicker:
+ Added days, monthes and years range check
+ Added language selection
* Fixed default language (it is English now)
2021-07-13 21:18:55 +03:00
FlorianDahn 47aa8c2aaa Extracting custom field conversion for use in other places 2021-06-07 01:35:02 +02:00
FlorianDahn 1709631965 adding GetObject and SaveObject methods for custom session serialization
- adding GetObject and SaveObject methods for custom session serialization
2021-06-07 01:33:57 +02:00
FlorianDahn 264a998052 Update FormBase.cs
- making more basic methods virtual for easiert overriding
- adding existing control check to AddControl method
2021-06-07 01:33:19 +02:00
FlorianDahn a00aaacbfb Update ButtonGrid.cs
- adding DeleteReplyMessage property for seperating now deleting the message from the user and the message (buttons) send by the control
2021-06-07 01:31:41 +02:00
Florian Dahn bd908db867 Merge pull request #4 from MajMcCloud/development
A lot of updates and fixes
2021-05-16 15:09:09 +02:00
FlorianDahn e2c485aede Adding some conversion checks for JSON State serialization 2021-04-25 15:51:19 +02:00
FlorianDahn c8936d0748 Adding more details to PromptDialog
- replacing the default EventArgs within the Completed event with a more detailed one
- adding new PromptDialogCompletedEventArgs class for easier details access
2021-03-29 23:51:39 +02:00
FlorianDahn 92f12735c8 Adding more details on ConfirmDialog
- adding a Tag property for optional use in ArrayPromptDialog and ConfirmDialog
- adding it to the ButtonClickedEventArgs
2021-03-29 23:50:42 +02:00
Florian Dahn 8142b626b2 Merge pull request #3 from Xilosof/bug/ApiRequestException-handling
Fix handling ApiRequestException when sending a request.
2021-03-14 20:34:07 +01:00
Xilosof 5dcbe04a73 Added initialization of MaxNumberOfRetries setting 2021-03-14 22:03:13 +03:00
Xilosof e742ded371 Change exception handling when sending a request.
1) Now RetryAfter  is multiplied by 1000, because request contains value in second instead milliseconds. (see https://core.telegram.org/bots/api#responseparameters)
2) Now calls occur in a loop, but a limited number of times. This also ensures that exceptions are caught during a repeated request.
2021-03-14 22:00:51 +03:00
Xilosof 706b0f522a Add static propert MaxNumberOfRetries.
This is necessary so that the DeviceSession instance can get this setting. There is no direct access to the settings in BotBase at the moment.
This property will be set when calling the Start method for the bot and is the same for all bots in the same application.
2021-03-14 21:55:07 +03:00
Xilosof 61e3652edf Add MaxNumberOfRetries setting.
Indicates the maximum number of times a request that received error 429 will be sent again after a timeout until it receives code 200 or an error code not equal to 429.
2021-03-14 21:43:07 +03:00
FlorianDahn c484edd427 Update README.md 2021-03-14 16:52:19 +01:00
FlorianDahn 181902336c Adding ChannelCreated messagetype to GroupForm 2021-03-14 16:51:25 +01:00
FlorianDahn dc364b5fd3 Fix for Cleanup method
- there was an collection changed exception which blocked the method from working
2021-03-04 15:17:06 +01:00
FlorianDahn 1fd4752c0e Slash fix 2021-03-02 15:25:35 +01:00
FlorianDahn d84be50243 Adding AutoCleanForm Fix for TaggedButtonGrid 2021-03-02 15:19:28 +01:00
FlorianDahn 1caa71cd25 Update to fix some bad behaviour with combination of AutoCleanForm 2021-03-01 22:53:32 +01:00
FlorianDahn e81e5f1854 Bot command extensions for default commands 2021-03-01 22:53:03 +01:00
FlorianDahn 399e056a8f Deleting messages update
- new constant for how much message deletions per second
- updating AutoCleanForm "Cleanup" process to use constant and using MessageDeleted eventhandler
2021-03-01 22:52:45 +01:00
FlorianDahn 26c4f479fa New internal MessageDeleted event handler for better control and form use. 2021-03-01 22:50:42 +01:00
FlorianDahn 72d94df907 FIX: new message loop will now correctly manage events and form switches 2021-03-01 22:14:36 +01:00
FlorianDahn 31e0683699 FIX: for using ButtonGrid within AutoCleanForm can cause to no message 2021-03-01 22:13:40 +01:00
FlorianDahn 676be49a62 Update MultiToggleButtons.cs 2021-03-01 01:54:49 +01:00
FlorianDahn 5b67f334ff MultiToggleButton minor updates
- updating localization
- adding AllowEmptySelection property
- updating test project
2021-03-01 01:48:17 +01:00
FlorianDahn 6a556af070 New control: MultiToggleButton
- new control for having multiple simple toggle options
2021-03-01 01:36:07 +01:00
FlorianDahn 4910a04923 Update README.md 2021-02-28 18:25:12 +01:00
FlorianDahn 10f4e1b745 New example project for async form updates 2021-02-28 18:23:21 +01:00
FlorianDahn 42b624c28f Fix for MessageType use when no Message is available 2021-02-28 18:22:08 +01:00
FlorianDahn 75013d51bd Remove old comments 2021-02-28 17:47:52 +01:00
FlorianDahn 7f3bcfe418 Speed/Performance optimization for AutoCleanForm 2021-02-28 17:46:49 +01:00
FlorianDahn f5e26924d8 Adding InvokeMessageLoop method
- For asyncronous form updates adding InvokeMessageLoop
2021-02-28 17:45:28 +01:00
FlorianDahn 884aa60fac Joining both message loops for easier maintainability 2021-02-28 17:44:35 +01:00
FlorianDahn adc18891e0 Update MessageResult.cs 2021-02-28 17:40:02 +01:00
FlorianDahn 07eb4bef75 Adding new controls to Readme 2021-02-28 15:44:05 +01:00
FlorianDahn dfe64b22ab New control: CheckedButtonList
- new control for having a checked listview with paging possible
- new CheckedChangedEventArgs class
- adding Test for CheckedButtonList
- refactoring MultiView Test
2021-02-28 15:35:49 +01:00
FlorianDahn 15a8b8897f Updates and improvements
- refactoring of ButtonGrid control for more readability
- refactoring of TaggedButtonGrid control for more readability
- adding Index parameter to ButtenClickedEventArgs
- adding FindRowByButton method to ButtonForm to get the row index
-
2021-02-28 15:34:04 +01:00
FlorianDahn 91048b4f7c Async invoke fixes and ButtonGrid update
- removing unused imports
- adding null check for async handler invokes
- fixing PageCount calculation on ButtonGrid
2021-02-27 20:51:47 +01:00
FlorianDahn 426bb078af Adding DebuggerDisplay attribute 2021-02-27 15:18:23 +01:00
FlorianDahn 63deb978a4 PageCount Fix for ButtonGrid
- replace constant 3 with LayoutRows property
2021-02-27 15:17:21 +01:00
FlorianDahn 1a2b5818aa New Control: TaggedButtonGrid
- tag your list with some strings and filter it right away
- adding Test to test project
- adding a new localization

PS: it is an improved copy of the normal ButtonGrid
2021-02-20 01:53:49 +01:00
FlorianDahn fc44b7d38c New Control: MultiView
- adding basic paging options
- adding GetOrigin method to find control or form who has sent a message
- adding Test for MultiView
2021-02-20 01:51:01 +01:00
FlorianDahn dc8cb81284 Adding Init method to controls 2021-02-20 01:39:37 +01:00
FlorianDahn 0202fc29a8 Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2021-02-19 15:03:33 +01:00
FlorianDahn 7e51126fd3 Fix for double sessions in sessionstate 2021-02-19 15:03:28 +01:00
Florian Dahn 67e82acf20 Update FUNDING.yml 2021-02-02 15:59:03 +01:00
Florian Dahn c32fc2c3da Update FUNDING.yml 2021-02-02 15:57:02 +01:00
Florian Dahn 4351fc7c68 Create FUNDING.yml 2021-02-02 15:56:27 +01:00
FlorianDahn 80d43ac369 Update DeviceSession.cs 2021-01-15 19:36:13 +01:00
FlorianDahn 376ba68e8e MAJOR CHANGE: Refactoring namespace for controls
Chaning namespace for controls depending on their use area to:

Controls.Hybrid
Controls.Inline
Controls.Reply

Updating example project as well.
2021-01-15 19:33:44 +01:00
FlorianDahn 6b404545df Update MessageResult.cs
- adding more parameters to ConfirmAction method
2021-01-15 16:53:45 +01:00
FlorianDahn a524fef0d5 Update DeviceSession.cs
Workaround: Unknown API differences
2020-12-23 03:09:47 +01:00
FlorianDahn 73ba36ea2b Optimization of API calls within DeviceSession
- replacing all API calls with new method "API" to cover APIRequestExceptions
- adding IDeviceSession interface for future use
- adding "API" method without result
2020-12-22 21:55:47 +01:00
FlorianDahn 46160b54ae Update DataResult.cs
- adding DownloadRawDocument & DownloadRawTextDocument functions
2020-12-22 15:40:18 +01:00
FlorianDahn 87690a3630 Update DeviceSession.cs
Adding a feature for making future API calls.
2020-12-22 15:37:05 +01:00
FlorianDahn 975a942993 Update ButtonGrid.cs
Small refactoring for ButtonGrid
2020-12-06 22:43:39 +01:00
FlorianDahn 4336ff1681 Session Updates 2020-12-04 17:19:11 +01:00
Florian Dahn 567a9f937f Update README.md 2020-11-23 13:51:25 +01:00
FlorianDahn 95b4ff77c5 Update README.md 2020-11-23 13:48:13 +01:00
FlorianDahn ba82c11d66 Update README.md 2020-11-23 13:46:28 +01:00
FlorianDahn c2e33da277 Adding SaveSessionState to SessionBase
Feature request: https://github.com/MajMcCloud/TelegramBotFramework/issues/2
2020-11-22 23:15:31 +01:00
FlorianDahn 259fa54236 Update ModalDialog.cs
- Adding await keyword
2020-11-03 23:52:35 +01:00
FlorianDahn f790a16e85 Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2020-11-03 20:38:40 +01:00
FlorianDahn 97fea333f7 Update ConfirmDialog.cs
- adding additional checks to ConfirmDialog
- adding AutoCloseOnClick property, to close this modal form after button click
2020-11-03 20:38:36 +01:00
FlorianDahn e1b31b0b9a Update PromptDialog.cs
- adding additional checks to promptdialog
2020-11-03 20:38:07 +01:00
Florian Dahn 7aca5a648f Update README.md 2020-10-31 16:22:47 +01:00
FlorianDahn f24c309f63 Update DeviceSession.cs
- adding ParseMode to Edit functions
- adding a Send method which the parameter of the DeviceId
2020-10-22 22:14:40 +02:00
FlorianDahn ba9fd8062c Update PromptDialog.cs
- added ReceivedMessage for further use
2020-10-20 19:11:04 +02:00
FlorianDahn b37c31f2bd Update BotBase.cs
- Bugfix, Unhandled actions has been producing a loop
2020-10-20 19:10:30 +02:00
FlorianDahn 2c34a178ee Update DeviceSession.cs
- adding caption parameter
2020-10-12 23:16:42 +02:00
FlorianDahn 2f030f3c12 Updates to ButtonGrid
- adding IsNavigationBarVisible property to ButtonGrid
- adding SubHeadLayout row for column "descriptions" like a table design
- uses FilterDuplicate with ByRow enabled to keep the full row on matches
2020-10-05 14:24:34 +02:00
FlorianDahn a364562fd3 Update ButtonForm.cs
- Modifying FilterDuplicate to allow full row filter or not
This will keep only the matches, or the full row where it matches.
2020-10-05 14:22:47 +02:00
FlorianDahn 8ef8733a2b Update DeviceSession.cs
- Adding the name parameter to SendPhoto methods
- Adding caption parameter to SendVideo methods
2020-10-05 14:21:57 +02:00
FlorianDahn 049b2081ec Update SessionBase.cs
Use of Fallback form has been missed in Session loading. Added now.
2020-09-15 23:46:30 +02:00
FlorianDahn 48b76a84be Update SessionBase.cs
- improving handling for Sessions loading with no default constructors available.
2020-09-15 16:42:40 +02:00
FlorianDahn 31c8231b2d Adding NavigationBarVisibility property to hide/show navigation buttons auto/never/always 2020-09-15 16:38:36 +02:00
FlorianDahn e9504f3b36 Update ButtonForm.cs
- adding method for adding one single button row
- fix for ToList method if no buttons are available
2020-09-15 16:36:54 +02:00
FlorianDahn 8592f4279d Update ButtonGrid.cs
Fix "Page 1 of 0" when no rows are existing.
2020-09-08 15:32:04 +02:00
FlorianDahn 2457773a12 Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2020-09-05 02:08:28 +02:00
FlorianDahn 46e65faf31 Adding Enum deserialization
Fixing issue in SessionBase where it does not correctly deserialize Enum's
2020-09-05 02:08:19 +02:00
FlorianDahn 193a4811bf Simplifiy AutoCleanForm
Improving serialization by replacing the message with just the ID
2020-09-05 02:07:48 +02:00
Florian Dahn 08a0de8087 Update README.md 2020-09-01 19:00:11 +02:00
FlorianDahn 82469cff70 - Adding not about search feature 2020-08-30 20:48:44 +02:00
Florian Dahn 81eafe1219 Merge pull request #1 from JTOne123/master
[PR] The proj files have been updated to enable SourceLink
2020-08-09 17:25:00 +02:00
FlorianDahn 183e89c36f Adding paging/search example 2020-08-09 17:22:19 +02:00
FlorianDahn ad6cf9539c Adding filter functionality to ButtonGrid
Adding filter functionality
2020-08-09 17:00:04 +02:00
FlorianDahn 46576eb819 Update ButtonForm.cs
adding method to create a ButtonForm duplicate and filter the single buttons, used for ButtonGrid
2020-08-09 16:59:25 +02:00
FlorianDahn cf9e2285f1 Adding DynamicButton for Button with dynamic Text updates
For use, call the ButtonGrid.Updated() method
2020-07-25 15:11:15 +02:00
FlorianDahn ecfacdadbb Update AssemblyInfo.cs 2020-07-25 15:09:01 +02:00
FlorianDahn c00c2e8d8f Update DeviceSession.cs
- adding Unban method
2020-07-25 15:08:55 +02:00
FlorianDahn bfd905d590 Update TelegramBotBase.csproj 2020-07-25 15:08:41 +02:00
FlorianDahn 7b1bf75dde Update ToggleButton
- adding ChangedString variable for custom message
2020-07-25 15:08:22 +02:00
FlorianDahn 7a7a65c3a7 Update FormBase.cs
- adding RemoveAllControls method
2020-07-25 15:07:36 +02:00
Pavlo Datsiuk 8a42a861cf [COMMIT] The proj files have been updated to enable SourceLink [TelegramBotBase.csproj] 2020-06-25 15:54:40 +03:00
FlorianDahn 47876681e0 IgnoreState and SaveState addings for better session serialization
- adding IgnoreState to all ModalDialogs to prevent them get saved.
- adding SaveState to AutoCleanForm to keep state
- change OldMessages property of AutoCleanForm to public
2020-05-07 23:49:08 +02:00
FlorianDahn 9314b4c0e5 Update ButtonGrid.cs
- Fix for async event timings, button click got sometimes fired after Render event
2020-05-06 20:56:38 +02:00
FlorianDahn 01c19b9900 Update README.md 2020-05-04 15:33:50 +02:00
FlorianDahn 0af587d27f Updating readme and test project 2020-05-04 15:33:21 +02:00
FlorianDahn 97b9176c3a Updating packages 2020-05-04 15:22:33 +02:00
FlorianDahn dfa479d409 Fix in StateMachines 2020-05-04 15:18:48 +02:00
FlorianDahn 07dec0dc9d Small fixes and clarifications 2020-05-04 02:20:16 +02:00
FlorianDahn 2fc16b18da DefaultStateForm
- adding DefaultStateForm to redirect Sessions after reloading Sessions to a different Form instead of "restart" the session (i.e. on an in Bot wizard you just want to let the user continue on the main menu)
- adding to SaveSessions to save the "main menu" instead of skipping the ignored form
2020-05-04 02:12:50 +02:00
FlorianDahn 11eeb0dcab Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2020-04-29 22:11:29 +02:00
FlorianDahn fbfcbe99fb Update SystemCommandsBot.csproj
adding NugetPackage instead of direct Project reference
2020-04-29 22:11:26 +02:00
Florian Dahn 948c1368d7 Update README.md 2020-04-28 20:18:24 +02:00
FlorianDahn d66df4978d Create Program.cs 2020-04-28 20:17:18 +02:00
FlorianDahn 7d8895850a Example for a Join/Leave Messages delete Bot 2020-04-28 20:10:18 +02:00
FlorianDahn 5e87d00c58 Localization updates
- small localization updates
- correction in enumration from safe to save
2020-04-21 21:50:59 +02:00
FlorianDahn efe5fef7d0 Adding setting for automated session serialization on shutdown
- adding automated session serialization on application ending (console or shutdown/logoff)
- adding console class for managing system events
2020-04-21 21:45:21 +02:00
FlorianDahn eebb5cb6f5 Create eSettings.cs
- adding eSettings enumeration
2020-04-21 21:44:01 +02:00
FlorianDahn 992065d289 Update BotBase.cs
- improving system settings for bot
2020-04-21 21:43:25 +02:00
FlorianDahn 1acfeb873f Buttongrid Paging support and Localization updates
- adding Paging functionality to ButtonGrid
- adding localization for ButtonGrid
2020-04-21 21:40:47 +02:00
Florian Dahn de35ebb66e Update README.md 2020-04-18 13:16:53 +02:00
FlorianDahn 2c3e707673 Session serialization update
- updating Session serialization for using new method GetChatTitle() to get the chat title for debugging purposes
- removing unncessary use of getting the chat title at every call (improving performance)
2020-04-15 20:30:42 +02:00
FlorianDahn 6d39d2fb73 Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2020-04-15 20:27:54 +02:00
FlorianDahn ef667d12d8 Update DeviceSession.cs
- adding EditReplyMarkup to Device methods
- adding GetChatTitle method to get easier the name of a chat/group/channel
- some cleanup
2020-04-15 20:27:46 +02:00
FlorianDahn d24f8e7ac5 Localizations
- adding localizations in english and german for default controls
- updating default controls
2020-04-15 20:26:39 +02:00
FlorianDahn e5dc3fd2bc Update ButtonForm.cs
- adding a Duplicate function
- improving Count property
- adding InsertButtonRow methods
- adding a dictionary property to get all buttons in a specific row
2020-04-15 20:23:39 +02:00
Florian Dahn 6ebb96bd93 Update README.md 2020-04-15 20:20:57 +02:00
FlorianDahn a1b3b9246b Update BotBase.cs
fixing new command handling
2020-04-11 19:42:24 +02:00
FlorianDahn 25a0ff2e3e Update BotCommandEventArgs.cs
updating documentation
2020-04-11 19:41:56 +02:00
FlorianDahn b74233d66d Update BotBase.cs
Small fix
2020-04-11 19:24:31 +02:00
FlorianDahn fb7b624ad8 BotCommands and refactoring
- refactoring SystemCalls to BotCommand (due new Telegram Bot update)
- Adding UploadBotCommands method to BotBase
- refactoring and simplifying BotBase constructors
- adding getBotCommands and SetBotCommands to MessageClient
2020-04-11 19:12:34 +02:00
Florian Dahn 965e6e18b9 Update README.md 2020-04-10 10:24:29 +02:00
Florian Dahn b1c518cbd3 Update README.md 2020-04-10 10:16:25 +02:00
Florian Dahn ab0ae5cda4 Update README.md 2020-04-10 10:11:20 +02:00
FlorianDahn b850330f65 update readme 2020-04-10 10:09:03 +02:00
FlorianDahn 317dad7d24 - adding state machine documentation 2020-04-10 10:08:02 +02:00
FlorianDahn eb61d40a38 -Projectfile update 2020-04-09 15:32:48 +02:00
FlorianDahn 2ed030b2fb - adding MarkdownV2 escape function
- adding MarkdownV2 autoescape feature
2020-04-09 11:57:45 +02:00
FlorianDahn fd6f6fef34 - moving LoadSessionStates and SaveSessionStates from BotBase to SessionBase 2020-04-09 11:18:23 +02:00
FlorianDahn 54176f9136 Update SessionBase.cs
- adding method to get User Sessions and Group Sessions
2020-04-05 23:47:27 +07:00
FlorianDahn f80fda8ff9 More Attributes for Serialization
- adding IgnoreState Attribute to make it easier to skip forms on session serialization
2020-04-05 23:46:33 +07:00
FlorianDahn 8b66842d34 Update BotBase.cs
- prevent BotBase from crashing on  loading serialized sessions
2020-04-05 23:31:37 +07:00
FlorianDahn 0cdb8c3a1a Adding StateMachine for Session serialization
- adding multiple classes and interfaces for Session Serialization and recovery after restart
2020-04-05 13:13:11 +07:00
FlorianDahn 8b9929198a Modal and Form updates
- adding ReturnFromModal method to FormBase, will get invoked if context leaves a ModalDialog
- ModalDialog contains ParentForm for better navigation
- ModalDialog invokes ReturnFromModal on parentForm after closing
- adding ShowBackButton to PromptDialog for show a back button
2020-04-04 19:52:15 +07:00
FlorianDahn 7a3e4721bd Update DataResult.cs
- adding DownloadAudio
- adding an optional method for all available ones to save attachments directly to a file
2020-04-03 21:54:32 +07:00
FlorianDahn f914920919 Update LinkReplaceTest.cs
- updating LinkReplaceTest to use Message edits, so links will replace in original messages or edited ones
2020-04-02 12:41:26 +07:00
FlorianDahn 0435c68281 Update DeviceSession.cs
DeviceSession: Removing unused ex variable in Exceptions
2020-04-02 12:39:48 +07:00
FlorianDahn 5a1e050aec Update Generator.cs
- Markup Generator: Updating MentionUser
2020-04-02 12:39:26 +07:00
FlorianDahn 6a62d73fe0 Adding Edited Features
- FormBase: adding Edited method
- MessageClient: adding MessageEdit event
- GroupForm: adding Edited method
- DeviceSession: adding ChatTitle
2020-04-02 12:37:53 +07:00
FlorianDahn 519a6fe41c Update ButtonGrid.cs
- ButtonGrid: adding ConfirmationText
- ButtonGrid: adding ParseMode
2020-04-02 12:30:38 +07:00
FlorianDahn 201dfd10dd - updating Testproject to latest changes in Main project 2020-03-29 11:12:34 +07:00
FlorianDahn 78a412c630 - simplify function returns and improving memory use 2020-03-28 10:58:25 +07:00
FlorianDahn 7a7014cec3 - adding SendTextFile to simplify sending csv or textfiles by code without saving them to harddrive 2020-03-28 10:55:32 +07:00
FlorianDahn 64311d0cd9 - adding link to documentation 2020-03-28 10:29:34 +07:00
FlorianDahn 762fb27294 Markdown Fix
- Updating Markdown generator
- adding more options
2020-03-27 12:41:36 +07:00
FlorianDahn 164194ac66 Update
- adding more Constants to Telegram file
- Buttongrid: fixing check for rows and columns
- Buttongrid: replacingvalues with constants from Telegram file
2020-03-27 12:08:34 +07:00
FlorianDahn 321bd7c0ff - adding GetButtonByValue to ButtonForm 2020-03-27 11:03:15 +07:00
FlorianDahn 37ef30ebee - changing OpenModal and NavigateTo to virtual, so you could override it 2020-03-25 18:10:13 +07:00
FlorianDahn 9b1461ad78 Update README.md 2020-03-25 12:45:07 +07:00
FlorianDahn 38fa0a4e99 Update README.md 2020-03-25 12:44:29 +07:00
FlorianDahn fba922049f Updates and fixes
- adding IsFirstHandler to allow controls to skip Actions which has been invoked already (i.e. due to navigation)
- adding Hidden method to ButtonGrid to fix re-rendering Buttons after opening a Modal form and comming back
- improving and fixing the Cleanup method for ButtonGrid to prevent "Keyboard jumping" for ReplyKeyboard on mobile devices.
- renaming some old *Result objects to *EventArgs for more clear definitions
2020-03-25 12:37:16 +07:00
FlorianDahn d5a9a5f7a4 - adding Hidden method to ControlBase which will be called on leaving form or on opening a modal one 2020-03-25 12:27:01 +07:00
FlorianDahn 5126074419 Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2020-03-23 13:06:56 +07:00
FlorianDahn aa38492bde - more refactoring and renaming models *Result to *EventArgs 2020-03-23 13:06:52 +07:00
Florian Dahn 9c68914e9d Delete TelegramBotBase.2.1.0.zip 2020-03-23 13:03:59 +07:00
Florian Dahn 3fe2779b1e Delete TelegramBotBase.2.0.0.zip 2020-03-23 13:03:51 +07:00
FlorianDahn 8e6de4a0e1 - Refactoring and moving all EventArgs structures to folder Args 2020-03-23 13:02:31 +07:00
FlorianDahn 382ae2de2d - adding example project for Bot to start predefined system commands 2020-03-10 01:24:09 +01:00
FlorianDahn 77fd3393de - update spaces 2020-03-10 01:22:52 +01:00
FlorianDahn 0f1da77aca - update on AutoCleanForm for better message deletion on leaving 2020-03-10 01:22:28 +01:00
FlorianDahn 1561c1dd08 -adding HideKeyboardOnCleanup and DeletePreviousMessage on ButtonGrid Control 2020-03-10 01:21:42 +01:00
FlorianDahn 5b38f2e1e2 - adding sendvideo to device for videos or gifs 2019-12-04 01:29:22 +01:00
FlorianDahn 2989456fe5 - change Formbase for better navigation management 2019-12-04 01:28:15 +01:00
FlorianDahn 7e4174a3be Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2019-12-04 01:27:07 +01:00
FlorianDahn 73f81cc8a2 Revert "- adding new exceptions"
This reverts commit 2071ba2eeb.
2019-12-02 03:54:10 +01:00
FlorianDahn 2071ba2eeb - adding new exceptions 2019-12-02 03:48:43 +01:00
FlorianDahn a08ba8f512 -update on FormBase which should prevent issues on control management 2019-12-02 03:46:40 +01:00
Florian Dahn 649b94dafe Delete dotnetcore.yml 2019-11-28 17:17:19 +01:00
FlorianDahn 7be0f15ca4 - updated readme for groups
- adding readme to project solution
2019-11-28 15:41:44 +01:00
FlorianDahn e29be4f1f4 Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2019-11-23 02:44:59 +01:00
FlorianDahn 176107300b - adding group examples for:
link replacing
   group change notifications
   welcoming users
2019-11-23 02:44:50 +01:00
FlorianDahn 0ceab58059 - updating namespaces in test project
- adding chat splitter to testproject to allow single chat and group chat examples at the same time
2019-11-23 02:44:05 +01:00
FlorianDahn 6ebdd9f208 - additional stuff for group management 2019-11-23 02:41:17 +01:00
FlorianDahn 595efd2276 - adding base class for group management 2019-11-23 02:40:51 +01:00
FlorianDahn 85a479d826 - adding useful tasks for groups 2019-11-23 02:40:25 +01:00
FlorianDahn 150e9f731d - addes SplitterForm to switch between "normal" chats and group chats 2019-11-23 02:39:56 +01:00
FlorianDahn 0eab45bdbb - making Events property of FormBase public, for use in sub classes
- making ResultBase derive from Eventargs
- adding messagetype to messageresult
2019-11-21 21:38:20 +01:00
Florian Dahn a33cf5b841 Create dotnetcore.yml 2019-11-14 22:58:50 +01:00
FlorianDahn 1f9539a23d - updating readme 2019-10-11 22:38:50 +02:00
FlorianDahn 9cbde4830e - adding OpenModal/CloseModal features to leave a form open and "just" show a prompt and get back
- added new "ModalDialog" class to present modal forms
- updating examples for prompt dialog to use the modal feature
2019-10-11 22:37:54 +02:00
FlorianDahn 3bea2783b1 - updated Readme 2019-10-11 21:41:02 +02:00
FlorianDahn 038a941d33 - PromptDialog has been changed to ConfirmDialog
- PromptDialog is now (like in JavaScript) for text inputs
- examples corrected
2019-10-11 21:33:33 +02:00
FlorianDahn 48c62e57f8 - moving TelegramBaseTest to TelegramBotBaseTest 2019-09-29 00:47:18 +02:00
FlorianDahn b0e6a8b981 -migrate from .net 4.7.2 to .Net standard 2.0 for the library and .Net core 3.0 for the example project 2019-09-28 14:06:19 +02:00
FlorianDahn 2e0656dc06 - update readme 2019-09-28 01:19:55 +02:00
FlorianDahn d1456d5bd1 - update readme and formbase 2019-09-28 01:17:38 +02:00
FlorianDahn 058539777f - removing obsolete Property and methods
- updating test project
2019-09-27 23:24:31 +02:00
FlorianDahn 5c69289198 - small additions 2019-09-27 23:18:18 +02:00
FlorianDahn 4c8b085655 - small cleanup 2019-09-27 23:14:28 +02:00
FlorianDahn 94d22318f8 - change SetKeyboardType method to Property 2019-09-27 23:08:55 +02:00
FlorianDahn 2028e2f9e6 - adding ButtonGrid control to Readme 2019-09-27 21:26:43 +02:00
FlorianDahn 394209b303 updating both projects to .Net 4.7.2 2019-09-27 00:01:20 +02:00
FlorianDahn f0fa953819 Updating csproj Files 2019-09-26 23:27:38 +02:00
FlorianDahn 1d44b50883 - small change 2019-09-26 20:57:51 +02:00
FlorianDahn 3c21af8f68 - small change and adding comments 2019-09-26 20:57:16 +02:00
FlorianDahn 1b811e1786 - added ToKeyboardButton to ButtonBase
- added eKeyboardType to switch between Inline and KeykboardButtons
- addes some small functions to ButtonForm
- added new control "ButtonGrid" which manages Button handling with Event Handler for each Button and can switch between InlineMode and KeyboardMode during runtime
- added also Examples to it
2019-09-26 20:54:35 +02:00
FlorianDahn 94195ec5bf - added another Edit Method for InlineKeyboardMarkup
- change Send method parameter ReplyKeyboardMarkup to ReplyMarkupBase to allow also RemoveKeyboard
2019-09-26 20:51:30 +02:00
FlorianDahn 256cac825f - adding Load method for controls 2019-09-26 20:50:33 +02:00
FlorianDahn 69e5a6717b - Splitting enums into single files for more structure
- remove old Enum file
2019-09-26 17:14:12 +02:00
FlorianDahn a6d22cdd82 Update version 2019-09-17 16:44:14 +02:00
FlorianDahn ee4a3d765c Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2019-09-17 16:43:10 +02:00
FlorianDahn 703c99eb8d - added a custom exception for maximum length messages, saves to time to sent to API
- added a constants file, for have constants to work with
- catching maximum message length
2019-09-17 16:43:02 +02:00
Florian Dahn 138fee9772 Update README.md
adding paypal
2019-09-17 16:33:51 +02:00
Florian Dahn d60399514f Update README.md 2019-09-04 15:13:42 +02:00
FlorianDahn ae0fa3b0e3 Added simple Markdown/HTML Generator
Use with:

"Bold Text".Bold();

"Other text".Italic();

"www.google.de".Link("Google")
2019-08-28 17:33:18 +02:00
FlorianDahn 6a2ac7a309 Added simple Markdown/HTML Generator
Use with:

"Bold Text".Bold();

"Other text".Italic();

"www.google.de".AsLink("Google")
2019-08-28 17:32:08 +02:00
FlorianDahn 1a76b2b3f8 added some forms to obsolete, cause their use is bad for ram management 2019-08-26 01:34:50 +02:00
FlorianDahn 5893108bd3 Update readme 2019-08-23 14:55:54 +02:00
FlorianDahn d668f57711 Update readme 2019-08-23 14:53:24 +02:00
FlorianDahn 56c7754408 - Added ToggleButton as an easy switch control (i.e. for settings page)
- added control id, for easier separating of controls in one form
- added automatic event selection for specific controls, to not raise an event for other controls who has invoked it
- changed Action method
- added example
2019-08-23 14:02:24 +02:00
FlorianDahn 805ffb9ec7 Update version 2019-07-15 13:33:01 +02:00
FlorianDahn aeddd82ea9 Merge branch 'master' of https://github.com/MajMcCloud/TelegramBotFramework 2019-07-15 13:31:07 +02:00
FlorianDahn 1ec14ce680 Updating nuget packages 2019-07-15 13:31:02 +02:00
182 changed files with 12026 additions and 1240 deletions
+2
View File
@@ -0,0 +1,2 @@
github: #[MajMcCloud]
custom: ["https://paypal.me/majmccloud", dahn.partners]
+12
View File
@@ -240,3 +240,15 @@ ModelManifest.xml
# FAKE - F# Make
.fake/
MigrationBackup
TelegramBotBase/nuget.exe
Material
TelegramBotBase/cp.ps1
TelegramBotBase/TelegramBotBase.nuspec
Program.cs
TelegramBotBase/Archive
/TelegramBotBase/TelegramBotBase.1.5.0.zip
/TelegramBotBase/TelegramBotBase.1.5.1.zip
/TelegramBotBase/TelegramBotBase.1.5.2.zip
/TelegramBotBase/cpush.ps1
/TelegramBotBase/cpack.ps1
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{5817184C-0D59-4924-AC6C-6B943967811C}</ProjectGuid>
<ProjectGuid>{673A56F5-6110-4AED-A68D-562FD6ED3EA6}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TelegramBaseTest</RootNamespace>
<AssemblyName>TelegramBaseTest</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<RootNamespace>AsyncFormUpdates</RootNamespace>
<AssemblyName>AsyncFormUpdates</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -33,61 +33,37 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="Telegram.Bot, Version=14.10.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Telegram.Bot.14.10.0\lib\net45\Telegram.Bot.dll</HintPath>
<Reference Include="Telegram.Bot, Version=15.7.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\Telegram.Bot.15.7.1\lib\net45\Telegram.Bot.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="forms\AsyncFormEdit.cs" />
<Compile Include="forms\AsyncFormUpdate.cs" />
<Compile Include="forms\Start.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tests\ButtonTestForm.cs" />
<Compile Include="Tests\DataForm.cs" />
<Compile Include="Tests\ProgressTest.cs" />
<Compile Include="Tests\Register\PerForm.cs" />
<Compile Include="Tests\Register\PerStep.cs" />
<Compile Include="Tests\Register\Start.cs" />
<Compile Include="Tests\Register\Steps\Data.cs" />
<Compile Include="Tests\Register\Steps\Step3.cs" />
<Compile Include="Tests\Register\Steps\Step2.cs" />
<Compile Include="Tests\Register\Steps\Step1.cs" />
<Compile Include="Tests\SimpleForm.cs" />
<Compile Include="Tests\Start.cs" />
<Compile Include="Tests\TestForm.cs" />
<Compile Include="Tests\TestForm2.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TelegramBotBase\TelegramBotBase.csproj">
<Project>{a61eda27-cfdf-4bcb-b2d6-184f94904f2d}</Project>
<ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj">
<Project>{0bd16fb9-7ed4-4ccb-83eb-5cee538e1b6c}</Project>
<Name>TelegramBotBase</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
+48
View File
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace AsyncFormUpdates
{
class Program
{
static TelegramBotBase.BotBase<forms.Start> bot = null;
static void Main(string[] args)
{
String apiKey = "APIKey";
bot = new TelegramBotBase.BotBase<forms.Start>(apiKey);
bot.Start();
var timer = new Timer(5000);
timer.Elapsed += Timer_Elapsed;
timer.Start();
Console.ReadLine();
timer.Stop();
bot.Stop();
}
private static async void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
foreach(var s in bot.Sessions.SessionList)
{
//Only for AsyncUpdateForm
if (s.Value.ActiveForm.GetType() != typeof(forms.AsyncFormUpdate) && s.Value.ActiveForm.GetType() != typeof(forms.AsyncFormEdit))
continue;
await bot.InvokeMessageLoop(s.Key);
}
}
}
}
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die einer Assembly zugeordnet sind.
[assembly: AssemblyTitle("AsyncFormUpdates")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AsyncFormUpdates")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly
// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von
// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen.
[assembly: ComVisible(false)]
// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
[assembly: Guid("673a56f5-6110-4aed-a68d-562fd6ed3ea6")]
// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
//
// Hauptversion
// Nebenversion
// Buildnummer
// Revision
//
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Attributes;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace AsyncFormUpdates.forms
{
public class AsyncFormEdit : FormBase
{
[SaveState]
int counter = 0;
int MessageId = 0;
public override async Task Load(MessageResult message)
{
counter++;
}
public override async Task Action(MessageResult message)
{
await message.ConfirmAction("");
switch (message.RawData ?? "")
{
case "back":
var st = new Start();
await NavigateTo(st);
break;
}
}
public override async Task Render(MessageResult message)
{
var bf = new ButtonForm();
bf.AddButtonRow("Back", "back");
if (MessageId != 0)
{
await Device.Edit(MessageId, $"Your current count is at: {counter}", bf);
}
else
{
var m = await Device.Send($"Your current count is at: {counter}", bf, disableNotification: true);
MessageId = m.MessageId;
}
}
}
}
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Attributes;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace AsyncFormUpdates.forms
{
public class AsyncFormUpdate : AutoCleanForm
{
[SaveState]
int counter = 0;
public override async Task Load(MessageResult message)
{
counter++;
}
public override async Task Action(MessageResult message)
{
await message.ConfirmAction("");
switch (message.RawData ?? "")
{
case "back":
var st = new Start();
await NavigateTo(st);
break;
}
}
public override async Task Render(MessageResult message)
{
var bf = new ButtonForm();
bf.AddButtonRow("Back", "back");
await Device.Send($"Your current count is at: {counter}", bf, disableNotification: true);
}
}
}
+52
View File
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace AsyncFormUpdates.forms
{
public class Start : AutoCleanForm
{
public override async Task Action(MessageResult message)
{
await message.ConfirmAction("");
switch (message.RawData ?? "")
{
case "async":
var afe = new AsyncFormEdit();
await NavigateTo(afe);
break;
case "async_del":
var afu = new AsyncFormUpdate();
await NavigateTo(afu);
break;
}
}
public override async Task Render(MessageResult message)
{
var bf = new ButtonForm();
bf.AddButtonRow("Open Async Form with AutoCleanupForm", "async_del");
bf.AddButtonRow("Open Async Form with Edit", "async");
await Device.Send("Choose your option", bf);
}
}
}
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net472" />
<package id="System.Net.Requests" version="4.3.0" targetFramework="net472" />
<package id="Telegram.Bot" version="15.7.1" targetFramework="net472" />
</packages>
+12
View File
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="TelegramBotBase" Version="3.1.1" />
</ItemGroup>
</Project>
+19
View File
@@ -0,0 +1,19 @@
using System;
namespace JoinHiderBot
{
class Program
{
static void Main(string[] args)
{
String apiKey = "";
var bot = new TelegramBotBase.BotBase<forms.Start>(apiKey);
bot.Start();
Console.ReadLine();
}
}
}
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Args;
using TelegramBotBase.Form;
namespace JoinHiderBot.forms
{
public class GroupManageForm : GroupForm
{
public override async Task OnMemberChanges(MemberChangeEventArgs e)
{
if (e.Type != Telegram.Bot.Types.Enums.MessageType.ChatMembersAdded && e.Type != Telegram.Bot.Types.Enums.MessageType.ChatMemberLeft)
return;
var m = e.Result.Message;
await this.Device.DeleteMessage(m);
}
}
}
+29
View File
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace JoinHiderBot.forms
{
public class Start : SplitterForm
{
public override async Task<bool> Open(MessageResult e)
{
await this.Device.Send("This bot works only in groups.");
return true;
}
public override async Task<bool> OpenGroup(MessageResult e)
{
var gmf = new GroupManageForm();
await this.NavigateTo(gmf);
return true;
}
}
}
+36
View File
@@ -0,0 +1,36 @@
using System;
namespace SystemCommandsBot
{
class Program
{
public static config.Config BotConfig { get; set; }
static void Main(string[] args)
{
BotConfig = config.Config.load();
if (BotConfig.ApiKey == null || BotConfig.ApiKey.Trim() == "")
{
Console.WriteLine("Config created...");
Console.ReadLine();
return;
}
var bot = new TelegramBotBase.BotBase<forms.StartForm>(BotConfig.ApiKey);
bot.Start();
Console.WriteLine("Bot started");
Console.ReadLine();
bot.Stop();
}
}
}
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="TelegramBotBase" Version="3.1.1" />
</ItemGroup>
</Project>
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace SystemCommandsBot.commands
{
public class Commando
{
public int ID { get; set; }
public String Title { get; set; }
public String ShellCmd { get; set; }
public bool Enabled { get; set; } = true;
public String Action { get; set; }
public bool UseShell { get; set; } = true;
public int? MaxInstances { get; set; }
public String ProcName
{
get;set;
}
}
}
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace SystemCommandsBot.config
{
public class Config
{
public String Password { get; set; }
public String ApiKey { get; set; }
public List<commands.Commando> Commandos { get; set; }
public Config()
{
this.Commandos = new List<commands.Commando>();
}
public void loadDefaultValues()
{
this.ApiKey = "";
this.Commandos.Add(new commands.Commando() { ID = 0, Enabled = true, Title = "Test Befehl", ShellCmd = "explorer.exe", Action = "start", MaxInstances = 2 });
}
public static Config load()
{
try
{
return load(AppContext.BaseDirectory + "config\\default.cfg");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
public static Config load(String path)
{
try
{
var cfg = Newtonsoft.Json.JsonConvert.DeserializeObject<Config>(File.ReadAllText(path)) as Config;
return cfg;
}
catch (DirectoryNotFoundException ex)
{
DirectoryInfo di = new DirectoryInfo(path);
if (!Directory.Exists(di.Parent.FullName))
{
Directory.CreateDirectory(di.Parent.FullName);
}
var cfg = new Config();
cfg.loadDefaultValues();
cfg.save(path);
return cfg;
}
catch (FileNotFoundException ex)
{
var cfg = new Config();
cfg.loadDefaultValues();
cfg.save(path);
return cfg;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
return null;
}
public void save(String path)
{
try
{
File.WriteAllText(path, Newtonsoft.Json.JsonConvert.SerializeObject(this));
}
catch
{
}
}
}
}
+172
View File
@@ -0,0 +1,172 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace SystemCommandsBot.forms
{
public class CmdForm : TelegramBotBase.Form.AutoCleanForm
{
public DateTime ExpiresAt { get; set; }
public int? MessageId { get; set; }
public override async Task Load(MessageResult message)
{
}
public override async Task Action(MessageResult message)
{
var btn = message.RawData;
int id = -1;
if (!int.TryParse(btn, out id))
{
return;
}
var cmd = Program.BotConfig.Commandos.Where(a => a.Enabled && a.ID == id).FirstOrDefault();
if (cmd == null)
{
await this.Device.Send("Cmd nicht verfügbar.");
return;
}
message.Handled = true;
switch (cmd.Action)
{
case "start":
FileInfo fi = new FileInfo(cmd.ShellCmd);
if (cmd.MaxInstances != null && cmd.MaxInstances >= 0)
{
if (Process.GetProcessesByName(cmd.ProcName).Count() >= cmd.MaxInstances)
{
await this.Device.Send("Anwendung läuft bereits.");
await message.ConfirmAction("Anwendung läuft bereits.");
return;
}
}
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = cmd.ShellCmd;
psi.WorkingDirectory = fi.DirectoryName;
psi.UseShellExecute = cmd.UseShell;
Process.Start(psi);
await this.Device.Send(fi.Name + " wurde gestarted.");
await message.ConfirmAction(fi.Name + " wurde gestarted.");
break;
case "kill":
FileInfo fi2 = new FileInfo(cmd.ShellCmd);
String pros = fi2.Name.Replace(fi2.Extension, "");
var proc = Process.GetProcessesByName(pros).ToList();
foreach (var p in proc)
{
try
{
p.Kill();
}
catch
{
}
}
await this.Device.Send(fi2.Name + " wurde beendet.");
await message.ConfirmAction(fi2.Name + " wurde beendet.");
break;
case "restart":
FileInfo fi3 = new FileInfo(cmd.ShellCmd);
String pros2 = fi3.Name.Replace(fi3.Extension, "");
var proc2 = Process.GetProcessesByName(pros2).ToList();
foreach (var p in proc2)
{
try
{
p.Kill();
}
catch
{
}
}
FileInfo fi4 = new FileInfo(cmd.ShellCmd);
ProcessStartInfo psi2 = new ProcessStartInfo();
psi2.FileName = cmd.ShellCmd;
psi2.WorkingDirectory = fi4.DirectoryName;
psi2.FileName = cmd.ShellCmd;
psi2.UseShellExecute = cmd.UseShell;
Process.Start(psi2);
await this.Device.Send(fi3.Name + " wurde neugestarted.");
await message.ConfirmAction(fi3.Name + " wurde neugestarted.");
break;
default:
await message.ConfirmAction();
break;
}
}
public override async Task Render(MessageResult message)
{
if (this.MessageId == null)
{
var buttons = Program.BotConfig.Commandos.Where(a => a.Enabled).Select(a => new ButtonBase(a.Title, a.ID.ToString()));
ButtonForm bf = new ButtonForm();
bf.AddSplitted(buttons, 1);
await this.Device.Send("Deine Optionen", bf);
return;
}
}
}
}
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
namespace SystemCommandsBot.forms
{
public class StartForm : TelegramBotBase.Form.FormBase
{
public String Password { get; set; }
public override async Task Load(MessageResult message)
{
var inp = message.MessageText;
if (Program.BotConfig.Password == inp)
{
this.Password = inp;
}
}
public override async Task Render(MessageResult message)
{
if (this.Password == null || this.Password.Trim() == "")
{
await this.Device.Send("Bitte gib dein Passwort an.");
return;
}
var cmd = new forms.CmdForm();
cmd.ExpiresAt = DateTime.Now.AddDays(14);
await this.NavigateTo(cmd);
}
}
}
+558 -48
View File
@@ -17,9 +17,29 @@ Download a release: [Releases](https://github.com/MajMcCloud/TelegramBotFramewor
---
Donations
Bitcoin: 1GoUJYMwAvBipQTfw2FKydAz12J8RDyeJs / bc1qqwlp0p5ley29lsu6jhe0qv7s7963kfc7d0m53d
ETH: 0xAf3835104c2C3E5b3e721FA2c7365955e87DB931
Litecoin: LRhF1eB7kneFontcDRDU8YjJhEm2GoYHch
DASH: XudiUwWtSmAJj1QDdVW7jocQumJFLsyoGZ
TRON: TYVZSykaVT1nKZnz9hjDgBRNB9VavU1bpW
BITTORRENT: TYVZSykaVT1nKZnz9hjDgBRNB9VavU1bpW
Thanks !
---
## Index
- [Introduction](#introduction)
- [How to Start](#how-to-start)
- [Quick Start](#quick-start)
- [Message Handling](#message-handling)
* [Example #0 - System Calls](#add-some-system-calls-example-0---system-calls)
@@ -39,6 +59,8 @@ Download a release: [Releases](https://github.com/MajMcCloud/TelegramBotFramewor
* [PromptDialog](#prompt-dialog)
* [ConfirmDialog](#confirm-dialog)
- [Controls](#controls)
* [ProgressBar](#progress-bar)
@@ -46,7 +68,58 @@ Download a release: [Releases](https://github.com/MajMcCloud/TelegramBotFramewor
* [MonthPicker](#month-picker)
+ [TreeView](#tree-view)
* [TreeView](#tree-view)
* [ToggleButton](#toggle-button)
* [ButtonGrid](#button-grid)
* [Paging and Searching](#paging--searching)
* [TaggedButtonGrid](#tagged-button-grid)
* [CheckedButtonList](#checked-button-list)
* [MultiToggleButton](#multi-toggle-button)
- [Groups](#groups)
* [SplitterForm](#splitter-form)
* [GroupForm](#group-form)
- [State Machine and Session Serialization (v3.0.0)](#statemachine-and-sessions)
* [StateMachines](#statemachines)
* [SimpleJSONStateMachine](#simplejsonstatemachine)
* [JSONStateMachine](#jsonstatemachine)
* [XMLStateMachine](#xmlstatemachine)
* [Interfaces](#interfaces)
* [IStateMachine](#istatemachine)
* [IStateForm](#istateform)
* [Attributes](#attributes)
* [SaveState](#savestate)
* [IgnoreState](#ignorestate)
- [Navigation and NavigationController (v4.0.0)](#navigation-and-navigationcontroller)
* [As of Now](#as-of-now)
* [How to use](#how-to-use-)
- [Examples](#examples)
---
@@ -66,18 +139,31 @@ It gives you features which will look/feel like WinForms or have a good way to c
## How to start:
Within your empty App your need to put some initial lines including your APIKey to get things started.
The "BotBase" Class will manage a lot of things for you, like system calls, action events and so on.
The "BotBase" Class will manage a lot of things for you, like bot commands, action events and so on.
"StartForm" is your first Formular which every user will get internally redirected to, like a start page, you could redirect from there later in code, so users won't recognize it.
It needs to be a subclass of "FormBase" you will find in Namespace TelegramBotBase.Base
```
//Prepare the System
BotBase<StartForm> bb = new BotBase<StartForm>("{YOUR API KEY}");
//Prepare the System (New in V5)
var bb = BotBaseBuilder
.Create()
.WithAPIKey("{YOUR API KEY}")
.DefaultMessageLoop()
.WithStartForm<StartForm>()
.NoProxy()
.CustomCommands(a =>
{
a.Start("Starts the bot")
//Add Systemcommands if you like, you could catch them later
bb.SystemCalls.Add("/start");
})
.NoSerialization()
.UseEnglish()
.Build();
//Update bot commands to botfather
bb.UploadBotCommands().Wait();
//Start your Bot
bb.Start();
@@ -97,22 +183,13 @@ public class StartForm : FormBase
}
//Gets invoked during Navigation to this form
public override async Task Init(params object[] param)
{
}
//Init() got replaced with event handler
//Gets invoked if the form gets opened (with Form.NavigateTo(NewForm))
public override async Task Opened()
{
//Opened() got replaced with event handler
}
//Closed() got replaced with event handler
//Gets invoked if the form gets leaved
public override async Task Closed()
{
}
//Gets invoked on every Message/Action/Data in this context
public override async Task Load(MessageResult message)
@@ -120,6 +197,12 @@ public class StartForm : FormBase
await this.Device.Send("Hello world!");
}
//Gets invoked on edited messages
public override async Task Edited(MessageResult message)
{
}
//Gets invoked on Button clicks
public override async Task Action(MessageResult message)
{
@@ -159,14 +242,36 @@ var tf = new TestForm();
await this.NavigateTo(tf);
```
## Quick Start:
When migrating from a previous version or starting completely new, all these options can be a bit overwhelming.
For this I added a QuickStart option, directly after the Create call. It just need basic parameters like in earlier versions.
```
//Prepare the System (New in V5)
var bb = BotBaseBuilder
.Create()
.QuickStart<StartForm>("{YOUR API KEY}")
.Build();
//Start your Bot
bb.Start();
```
## Message Handling
All examples are within the test project, so just try it out on your own.
### Add some system calls (Example #0 - System Calls)
### Add some system calls (Example #0 - Bot Commands)
Inside of the BotFather you are able to add "Commands" to your TelegramBot. The user will see them, depending on the application as options he could choose.
I'm calling them Systemcalls. Before start (and later for sure) you could add them to your BotBase. Every time a message comes in they will get checked if they are one of them.
Before start (and later for sure) you could add them to your BotBase. Every time a message comes in they will get checked if they are one of them.
If so, a special event Handler will get raised where you are easier able to manage the action behind.
Below we have 4 options.
@@ -182,15 +287,26 @@ Below we have 4 options.
```
BotBase<Start> bb = new BotBase<Start>("{YOUR API KEY}");
var bb = BotBaseBuilder
.Create()
.WithAPIKey("{YOUR API KEY}")
.DefaultMessageLoop()
.WithStartForm<Start>()
.NoProxy()
.CustomCommands(a =>
{
a.Start("Starts the bot");
a.Add("form1","Opens test form 1" );
a.Add("form2", "Opens test form 2" );
a.Add("params", "Returns all send parameters as a message." );
bb.SystemCalls.Add("/start");
bb.SystemCalls.Add("/form1");
bb.SystemCalls.Add("/form2");
bb.SystemCalls.Add("/params");
})
.NoSerialization()
.UseEnglish()
.Build();
bb.SystemCall += async (s, en) =>
bb.BotCommand += async (s, en) =>
{
switch (en.Command)
{
@@ -221,6 +337,9 @@ bb.SystemCall += async (s, en) =>
};
//Update Bot commands to botfather
bb.UploadBotCommands().Wait();
bb.Start();
```
@@ -237,11 +356,13 @@ public class SimpleForm : AutoCleanForm
{
public SimpleForm()
{
this.DeleteSide = eSide.Both;
this.DeleteMode = eDeleteMode.OnLeavingForm;
this.DeleteSide = TelegramBotBase.Enums.eDeleteSide.Both;
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
this.Opened += SimpleForm_Opened;
}
public override async Task Opened()
private async Task SimpleForm_Opened(object sender, EventArgs e)
{
await this.Device.Send("Hello world! (send 'back' to get back to Start)\r\nOr\r\nhi, hello, maybe, bye and ciao");
}
@@ -622,7 +743,7 @@ public async override Task Render(MessageResult message)
```
There is also a second example, where every of these 3 inputs gets requested by a different formular (class). Just for imagination of the possiblites.
Cause its to much Text, i didnt have added it here. You will find it under [TelegramBaseTest/Tests/Register/PerStep.cs](TelegramBaseTest/Tests/Register/PerStep.cs)
Cause its to much Text, i didnt have added it here. You will find it under [TelegramBotBaseTest/Tests/Register/PerStep.cs](TelegramBotBaseTest/Tests/Register/PerStep.cs)
Beginn there and navigate your way through these Steps in the subfolder.
@@ -642,6 +763,9 @@ For now we have the following:
Makes sense to create a "feeling" of a clean environment for the user. For instance if you have a multilevel menu. This will remove the previously shown menu, and renders the new sub/top level.
- [PromptDialog](#prompt-dialog)
A simple dialog which will show a message and then waits for a text input (response).
- [ConfirmDialog](#confirm-dialog)
A simple dialog which is able to show multiple buttons and a Text message. The user could select one option and will get redirected to a different form, depending on the click.
### Alert Dialog
@@ -650,9 +774,13 @@ For now we have the following:
```
var fto = new TestForm2();
AlertDialog ad = new AlertDialog("This is a message", "Ok");
AlertDialog ad = new AlertDialog("This is a message", "Ok", fto);
ad.ButtonClicked += async (s, en) =>
{
var fto = new TestForm2();
await this.NavigateTo(fto);
};
await this.NavigateTo(ad);
@@ -667,45 +795,44 @@ No example yet
<img src="images/promptdialog.PNG" />
#### Without Eventhandler (pre-init of form necessary)
```
PromptDialog pd = new PromptDialog("Please confirm");
PromptDialog pd = new PromptDialog("Please tell me your name ?");
pd.AddButton(new ButtonBase("Ok", "ok"));
pd.AddButton(new ButtonBase("Cancel", "cancel"));
pd.Completed += async (s, en) =>
{
await this.Device.Send("Hello " + pd.Value);
};
var tf = new TestForm2();
pd.ButtonForms.Add("ok", tf);
pd.ButtonForms.Add("cancel", tf);
await this.NavigateTo(pd);
await this.OpenModal(pd);
```
### Confirm Dialog
<img src="images/confirmdialog.PNG" />
#### With Eventhandler (no pre-init of form necessary)
```
PromptDialog pd = new PromptDialog("Please confirm", new ButtonBase("Ok", "ok"), new ButtonBase("Cancel", "cancel"));
ConfirmDialog cd = new ConfirmDialog("Please confirm", new ButtonBase("Ok", "ok"), new ButtonBase("Cancel", "cancel"));
pd.ButtonClicked += async (s, en) =>
cd.ButtonClicked += async (s, en) =>
{
var tf = new TestForm2();
//Remember only to navigate from the current running form. (here it is the prompt dialog, cause we have left the above already)
await pd.NavigateTo(tf);
await cd.NavigateTo(tf);
};
await this.NavigateTo(pd);
await this.NavigateTo(cd);
```
## Controls
### Progress Bar
@@ -715,6 +842,7 @@ await this.NavigateTo(pd);
### Calendar Picker
<img src="images/calendarpicker.PNG?raw=true" />
<img src="images/calendarpicker.gif?raw=true" />
### Month Picker
<img src="images/monthpicker1.PNG?raw=true" />
@@ -728,6 +856,388 @@ await this.NavigateTo(pd);
<img src="images/treeview4.PNG?raw=true" />
### Toggle Button
<img src="images/togglebutton.gif?raw=true" />
### Button Grid
<img src="images/buttongrid.gif?raw=true" />
#### Paging & Searching
<img src="images/buttongrid_pagingfilter.gif?raw=true" />
### Tagged Button Grid
<img src="images/taggedbuttongrid.gif?raw=true" />
### Checked Button List
<img src="images/checkedbuttonlist.gif?raw=true" />
### Multi Toggle Button
<img src="images/multitogglebutton.gif?raw=true" />
## Groups
For working with groups, there are multiple different tools which helps to work with and allows bot also to manage "Single-User" chats and group chats.
### Splitter Form
An easy way to switch between a "Single-User" form and one for managing a group is the SplitterForm base class.
It calls special methods which you can override and then move from there to the form you need.
The OpenGroup method is the "backup" if OpenChannel or OpenSupergroup is not overridden. Same for Open, it is "backup" if none of the previous methods has been overridden.
```
public class Start : SplitterForm
{
public override async Task<bool> Open(MessageResult e)
{
var st = new Menu();
await this.NavigateTo(st);
return true;
}
public override async Task<bool> OpenGroup(MessageResult e)
{
var st = new Groups.LinkReplaceTest();
await this.NavigateTo(st);
return true;
}
public override Task<bool> OpenChannel(MessageResult e)
{
return base.OpenChannel(e);
}
public override Task<bool> OpenSupergroup(MessageResult e)
{
return base.OpenSupergroup(e);
}
}
```
### Group Form
For managing groups im introducing a new base class called "GroupForm". This one has special events which should make it easier to work with groups and channels.
In the Example project is a simple example for deleting a url written by a user and incrementing an internal counter. At every url he writes he got blocked for a small amount of time and the messages gots deleted. At 3 "failes" the user gets kicked of the group and blocked.
```
public class GroupForm : FormBase
{
public override async Task Load(MessageResult message)
{
switch (message.MessageType)
{
case Telegram.Bot.Types.Enums.MessageType.ChatMembersAdded:
await OnMemberChanges(new MemberChangeEventArgs(Telegram.Bot.Types.Enums.MessageType.ChatMembersAdded, message, message.RawMessageData.Message.NewChatMembers));
break;
case Telegram.Bot.Types.Enums.MessageType.ChatMemberLeft:
await OnMemberChanges(new MemberChangeEventArgs(Telegram.Bot.Types.Enums.MessageType.ChatMemberLeft, message, message.RawMessageData.Message.LeftChatMember));
break;
case Telegram.Bot.Types.Enums.MessageType.ChatPhotoChanged:
case Telegram.Bot.Types.Enums.MessageType.ChatPhotoDeleted:
case Telegram.Bot.Types.Enums.MessageType.ChatTitleChanged:
case Telegram.Bot.Types.Enums.MessageType.MigratedFromGroup:
case Telegram.Bot.Types.Enums.MessageType.MigratedToSupergroup:
case Telegram.Bot.Types.Enums.MessageType.MessagePinned:
case Telegram.Bot.Types.Enums.MessageType.GroupCreated:
case Telegram.Bot.Types.Enums.MessageType.SupergroupCreated:
case Telegram.Bot.Types.Enums.MessageType.ChannelCreated:
await OnGroupChanged(new GroupChangedEventArgs(message.MessageType, message));
break;
default:
OnMessage(message);
break;
}
}
public virtual async Task OnMemberChanges(MemberChangeEventArgs e)
{
}
public virtual async Task OnGroupChanged(GroupChangedEventArgs e)
{
}
public virtual async Task OnMessage(MessageResult e)
{
}
}
```
## Statemachine and Sessions
Depending on the usecases and the overall structure of a Telegram Bot it is essential to have some kind of session serialization or state machine to keep the user context after restarts of the bot (ie. due to updates) or crashes.
For this I have created some easy to implement structures which fits into the current environment.
Below you find all possiblities.
### Statemachines
There are actually 3 types of example state machines you could use. A state machine is a kind of serializer which saves the important session data in a reusable structure like JSON or XML.
You could use one of the following state machines:
#### SimpleJSONStateMachine
Is easy to use and useful for simple structures like basic datatypes. Did not work for complex ones like generics. Use the JSONStateMachine for them.
In general you didn't need to do more then, to keep the actual form:
```
//Prepare the System
var bb = BotBaseBuilder
.Create()
.WithAPIKey("{YOUR API KEY}")
.DefaultMessageLoop()
.WithStartForm<StartForm>()
.NoProxy()
.CustomCommands(a =>
{
a.Start("Starts the bot");
})
.UseSimpleJSON(AppContext.BaseDirectory + "config\\states.json")
.UseEnglish()
.Build();
//Start your Bot
bb.Start();
```
#### JSONStateMachine
Is easy to use too, but works for complex datatypes cause it saves there namespaces and additional type informations into the JSON file too.
In general you didn't need to do more then, to keep the actual form:
```
//Prepare the System
var bb = BotBaseBuilder
.Create()
.WithAPIKey("{YOUR API KEY}")
.DefaultMessageLoop()
.WithStartForm<StartForm>()
.NoProxy()
.CustomCommands(a =>
{
a.Start("Starts the bot");
})
.UseJSON(AppContext.BaseDirectory + "config\\states.json")
.UseEnglish()
.Build();
//Start your Bot
bb.Start();
```
#### XMLStateMachine
The last one, should work like the others.
In general you didn't need to do more then, to keep the actual form:
```
//Prepare the System
var bb = BotBaseBuilder
.Create()
.WithAPIKey("{YOUR API KEY}")
.DefaultMessageLoop()
.WithStartForm<StartForm>()
.NoProxy()
.CustomCommands(a =>
{
a.Start("Starts the bot");
})
.UseXML(AppContext.BaseDirectory + "config\\states.xml")
.UseEnglish()
.Build();
//Start your Bot
bb.Start();
```
### Interfaces
There are two interfaces, one for the StateMachine itself, which is useful to build a custom one for a different datatype and one for implementing into a form which should be invoked with events.
#### IStateMachine
Is the basic StateMachine interface, it has two methods SaveFormStates(SaveStatesEventArgs e) and StateContainer LoadFormStates(), nothing fancy, just simple calls. Implement into both methods your own serialization process.
```
public interface IStateMachine
{
void SaveFormStates(SaveStatesEventArgs e);
StateContainer LoadFormStates();
}
```
#### IStateForm
When implemented, this will invoke one of these two methods: LoadState(LoadStateEventArgs e) or SaveState(SaveStateEventArgs e).
They have methods to load or save data from the statemachine of the current form.
```
public interface IStateForm
{
void LoadState(LoadStateEventArgs e);
void SaveState(SaveStateEventArgs e);
}
```
### Attributes
If you don't want to implement the IStateForm interface, cause there are maybe "just" one or two properties you want to keep and restore, use the following attribute:
#### SaveState
This will let the engine know, that you want too keep and restore this field automatically. Unlike the IStateForm methods, you have no option to manipulate data.
```
[SaveState]
public long UserId { get; set; }
```
#### IgnoreState
Due to the fact that Attribute implementation and interaction is optional, you want to let the engine maybe know, that you don't want to keep a specific form. So it should get "lost". This attribute will help you here, add it to the form class and it will not get serialized, even if it implements IStateForm or the SaveState attributes.
```
[IgnoreState]
public class Registration : STForm
{
}
```
## Navigation and NavigationController
### As of now
As from earlier topics on this readme you already know the default way for (cross) navigation between Forms.
It will look something like this:
```
var f = new FormBase();
await this.NavigateTo(f);
```
Depending on the model and structure of your bot it can make sense, to have more linear navigation instead of "cross" navigation.
In example you have a bot which shows a list of football teams. And when clicking on it you want to open the team details and latest matches.
After the matches you want to maybe switch to a different teams and take a look at their statistics and matches.
At some point, you "just" want to get back to the first team so like on Android your clicking the "back" button multiple times.
This can become really complicated, when not having some controller below which handle these "Push/Pop" calls.
Thats why I introduced a NavigationController class which manages these situations and the stack.
### How to use ?
First you need to create a NavigationController instance at the same position in code, where you want to start the navigation.
You will use the current FormBase instance as a root class within the constructor. So you can later come back to this one.
**Tip**: *You can add also a completely new instance of i.e. a main menu form here to get back to it then. So you are free to choose.*
We are using the same FormBase instance as above.
```
var nc = new NavigationController(this);
var f = new FormBase();
//Replace the current form in the context with the controller.
await this.NavigateTo(nc);
//Push the new from onto the stack to render it
nc.PushAsync(f);
```
Later to open a new form use PushAsync again:
```
await this.NavigationController.PushAsync(newForm);
```
When you want to go back one Form on the stack use PopAsync:
```
await this.NavigationController.PopAsync();
```
**Notice**: *By default the NavigationController has ForceCleanupOnLastPop enabled, which means that when the stack is again at 1 (due to PopAsync or PopToRootAsync calls) it will replace the controller automatically with the root form you have given to the constructor at the beginning.*
## Examples
You find all example projects in the "Examples" subfolder.
- [Examples/SystemCommandsBot/](Examples/SystemCommandsBot/)
Will allow you to run specific system commands or run/kill processes via Bot. Has also a simple authentication mechanism with one password.
- [Examples/JoinHiderBot/](Examples/JoinHiderBot/)
Will delete Join and Leave messages automatically in groups.
- [Examples/AsyncFormUpdates/](Examples/AsyncFormUpdates/)
When you want to update forms async without any user interaction (message/action) before. Use the new InvokeMessageLoop method of BotBase.
---
I will add more notes to it soon, so stay tuned.
-81
View File
@@ -1,81 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase;
using TelegramBotBase.Form;
using TelegramBaseTest.Tests;
namespace TelegramBaseTest
{
class Program
{
static void Main(string[] args)
{
String APIKey = "{YOUR API KEY}";
BotBase<Start> bb = new BotBase<Start>(APIKey);
bb.SystemCalls.Add("/start");
bb.SystemCalls.Add("/form1");
bb.SystemCalls.Add("/form2");
bb.SystemCalls.Add("/params");
bb.SystemCall += async (s, en) =>
{
switch (en.Command)
{
case "/start":
var start = new Start();
await en.Device.ActiveForm.NavigateTo(start);
break;
case "/form1":
var form1 = new TestForm();
await en.Device.ActiveForm.NavigateTo(form1);
break;
case "/form2":
var form2 = new TestForm2();
await en.Device.ActiveForm.NavigateTo(form2);
break;
case "/params":
String m = en.Parameters.DefaultIfEmpty("").Aggregate((a, b) => a + " and " + b);
await en.Device.Send("Your parameters are: " + m, replyTo: en.Device.LastMessageId);
en.Handled = true;
break;
}
};
bb.Start();
Console.WriteLine("Telegram Bot started...");
Console.WriteLine("Press q to quit application.");
Console.ReadLine();
bb.Stop();
}
}
}
-146
View File
@@ -1,146 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBaseTest.Tests
{
public class Start : AutoCleanForm
{
public Start()
{
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
}
public override async Task Action(MessageResult message)
{
var call = message.GetData<CallbackData>();
await message.ConfirmAction();
if (call == null)
return;
switch(call.Value)
{
case "text":
var sf = new SimpleForm();
await this.NavigateTo(sf);
break;
case "buttons":
var bf = new ButtonTestForm();
await this.NavigateTo(bf);
break;
case "progress":
var pf = new ProgressTest();
await this.NavigateTo(pf);
break;
case "registration":
var reg = new Register.Start();
await this.NavigateTo(reg);
break;
case "form1":
var form1 = new TestForm();
await this.NavigateTo(form1);
break;
case "form2":
var form2 = new TestForm2();
await this.NavigateTo(form2);
break;
case "data":
message.Handled = true;
var data = new DataForm();
await this.NavigateTo(data);
break;
case "calendar":
message.Handled = true;
var calendar = new Controls.CalendarPickerForm();
await this.NavigateTo(calendar);
break;
case "month":
message.Handled = true;
var month = new Controls.MonthPickerForm();
await this.NavigateTo(month);
break;
case "treeview":
message.Handled = true;
var tree = new Controls.TreeViewForms();
await this.NavigateTo(tree);
break;
}
}
public override async Task Render(MessageResult message)
{
ButtonForm btn = new ButtonForm();
btn.AddButtonRow(new ButtonBase("#1 Simple Text", new CallbackData("a", "text").Serialize()), new ButtonBase("#2 Button Test", new CallbackData("a", "buttons").Serialize()));
btn.AddButtonRow(new ButtonBase("#3 Progress Bar", new CallbackData("a", "progress").Serialize()));
btn.AddButtonRow(new ButtonBase("#4 Registration Example", new CallbackData("a", "registration").Serialize()));
btn.AddButtonRow(new ButtonBase("#5 Form1 Command", new CallbackData("a", "form1").Serialize()));
btn.AddButtonRow(new ButtonBase("#6 Form2 Command", new CallbackData("a", "form2").Serialize()));
btn.AddButtonRow(new ButtonBase("#7 Data Handling", new CallbackData("a", "data").Serialize()));
btn.AddButtonRow(new ButtonBase("#8 Calendar Picker", new CallbackData("a", "calendar").Serialize()));
btn.AddButtonRow(new ButtonBase("#9 Month Picker", new CallbackData("a", "month").Serialize()));
btn.AddButtonRow(new ButtonBase("#10 TreeView", new CallbackData("a", "treeview").Serialize()));
await this.Device.Send("Choose your test:", btn);
}
}
}
-6
View File
@@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="12.0.1" targetFramework="net452" />
<package id="System.Net.Requests" version="4.3.0" targetFramework="net452" />
<package id="Telegram.Bot" version="14.10.0" targetFramework="net452" />
</packages>
@@ -0,0 +1,61 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;
using Telegram.Bot.Types.InputFiles;
using Telegram.Bot.Types;
using TelegramBotBase.Sessions;
using TelegramBotBase.Form;
namespace TelegramBotBase.Extensions.Images
{
public static class ImageExtensions
{
public static Stream ToStream(this Image image, ImageFormat format)
{
var stream = new System.IO.MemoryStream();
image.Save(stream, format);
stream.Position = 0;
return stream;
}
/// <summary>
/// Sends an image
/// </summary>
/// <param name="image"></param>
/// <param name="name"></param>
/// <param name="buttons"></param>
/// <param name="replyTo"></param>
/// <param name="disableNotification"></param>
/// <returns></returns>
public static async Task<Message> SendPhoto(this DeviceSession session, Image image, String name, String caption, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false)
{
using (var fileStream = ToStream(image, ImageFormat.Png))
{
InputOnlineFile fts = new InputOnlineFile(fileStream, name);
return await session.SendPhoto(fts, caption: caption, buttons, replyTo, disableNotification);
}
}
/// <summary>
/// Sends an image
/// </summary>
/// <param name="image"></param>
/// <param name="name"></param>
/// <param name="buttons"></param>
/// <param name="replyTo"></param>
/// <param name="disableNotification"></param>
/// <returns></returns>
public static async Task<Message> SendPhoto(this DeviceSession session, Bitmap image, String name, String caption, ButtonForm buttons = null, int replyTo = 0, bool disableNotification = false)
{
using (var fileStream = ToStream(image, ImageFormat.Png))
{
InputOnlineFile fts = new InputOnlineFile(fileStream, name);
return await session.SendPhoto(fts, caption: caption, buttons, replyTo, disableNotification);
}
}
}
}
@@ -0,0 +1,8 @@
# TelegramBotBase.Extensions.Images
[![NuGet version (TelegramBotBase)](https://img.shields.io/nuget/v/TelegramBotBase.Extensions.Images.svg?style=flat-square)](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images/)
[![telegram chat](https://img.shields.io/badge/Support_Chat-Telegram-blue.svg?style=flat-square)](https://www.t.me/tgbotbase)
[![license](https://img.shields.io/github/license/MajMcCloud/telegrambotframework.svg?style=flat-square&maxAge=2592000&label=License)](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md)
[![downloads](https://img.shields.io/nuget/dt/TelegramBotBase.Extensions.Images.svg?style=flat-square&label=Package%20Downloads)](https://www.nuget.org/packages/TelegramBotBase.Extensions.Images)
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net5;netcoreapp3.1;net6</TargetFrameworks>
<RepositoryUrl>https://github.com/MajMcCloud/TelegramBotFramework</RepositoryUrl>
<PackageProjectUrl>https://github.com/MajMcCloud/TelegramBotFramework</PackageProjectUrl>
<Copyright>MIT</Copyright>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="Telegram.Bot" Version="17.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TelegramBotBase\TelegramBotBase.csproj" />
</ItemGroup>
</Project>
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
+109
View File
@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using TelegramBotBase;
using TelegramBotBase.Form;
using TelegramBotBaseTest.Tests;
using TelegramBotBase.Commands;
using TelegramBotBase.Builder;
namespace TelegramBotBaseTest
{
class Program
{
static void Main(string[] args)
{
String APIKey = "";
var bb = BotBaseBuilder
.Create()
.WithAPIKey(APIKey)
.DefaultMessageLoop()
.WithStartForm<Start>()
.NoProxy()
.CustomCommands(a =>
{
a.Start("Starts the bot");
a.Help("Should show you some help");
a.Settings("Should show you some settings");
a.Add("form1", "Opens test form 1");
a.Add("form2", "Opens test form 2");
a.Add("params", "Returns all send parameters as a message.");
})
.NoSerialization()
.UseEnglish()
.Build();
bb.BotCommand += async (s, en) =>
{
switch (en.Command)
{
case "/start":
var start = new Menu();
await en.Device.ActiveForm.NavigateTo(start);
break;
case "/form1":
var form1 = new TestForm();
await en.Device.ActiveForm.NavigateTo(form1);
break;
case "/form2":
var form2 = new TestForm2();
await en.Device.ActiveForm.NavigateTo(form2);
break;
case "/params":
String m = en.Parameters.DefaultIfEmpty("").Aggregate((a, b) => a + " and " + b);
await en.Device.Send("Your parameters are: " + m, replyTo: en.Device.LastMessageId);
en.Handled = true;
break;
}
};
//Update Bot commands to botfather
bb.UploadBotCommands().Wait();
bb.SetSetting(TelegramBotBase.Enums.eSettings.LogAllMessages, true);
bb.Message += (s, en) =>
{
Console.WriteLine(en.DeviceId + " " + en.Message.MessageText + " " + (en.Message.RawData ?? ""));
};
bb.Start();
Console.WriteLine("Telegram Bot started...");
Console.WriteLine("Press q to quit application.");
Console.ReadLine();
bb.Stop();
}
}
}
@@ -5,11 +5,11 @@ using System.Runtime.InteropServices;
// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die einer Assembly zugeordnet sind.
[assembly: AssemblyTitle("TelegramBaseTest")]
[assembly: AssemblyTitle("TelegramBotBaseTest")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TelegramBaseTest")]
[assembly: AssemblyProduct("TelegramBotBaseTest")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp3.1;net5;net6</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<Configurations>Debug;Release</Configurations>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\TelegramBotBase.Extensions.Images\TelegramBotBase.Extensions.Images.csproj" />
<ProjectReference Include="..\TelegramBotBase\TelegramBotBase.csproj" />
</ItemGroup>
</Project>
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBaseTest.Tests
namespace TelegramBotBaseTest.Tests
{
public class ButtonTestForm : AutoCleanForm
{
@@ -62,7 +62,7 @@ namespace TelegramBaseTest.Tests
case "back":
var st = new Start();
var st = new Menu();
await this.NavigateTo(st);
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Args;
using TelegramBotBase.Controls;
using TelegramBotBase.Controls.Hybrid;
using TelegramBotBase.Form;
namespace TelegramBotBaseTest.Tests.Controls
{
public class ButtonGridForm : AutoCleanForm
{
ButtonGrid m_Buttons = null;
public ButtonGridForm()
{
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
this.Init += ButtonGridForm_Init;
}
private async Task ButtonGridForm_Init(object sender, InitEventArgs e)
{
m_Buttons = new ButtonGrid();
m_Buttons.KeyboardType = TelegramBotBase.Enums.eKeyboardType.InlineKeyBoard;
ButtonForm bf = new ButtonForm();
bf.AddButtonRow(new ButtonBase("Back", "back"), new ButtonBase("Switch Keyboard", "switch"));
bf.AddButtonRow(new ButtonBase("Button1", "b1"), new ButtonBase("Button2", "b2"));
bf.AddButtonRow(new ButtonBase("Button3", "b3"), new ButtonBase("Button4", "b4"));
m_Buttons.ButtonsForm = bf;
m_Buttons.ButtonClicked += Bg_ButtonClicked;
this.AddControl(m_Buttons);
}
private async Task Bg_ButtonClicked(object sender, ButtonClickedEventArgs e)
{
if (e.Button == null)
return;
if (e.Button.Value == "back")
{
var start = new Menu();
await this.NavigateTo(start);
}
else if (e.Button.Value == "switch")
{
switch (m_Buttons.KeyboardType)
{
case TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard:
m_Buttons.KeyboardType = TelegramBotBase.Enums.eKeyboardType.InlineKeyBoard;
break;
case TelegramBotBase.Enums.eKeyboardType.InlineKeyBoard:
m_Buttons.KeyboardType = TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard;
break;
}
}
else
{
await this.Device.Send($"Button clicked with Text: {e.Button.Text} and Value {e.Button.Value}");
}
}
}
}
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Args;
using TelegramBotBase.Controls;
using TelegramBotBase.Controls.Hybrid;
using TelegramBotBase.Form;
namespace TelegramBotBaseTest.Tests.Controls
{
public class ButtonGridPagingForm : AutoCleanForm
{
ButtonGrid m_Buttons = null;
public ButtonGridPagingForm()
{
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
this.Init += ButtonGridForm_Init;
}
private async Task ButtonGridForm_Init(object sender, InitEventArgs e)
{
m_Buttons = new ButtonGrid();
m_Buttons.KeyboardType = TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard;
m_Buttons.EnablePaging = true;
m_Buttons.EnableSearch = true;
m_Buttons.HeadLayoutButtonRow = new List<ButtonBase>() { new ButtonBase("Back", "back") };
var countries = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
ButtonForm bf = new ButtonForm();
foreach (var c in countries)
{
bf.AddButtonRow(new ButtonBase(c.EnglishName, c.EnglishName));
}
m_Buttons.ButtonsForm = bf;
m_Buttons.ButtonClicked += Bg_ButtonClicked;
this.AddControl(m_Buttons);
}
private async Task Bg_ButtonClicked(object sender, ButtonClickedEventArgs e)
{
if (e.Button == null)
return;
if (e.Button.Value == "back")
{
var start = new Menu();
await this.NavigateTo(start);
}
else
{
await this.Device.Send($"Button clicked with Text: {e.Button.Text} and Value {e.Button.Value}");
}
}
}
}
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Args;
using TelegramBotBase.Controls;
using TelegramBotBase.Controls.Hybrid;
using TelegramBotBase.Form;
namespace TelegramBotBaseTest.Tests.Controls
{
public class ButtonGridTagForm : AutoCleanForm
{
TaggedButtonGrid m_Buttons = null;
public ButtonGridTagForm()
{
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
this.Init += ButtonGridTagForm_Init;
}
private async Task ButtonGridTagForm_Init(object sender, InitEventArgs e)
{
m_Buttons = new TaggedButtonGrid();
m_Buttons.KeyboardType = TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard;
m_Buttons.EnablePaging = true;
m_Buttons.HeadLayoutButtonRow = new List<ButtonBase>() { new ButtonBase("Back", "back") };
var countries = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
ButtonForm bf = new ButtonForm();
foreach (var c in countries)
{
bf.AddButtonRow(new TagButtonBase(c.EnglishName, c.EnglishName, c.Parent.EnglishName));
}
m_Buttons.Tags = countries.Select(a => a.Parent.EnglishName).Distinct().OrderBy(a => a).ToList();
m_Buttons.SelectedTags = countries.Select(a => a.Parent.EnglishName).Distinct().OrderBy(a => a).ToList();
m_Buttons.EnableCheckAllTools = true;
m_Buttons.DataSource = new TelegramBotBase.Datasources.ButtonFormDataSource(bf);
m_Buttons.ButtonClicked += Bg_ButtonClicked;
this.AddControl(m_Buttons);
}
private async Task Bg_ButtonClicked(object sender, ButtonClickedEventArgs e)
{
if (e.Button == null)
return;
switch (e.Button.Value)
{
case "back":
var start = new Menu();
await this.NavigateTo(start);
return;
}
await this.Device.Send($"Button clicked with Text: {e.Button.Text} and Value {e.Button.Value}");
}
}
}
@@ -7,8 +7,10 @@ using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.Controls;
using TelegramBotBase.Args;
using TelegramBotBase.Controls.Inline;
namespace TelegramBaseTest.Tests.Controls
namespace TelegramBotBaseTest.Tests.Controls
{
public class CalendarPickerForm : AutoCleanForm
{
@@ -39,7 +41,7 @@ namespace TelegramBaseTest.Tests.Controls
{
case "back":
var s = new Start();
var s = new Menu();
await this.NavigateTo(s);
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Args;
using TelegramBotBase.Controls;
using TelegramBotBase.Controls.Hybrid;
using TelegramBotBase.Form;
namespace TelegramBotBaseTest.Tests.Controls
{
public class CheckedButtonListForm : AutoCleanForm
{
CheckedButtonList m_Buttons = null;
public CheckedButtonListForm()
{
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
this.Init += CheckedButtonListForm_Init;
}
private async Task CheckedButtonListForm_Init(object sender, InitEventArgs e)
{
m_Buttons = new CheckedButtonList();
m_Buttons.KeyboardType = TelegramBotBase.Enums.eKeyboardType.InlineKeyBoard;
m_Buttons.EnablePaging = true;
m_Buttons.HeadLayoutButtonRow = new List<ButtonBase>() { new ButtonBase("Back", "back"), new ButtonBase("Switch Keyboard", "switch") };
m_Buttons.SubHeadLayoutButtonRow = new List<ButtonBase>() { new ButtonBase("No checked items", "$") };
ButtonForm bf = new ButtonForm();
for (int i = 0; i < 30; i++)
{
bf.AddButtonRow($"{i + 1}. Item", i.ToString());
}
m_Buttons.DataSource = new TelegramBotBase.Datasources.ButtonFormDataSource(bf);
m_Buttons.ButtonClicked += Bg_ButtonClicked;
m_Buttons.CheckedChanged += M_Buttons_CheckedChanged;
this.AddControl(m_Buttons);
}
private async Task M_Buttons_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
m_Buttons.SubHeadLayoutButtonRow = new List<ButtonBase>() { new ButtonBase($"{m_Buttons.CheckedItems.Count} checked items", "$") };
}
private async Task Bg_ButtonClicked(object sender, ButtonClickedEventArgs e)
{
if (e.Button == null)
return;
switch (e.Button.Value)
{
case "back":
var start = new Menu();
await NavigateTo(start);
break;
case "switch":
switch (m_Buttons.KeyboardType)
{
case TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard:
m_Buttons.KeyboardType = TelegramBotBase.Enums.eKeyboardType.InlineKeyBoard;
break;
case TelegramBotBase.Enums.eKeyboardType.InlineKeyBoard:
m_Buttons.KeyboardType = TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard;
break;
}
break;
default:
await Device.Send($"Button clicked with Text: {e.Button.Text} and Value {e.Button.Value}");
break;
}
}
}
}
@@ -7,8 +7,10 @@ using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.Controls;
using TelegramBotBase.Args;
using TelegramBotBase.Controls.Inline;
namespace TelegramBaseTest.Tests.Controls
namespace TelegramBotBaseTest.Tests.Controls
{
public class MonthPickerForm : AutoCleanForm
{
@@ -38,7 +40,7 @@ namespace TelegramBaseTest.Tests.Controls
{
case "back":
var s = new Start();
var s = new Menu();
await this.NavigateTo(s);
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Args;
using TelegramBotBase.Controls;
using TelegramBotBase.Controls.Inline;
using TelegramBotBase.Form;
namespace TelegramBotBaseTest.Tests.Controls
{
public class MultiToggleButtons : AutoCleanForm
{
public MultiToggleButtons()
{
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
this.Init += ToggleButtons_Init;
}
private async Task ToggleButtons_Init(object sender, InitEventArgs e)
{
var mtb = new MultiToggleButton();
mtb.Options = new List<ButtonBase>() { new ButtonBase("Option 1", "1"), new ButtonBase("Option 2", "2"), new ButtonBase("Option 3", "3") };
mtb.SelectedOption = mtb.Options.FirstOrDefault();
mtb.Toggled += Tb_Toggled;
this.AddControl(mtb);
mtb = new MultiToggleButton();
mtb.Options = new List<ButtonBase>() { new ButtonBase("Option 4", "4"), new ButtonBase("Option 5", "5"), new ButtonBase("Option 6", "6") };
mtb.SelectedOption = mtb.Options.FirstOrDefault();
mtb.AllowEmptySelection = false;
mtb.Toggled += Tb_Toggled;
this.AddControl(mtb);
}
private void Tb_Toggled(object sender, EventArgs e)
{
var tb = sender as MultiToggleButton;
if (tb.SelectedOption != null)
{
Console.WriteLine(tb.ID.ToString() + " was pressed, and toggled to " + tb.SelectedOption.Value);
return;
}
Console.WriteLine("Selection for " + tb.ID.ToString() + " has been removed.");
}
}
}
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Controls.Hybrid;
using TelegramBotBase.Form;
namespace TelegramBotBaseTest.Tests.Controls
{
public class MultiViewForm : AutoCleanForm
{
Subclass.MultiViewTest mvt = null;
ButtonGrid bg = null;
public MultiViewForm()
{
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
this.Init += MultiViewForm_Init;
}
private async Task MultiViewForm_Init(object sender, TelegramBotBase.Args.InitEventArgs e)
{
mvt = new Subclass.MultiViewTest();
AddControl(mvt);
bg = new ButtonGrid();
bg.ButtonsForm = new ButtonForm();
bg.ButtonsForm.AddButtonRow("Back", "$back$");
bg.ButtonClicked += Bg_ButtonClicked;
bg.KeyboardType = TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard;
AddControl(bg);
}
private async Task Bg_ButtonClicked(object sender, TelegramBotBase.Args.ButtonClickedEventArgs e)
{
switch(e.Button.Value)
{
case "$back$":
var mn = new Menu();
await NavigateTo(mn);
break;
}
}
}
}
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Args;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBotBaseTest.Tests.Controls.Subclass
{
public class MultiViewTest : TelegramBotBase.Controls.Hybrid.MultiView
{
public override async Task Action(MessageResult result, string value = null)
{
switch (result.RawData)
{
case "back":
this.SelectedViewIndex--;
break;
case "next":
this.SelectedViewIndex++;
break;
}
}
public override async Task RenderView(RenderViewEventArgs e)
{
ButtonForm bf = new ButtonForm();
bf.AddButtonRow(new ButtonBase("Back", "back"), new ButtonBase("Next", "next"));
switch (e.CurrentView)
{
case 0:
case 1:
case 2:
await Device.Send($"Page {e.CurrentView + 1}", bf);
break;
default:
await Device.Send("Unknown Page", bf);
break;
}
}
}
}
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Args;
using TelegramBotBase.Controls;
using TelegramBotBase.Controls.Inline;
using TelegramBotBase.Form;
namespace TelegramBotBaseTest.Tests.Controls
{
public class ToggleButtons : AutoCleanForm
{
public ToggleButtons()
{
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
this.Init += ToggleButtons_Init;
}
private async Task ToggleButtons_Init(object sender, InitEventArgs e)
{
var tb = new ToggleButton();
tb.Checked = true;
tb.Toggled += Tb_Toggled;
this.AddControl(tb);
tb = new ToggleButton();
tb.Checked = false;
tb.Toggled += Tb_Toggled;
this.AddControl(tb);
tb = new ToggleButton();
tb.Checked = true;
tb.Toggled += Tb_Toggled;
this.AddControl(tb);
}
private void Tb_Toggled(object sender, EventArgs e)
{
var tb = sender as ToggleButton;
Console.WriteLine(tb.ID.ToString() + " was pressed, and toggled to " + (tb.Checked ? "Checked" : "Unchecked"));
}
}
}
@@ -6,8 +6,10 @@ using System.Threading.Tasks;
using TelegramBotBase.Form;
using TelegramBotBase.Controls;
using TelegramBotBase.Base;
using TelegramBotBase.Args;
using TelegramBotBase.Controls.Inline;
namespace TelegramBaseTest.Tests.Controls
namespace TelegramBotBaseTest.Tests.Controls
{
public class TreeViewForms : AutoCleanForm
{
@@ -21,7 +23,7 @@ namespace TelegramBaseTest.Tests.Controls
this.Init += TreeViewForms_Init;
}
private async Task TreeViewForms_Init(object sender, TelegramBotBase.Base.InitEventArgs e)
private async Task TreeViewForms_Init(object sender, InitEventArgs e)
{
view = new TreeView();
@@ -60,7 +62,7 @@ namespace TelegramBaseTest.Tests.Controls
message.Handled = true;
var start = new Start();
var start = new Menu();
await this.NavigateTo(start);
@@ -3,13 +3,14 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.InputFiles;
using Telegram.Bot.Types.ReplyMarkups;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBaseTest.Tests
namespace TelegramBotBaseTest.Tests
{
public class DataForm : AutoCleanForm
{
@@ -110,7 +111,7 @@ namespace TelegramBaseTest.Tests
message.Handled = true;
var start = new Start();
var start = new Menu();
await this.NavigateTo(start);
@@ -0,0 +1,163 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using TelegramBotBase.Controls.Hybrid;
using TelegramBotBase.Datasources;
using TelegramBotBase.Form;
namespace TelegramBotBaseTest.Tests.Datasources
{
public class CustomDataSource : ButtonFormDataSource
{
public List<String> Countries = new List<string>() { "Country 1", "Country 2", "Country 3" };
public CustomDataSource()
{
loadData();
}
/// <summary>
/// This method has the example purpose of creating and loading some example data.
/// When using a database you do not need this kind of method.
/// </summary>
private void loadData()
{
//Exists data source? Read it
if (File.Exists(AppContext.BaseDirectory + "countries.json"))
{
try
{
var List = Newtonsoft.Json.JsonConvert.DeserializeObject<List<String>>(File.ReadAllText("countries.json"));
Countries = List;
}
catch
{
}
return;
}
//If not, create it
try
{
var countries = CultureInfo.GetCultures(CultureTypes.SpecificCultures).Select(a => a.DisplayName).ToList();
Countries = countries;
var tmp = Newtonsoft.Json.JsonConvert.SerializeObject(countries);
File.WriteAllText( AppContext.BaseDirectory + "countries.json", tmp);
}
catch
{
}
}
public override ButtonRow ItemAt(int index)
{
var item = Countries.ElementAt(index);
if (item == null)
return new ButtonRow();
return Render(item);
}
public override List<ButtonRow> ItemRange(int start, int count)
{
var items = Countries.Skip(start).Take(count);
List<ButtonRow> lst = new List<ButtonRow>();
foreach (var c in items)
{
lst.Add(Render(c));
}
return lst;
}
public override List<ButtonRow> AllItems()
{
List<ButtonRow> lst = new List<ButtonRow>();
foreach (var c in Countries)
{
lst.Add(Render(c));
}
return lst;
}
public override ButtonForm PickItems(int start, int count, string filter = null)
{
List<ButtonRow> rows = ItemRange(start, count);
ButtonForm lst = new ButtonForm();
foreach (var c in rows)
{
lst.AddButtonRow(c);
}
return lst;
}
public override ButtonForm PickAllItems(string filter = null)
{
List<ButtonRow> rows = AllItems();
ButtonForm bf = new ButtonForm();
bf.AddButtonRows(rows);
return bf;
}
public override int CalculateMax(string filter = null)
{
if (filter == null)
return Countries.Count;
return Countries.Where(a => a.IndexOf(filter, StringComparison.InvariantCultureIgnoreCase) != -1).Count();
}
public override ButtonRow Render(object data)
{
var s = data as String;
if (s == null)
return new ButtonRow(new ButtonBase("Empty", "zero"));
return new ButtonRow(new ButtonBase(s, s));
}
public override int Count
{
get
{
return Countries.Count;
}
}
public override int ColumnCount
{
get
{
return 1;
}
}
public override int RowCount
{
get
{
return this.Count;
}
}
}
}
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Controls.Hybrid;
using TelegramBotBase.Form;
namespace TelegramBotBaseTest.Tests.Datasources
{
public class List : FormBase
{
ButtonGrid __buttons = null;
public List()
{
this.Init += List_Init;
}
private async Task List_Init(object sender, TelegramBotBase.Args.InitEventArgs e)
{
__buttons = new ButtonGrid();
__buttons.EnablePaging = true;
__buttons.EnableSearch = false;
__buttons.ButtonClicked += __buttons_ButtonClicked;
__buttons.KeyboardType = TelegramBotBase.Enums.eKeyboardType.ReplyKeyboard;
__buttons.DeleteReplyMessage = true;
__buttons.HeadLayoutButtonRow = new ButtonRow(new ButtonBase("Back", "back"));
var cds = new CustomDataSource();
__buttons.DataSource = cds;
AddControl(__buttons);
}
private async Task __buttons_ButtonClicked(object sender, TelegramBotBase.Args.ButtonClickedEventArgs e)
{
switch(e.Button.Value)
{
case "back":
var mn = new Menu();
await NavigateTo(mn);
break;
}
}
}
}
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Args;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBotBaseTest.Tests.Groups
{
public class GroupChange : TelegramBotBase.Form.GroupForm
{
public GroupChange()
{
this.Opened += GroupChange_Opened;
}
private async Task GroupChange_Opened(object sender, EventArgs e)
{
ButtonForm bf = new ButtonForm();
bf.AddButtonRow(new ButtonBase("Open GroupChange Test", "groupchange"));
bf.AddButtonRow(new ButtonBase("Open WelcomeUser Test", "welcomeuser"));
bf.AddButtonRow(new ButtonBase("Open LinkReplace Test", "linkreplace"));
await this.Device.Send("GroupChange started, click to switch", bf);
}
public override async Task Action(MessageResult message)
{
if (message.Handled)
return;
var bn = message.RawData;
await message.ConfirmAction();
message.Handled = true;
switch (bn)
{
case "groupchange":
var gc = new GroupChange();
await this.NavigateTo(gc);
break;
case "welcomeuser":
var wu = new WelcomeUser();
await this.NavigateTo(wu);
break;
case "linkreplace":
var lr = new LinkReplaceTest();
await this.NavigateTo(lr);
break;
}
}
public override async Task OnGroupChanged(GroupChangedEventArgs e)
{
await this.Device.Send("Group has been changed by " + e.OriginalMessage.Message.From.FirstName + " " + e.OriginalMessage.Message.From.LastName);
}
}
}
@@ -0,0 +1,164 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBotBaseTest.Tests.Groups
{
public class LinkReplaceTest : TelegramBotBase.Form.GroupForm
{
Dictionary<long, int> Counter { get; set; } = new Dictionary<long, int>();
private const int Maximum = 3;
public LinkReplaceTest()
{
this.Opened += LinkReplaceTest_Opened;
}
private async Task LinkReplaceTest_Opened(object sender, EventArgs e)
{
ButtonForm bf = new ButtonForm();
bf.AddButtonRow(new ButtonBase("Open GroupChange Test", "groupchange"));
bf.AddButtonRow(new ButtonBase("Open WelcomeUser Test", "welcomeuser"));
bf.AddButtonRow(new ButtonBase("Open LinkReplace Test", "linkreplace"));
await this.Device.Send("LinkReplaceTest started, click to switch", bf);
}
public override async Task Action(MessageResult message)
{
if (message.Handled)
return;
var bn = message.RawData;
await message.ConfirmAction();
message.Handled = true;
switch (bn)
{
case "groupchange":
var gc = new GroupChange();
await this.NavigateTo(gc);
break;
case "welcomeuser":
var wu = new WelcomeUser();
await this.NavigateTo(wu);
break;
case "linkreplace":
var lr = new LinkReplaceTest();
await this.NavigateTo(lr);
break;
}
}
public override async Task OnMessage(MessageResult e)
{
var from = e.Message.From.Id;
if (e.Message.From.IsBot)
return;
//Are urls inside his message ?
if (!HasLinks(e.MessageText))
return;
var u = await Device.GetChatUser(from);
//Don't kick Admins or Creators
if (u.Status == Telegram.Bot.Types.Enums.ChatMemberStatus.Administrator | u.Status == Telegram.Bot.Types.Enums.ChatMemberStatus.Creator)
{
await this.Device.Send("You won't get kicked,...not this time.");
return;
}
await e.Device.DeleteMessage(e.MessageId);
var cp = new ChatPermissions();
cp.CanAddWebPagePreviews = false;
cp.CanChangeInfo = false;
cp.CanInviteUsers = false;
cp.CanPinMessages = false;
cp.CanSendMediaMessages = false;
cp.CanSendMessages = false;
cp.CanSendOtherMessages = false;
cp.CanSendPolls = false;
//Collect user "mistakes" with sending url, after 3 he gets kicked out.
if (Counter.ContainsKey(from))
{
Counter[from]++;
}
else
{
Counter[from] = 1;
}
if (Counter[from] >= 3)
{
await e.Device.KickUser(from);
await e.Device.Send(e.Message.From.FirstName + " " + e.Message.From.LastName + " has been removed from the group");
}
else
{
await e.Device.RestrictUser(from, cp, DateTime.UtcNow.AddSeconds(30));
await e.Device.Send(e.Message.From.FirstName + " " + e.Message.From.LastName + " has been blocked for 30 seconds");
}
}
/// <summary>
/// Call onmessage also on edited message, if someone want to spoof a failed message and insert a link.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public override async Task OnMessageEdit(MessageResult message)
{
await OnMessage(message);
}
/// <summary>
/// https://stackoverflow.com/a/20651284
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private bool HasLinks(String str)
{
var tmp = str;
var pattern = @"^(http|https|ftp|)\://|[a-zA-Z0-9\-\.]+\.[a-zA-Z](:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*[^\.\,\)\(\s]$";
Regex r = new Regex(pattern);
var matches = r.Matches(tmp);
return (matches.Count > 0);
}
}
}
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.Args;
namespace TelegramBotBaseTest.Tests.Groups
{
public class WelcomeUser : TelegramBotBase.Form.GroupForm
{
public WelcomeUser()
{
this.Opened += WelcomeUser_Opened;
}
private async Task WelcomeUser_Opened(object sender, EventArgs e)
{
ButtonForm bf = new ButtonForm();
bf.AddButtonRow(new ButtonBase("Open GroupChange Test", "groupchange"));
bf.AddButtonRow(new ButtonBase("Open WelcomeUser Test", "welcomeuser"));
bf.AddButtonRow(new ButtonBase("Open LinkReplace Test", "linkreplace"));
await this.Device.Send("WelcomeUser started, click to switch", bf);
}
public override async Task Action(MessageResult message)
{
if (message.Handled)
return;
var bn = message.RawData;
await message.ConfirmAction();
message.Handled = true;
switch (bn)
{
case "groupchange":
var gc = new GroupChange();
await this.NavigateTo(gc);
break;
case "welcomeuser":
var wu = new WelcomeUser();
await this.NavigateTo(wu);
break;
case "linkreplace":
var lr = new LinkReplaceTest();
await this.NavigateTo(lr);
break;
}
}
public override async Task OnMemberChanges(MemberChangeEventArgs e)
{
if (e.Type == Telegram.Bot.Types.Enums.MessageType.ChatMembersAdded)
{
await this.Device.Send("Welcome you new members!\r\n\r\n" + e.Members.Select(a => a.FirstName + " " + a.LastName).Aggregate((a, b) => a + "\r\n" + b));
}
else if (e.Type == Telegram.Bot.Types.Enums.MessageType.ChatMemberLeft)
{
await this.Device.Send(e.Members.Select(a => a.FirstName + " " + a.LastName).Aggregate((a, b) => a + " and " + b) + " has left the group");
}
}
}
}
+264
View File
@@ -0,0 +1,264 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBaseTest.Tests.Controls;
namespace TelegramBotBaseTest.Tests
{
public class Menu : AutoCleanForm
{
public Menu()
{
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
}
public override async Task Load(MessageResult message)
{
if (message.Message.Chat.Type == Telegram.Bot.Types.Enums.ChatType.Group | message.Message.Chat.Type == Telegram.Bot.Types.Enums.ChatType.Supergroup)
{
var sf = new TelegramBotBaseTest.Tests.Groups.WelcomeUser();
await this.NavigateTo(sf);
}
await Device.HideReplyKeyboard();
}
public override async Task Action(MessageResult message)
{
var call = message.GetData<CallbackData>();
await message.ConfirmAction();
if (call == null)
return;
message.Handled = true;
switch (call.Value)
{
case "text":
var sf = new SimpleForm();
await this.NavigateTo(sf);
break;
case "buttons":
var bf = new ButtonTestForm();
await this.NavigateTo(bf);
break;
case "progress":
var pf = new ProgressTest();
await this.NavigateTo(pf);
break;
case "registration":
var reg = new Register.Start();
await this.NavigateTo(reg);
break;
case "form1":
var form1 = new TestForm();
await this.NavigateTo(form1);
break;
case "form2":
var form2 = new TestForm2();
await this.NavigateTo(form2);
break;
case "data":
var data = new DataForm();
await this.NavigateTo(data);
break;
case "calendar":
var calendar = new Controls.CalendarPickerForm();
await this.NavigateTo(calendar);
break;
case "month":
var month = new Controls.MonthPickerForm();
await this.NavigateTo(month);
break;
case "treeview":
var tree = new Controls.TreeViewForms();
await this.NavigateTo(tree);
break;
case "togglebuttons":
var tb = new Controls.ToggleButtons();
await this.NavigateTo(tb);
break;
case "multitogglebuttons":
var mtb = new Controls.MultiToggleButtons();
await this.NavigateTo(mtb);
break;
case "buttongrid":
var bg = new Controls.ButtonGridForm();
await this.NavigateTo(bg);
break;
case "buttongridfilter":
var bg2 = new Controls.ButtonGridPagingForm();
await this.NavigateTo(bg2);
break;
case "buttongridtags":
var bg3 = new Controls.ButtonGridTagForm();
await this.NavigateTo(bg3);
break;
case "multiview":
var mvf = new MultiViewForm();
await NavigateTo(mvf);
break;
case "checkedbuttonlist":
var cbl = new CheckedButtonListForm();
await NavigateTo(cbl);
break;
case "navigationcontroller":
var nc = new Navigation.Start();
await NavigateTo(nc);
break;
case "dynamicbuttongrid":
var dg = new Datasources.List();
await NavigateTo(dg);
break;
case "notifications":
var not = new Notifications.Start();
await NavigateTo(not);
break;
default:
message.Handled = false;
break;
}
}
public override async Task Render(MessageResult message)
{
ButtonForm btn = new ButtonForm();
btn.AddButtonRow(new ButtonBase("#1 Simple Text", new CallbackData("a", "text").Serialize()), new ButtonBase("#2 Button Test", new CallbackData("a", "buttons").Serialize()));
btn.AddButtonRow(new ButtonBase("#3 Progress Bar", new CallbackData("a", "progress").Serialize()));
btn.AddButtonRow(new ButtonBase("#4 Registration Example", new CallbackData("a", "registration").Serialize()));
btn.AddButtonRow(new ButtonBase("#5 Form1 Command", new CallbackData("a", "form1").Serialize()));
btn.AddButtonRow(new ButtonBase("#6 Form2 Command", new CallbackData("a", "form2").Serialize()));
btn.AddButtonRow(new ButtonBase("#7 Data Handling", new CallbackData("a", "data").Serialize()));
btn.AddButtonRow(new ButtonBase("#8 Calendar Picker", new CallbackData("a", "calendar").Serialize()));
btn.AddButtonRow(new ButtonBase("#9 Month Picker", new CallbackData("a", "month").Serialize()));
btn.AddButtonRow(new ButtonBase("#10 TreeView", new CallbackData("a", "treeview").Serialize()));
btn.AddButtonRow(new ButtonBase("#11 ToggleButtons", new CallbackData("a", "togglebuttons").Serialize()));
btn.AddButtonRow(new ButtonBase("#11.2 MultiToggleButtons", new CallbackData("a", "multitogglebuttons").Serialize()));
btn.AddButtonRow(new ButtonBase("#12 ButtonGrid", new CallbackData("a", "buttongrid").Serialize()));
btn.AddButtonRow(new ButtonBase("#13 ButtonGrid Paging & Filter", new CallbackData("a", "buttongridfilter").Serialize()));
btn.AddButtonRow(new ButtonBase("#14 ButtonGrid Tags (Filter)", new CallbackData("a", "buttongridtags").Serialize()));
btn.AddButtonRow(new ButtonBase("#15 MultiView", new CallbackData("a", "multiview").Serialize()));
btn.AddButtonRow(new ButtonBase("#16 CheckedButtonList", new CallbackData("a", "checkedbuttonlist").Serialize()));
btn.AddButtonRow(new ButtonBase("#17 NavigationController (Push/Pop)", new CallbackData("a", "navigationcontroller").Serialize()));
btn.AddButtonRow(new ButtonBase("#18 Dynamic ButtonGrid (DataSources)", new CallbackData("a", "dynamicbuttongrid").Serialize()));
btn.AddButtonRow(new ButtonBase("#19 Notifications", new CallbackData("a", "notifications").Serialize()));
await this.Device.Send("Choose your test:", btn);
}
}
}
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Form;
using TelegramBotBase.Form.Navigation;
namespace TelegramBotBaseTest.Tests.Navigation
{
class CustomController : NavigationController
{
public CustomController(FormBase form) : base(form)
{
}
public override Task PushAsync(FormBase form, params object[] args)
{
Console.WriteLine($"Pushes form (Count on stack {this.Index + 1})");
//Device.Send($"Pushes form (Count on stack {this.Index + 1})");
return base.PushAsync(form, args);
}
public override Task PopAsync()
{
Console.WriteLine($"Pops one form (Count on stack {this.Index + 1})");
//Device.Send($"Pops one form (Count on stack {this.Index + 1})");
return base.PopAsync();
}
public override Task PopToRootAsync()
{
Console.WriteLine($"Moved back to root (Count on stack {this.Index + 1})");
//Device.Send($"Moved back to root (Count on stack {this.Index + 1})");
return base.PopToRootAsync();
}
}
}
@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBotBaseTest.Tests.Navigation
{
public class Form1 : FormBase
{
Message msg = null;
public Form1()
{
this.Closed += Form1_Closed;
}
private async Task Form1_Closed(object sender, EventArgs e)
{
if (msg == null)
return;
await Device.DeleteMessage(msg);
}
public override async Task Action(MessageResult message)
{
if (message.Handled)
return;
await message.ConfirmAction();
switch (message.RawData)
{
case "next":
message.Handled = true;
var f1 = new Form1();
await NavigationController.PushAsync(f1);
break;
case "previous":
message.Handled = true;
//Pop's the current form and move the previous one. The root form will be the Start class.
await NavigationController.PopAsync();
break;
case "root":
message.Handled = true;
await NavigationController.PopToRootAsync();
break;
}
}
public override async Task Render(MessageResult message)
{
if (msg != null)
return;
var bf = new ButtonForm();
bf.AddButtonRow("Next page", "next");
bf.AddButtonRow("Previous page", "previous");
bf.AddButtonRow("Back to root", "root");
msg = await Device.Send($"Choose your options (Count on stack {NavigationController.Index + 1})", bf);
}
}
}
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.Form.Navigation;
namespace TelegramBotBaseTest.Tests.Navigation
{
public class Start : FormBase
{
Message msg = null;
public Start()
{
}
public override async Task Load(MessageResult message)
{
}
public override async Task Action(MessageResult message)
{
if (message.Handled)
return;
await message.ConfirmAction();
switch (message.RawData)
{
case "yes":
message.Handled = true;
//Create navigation controller and navigate to it, keep the current form as root form so we can get back to here later
var nc = new CustomController(this);
nc.ForceCleanupOnLastPop = true;
var f1 = new Form1();
await nc.PushAsync(f1);
await NavigateTo(nc);
if (msg == null)
return;
await Device.DeleteMessage(msg);
break;
case "no":
message.Handled = true;
var mn = new Menu();
await NavigateTo(mn);
if (msg == null)
return;
await Device.DeleteMessage(msg);
break;
}
}
public override async Task Render(MessageResult message)
{
var bf = new ButtonForm();
bf.AddButtonRow("Yes", "yes");
bf.AddButtonRow("No", "no");
msg = await Device.Send("Open controller?", bf);
}
}
}
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBotBaseTest.Tests.Notifications
{
public class Start : AutoCleanForm
{
bool sent = false;
public Start()
{
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
}
public override async Task Action(MessageResult message)
{
if (message.Handled)
return;
switch (message.RawData)
{
case "alert":
await message.ConfirmAction("This is an alert.", true);
break;
case "back":
var mn = new Menu();
await NavigateTo(mn);
break;
default:
await message.ConfirmAction("This is feedback");
break;
}
}
public override async Task Render(MessageResult message)
{
if (sent)
return;
var bf = new ButtonForm();
bf.AddButtonRow("Normal feeback", "normal");
bf.AddButtonRow("Alert Box", "alert");
bf.AddButtonRow("Back", "back");
await Device.Send("Choose your test", bf);
sent = true;
}
}
}
@@ -5,9 +5,10 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Controls.Inline;
using TelegramBotBase.Form;
namespace TelegramBaseTest.Tests
namespace TelegramBotBaseTest.Tests
{
public class ProgressTest : AutoCleanForm
{
@@ -35,48 +36,48 @@ namespace TelegramBaseTest.Tests
if (call == null)
return;
TelegramBotBase.Controls.ProgressBar Bar = null;
ProgressBar Bar = null;
switch (call.Value)
{
case "standard":
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.standard);
Bar = new ProgressBar(0, 100, ProgressBar.eProgressStyle.standard);
Bar.Device = this.Device;
break;
case "squares":
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.squares);
Bar = new ProgressBar(0, 100, ProgressBar.eProgressStyle.squares);
Bar.Device = this.Device;
break;
case "circles":
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.circles);
Bar = new ProgressBar(0, 100, ProgressBar.eProgressStyle.circles);
Bar.Device = this.Device;
break;
case "lines":
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.lines);
Bar = new ProgressBar(0, 100, ProgressBar.eProgressStyle.lines);
Bar.Device = this.Device;
break;
case "squaredlines":
Bar = new TelegramBotBase.Controls.ProgressBar(0, 100, TelegramBotBase.Controls.ProgressBar.eProgressStyle.squaredLines);
Bar = new ProgressBar(0, 100, ProgressBar.eProgressStyle.squaredLines);
Bar.Device = this.Device;
break;
case "start":
var sf = new Start();
var sf = new Menu();
await this.NavigateTo(sf);
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBaseTest.Tests.Register
namespace TelegramBotBaseTest.Tests.Register
{
public class PerForm : AutoCleanForm
{
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBaseTest.Tests.Register
namespace TelegramBotBaseTest.Tests.Register
{
public class PerStep: AutoCleanForm
{
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBaseTest.Tests.Register
namespace TelegramBotBaseTest.Tests.Register
{
public class Start : AutoCleanForm
{
@@ -43,7 +43,7 @@ namespace TelegramBaseTest.Tests.Register
break;
case "backtodashboard":
var start = new Tests.Start();
var start = new Tests.Menu();
await this.NavigateTo(start);
@@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TelegramBaseTest.Tests.Register.Steps
namespace TelegramBotBaseTest.Tests.Register.Steps
{
public class Data
{
@@ -3,10 +3,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Args;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBaseTest.Tests.Register.Steps
namespace TelegramBotBaseTest.Tests.Register.Steps
{
public class Step1 : AutoCleanForm
{
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBaseTest.Tests.Register.Steps
namespace TelegramBotBaseTest.Tests.Register.Steps
{
public class Step2 : AutoCleanForm
{
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBaseTest.Tests.Register.Steps
namespace TelegramBotBaseTest.Tests.Register.Steps
{
public class Step3 : AutoCleanForm
{
@@ -6,14 +6,14 @@ using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBaseTest.Tests
namespace TelegramBotBaseTest.Tests
{
public class SimpleForm : AutoCleanForm
{
public SimpleForm()
{
this.DeleteSide = TelegramBotBase.Enums.eSide.Both;
this.DeleteSide = TelegramBotBase.Enums.eDeleteSide.Both;
this.DeleteMode = TelegramBotBase.Enums.eDeleteMode.OnLeavingForm;
this.Opened += SimpleForm_Opened;
@@ -55,7 +55,7 @@ namespace TelegramBaseTest.Tests
case "back":
var st = new Start();
var st = new Menu();
await this.NavigateTo(st);
+40
View File
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBotBaseTest.Tests
{
public class Start : SplitterForm
{
public override async Task<bool> Open(MessageResult e)
{
var st = new Menu();
await this.NavigateTo(st);
return true;
}
public override async Task<bool> OpenGroup(MessageResult e)
{
var st = new Groups.LinkReplaceTest();
await this.NavigateTo(st);
return true;
}
public override Task<bool> OpenChannel(MessageResult e)
{
return base.OpenChannel(e);
}
public override Task<bool> OpenSupergroup(MessageResult e)
{
return base.OpenSupergroup(e);
}
}
}
@@ -7,7 +7,7 @@ using Telegram.Bot.Types;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
namespace TelegramBaseTest.Tests
namespace TelegramBotBaseTest.Tests
{
public class TestForm : FormBase
{
@@ -39,7 +39,6 @@ namespace TelegramBaseTest.Tests
case "reply":
break;
case "navigate":
@@ -52,10 +51,10 @@ namespace TelegramBaseTest.Tests
default:
if (message.RawMessageData == null)
if (message.UpdateData == null)
return;
this.LastMessage = message.RawMessageData.Message.Text;
this.LastMessage = message.Message.Text;
break;
}
@@ -8,9 +8,10 @@ using System.Threading.Tasks;
using Telegram.Bot.Types;
using Telegram.Bot.Types.ReplyMarkups;
using TelegramBotBase.Base;
using TelegramBotBase.Extensions.Images;
using TelegramBotBase.Form;
namespace TelegramBaseTest.Tests
namespace TelegramBotBaseTest.Tests
{
public class TestForm2 : FormBase
{
@@ -43,6 +44,8 @@ namespace TelegramBaseTest.Tests
await message.DeleteMessage();
message.Handled = true;
if (call.Value == "testform1")
{
@@ -52,29 +55,19 @@ namespace TelegramBaseTest.Tests
}
else if (call.Value == "alert")
{
var fto = new TestForm2();
AlertDialog ad = new AlertDialog("This is a message", "Ok");
AlertDialog ad = new AlertDialog("This is a message", "Ok", fto);
ad.ButtonClicked += async (s, en) =>
{
var fto = new TestForm2();
await this.NavigateTo(fto);
};
await this.NavigateTo(ad);
}
else if (call.Value == "prompt")
else if (call.Value == "confirm")
{
PromptDialog pd = new PromptDialog("Please confirm");
pd.AddButton(new ButtonBase("Ok", "ok"));
pd.AddButton(new ButtonBase("Cancel", "cancel"));
var tf = new TestForm2();
pd.ButtonForms.Add("ok", tf);
pd.ButtonForms.Add("cancel", tf);
await this.NavigateTo(pd);
}
else if (call.Value == "promptevt")
{
PromptDialog pd = new PromptDialog("Please confirm", new ButtonBase("Ok", "ok"), new ButtonBase("Cancel", "cancel"));
ConfirmDialog pd = new ConfirmDialog("Please confirm", new ButtonBase("Ok", "ok"), new ButtonBase("Cancel", "cancel"));
pd.ButtonClicked += async (s, en) =>
{
@@ -85,6 +78,17 @@ namespace TelegramBaseTest.Tests
await this.NavigateTo(pd);
}
else if (call.Value == "prompt")
{
PromptDialog pd = new PromptDialog("Please tell me your name ?");
pd.Completed += async (s, en) =>
{
await this.Device.Send("Hello " + pd.Value);
};
await this.OpenModal(pd);
}
}
@@ -110,12 +114,12 @@ namespace TelegramBaseTest.Tests
btn.AddButtonRow(new ButtonBase("Information Prompt", CallbackData.Create("navigate", "alert")));
btn.AddButtonRow(new ButtonBase("Confirmation Prompt without event", CallbackData.Create("navigate", "prompt")));
btn.AddButtonRow(new ButtonBase("Confirmation Prompt with event", CallbackData.Create("navigate", "confirm")));
btn.AddButtonRow(new ButtonBase("Confirmation Prompt with event", CallbackData.Create("navigate", "promptevt")));
btn.AddButtonRow(new ButtonBase("Request Prompt", CallbackData.Create("navigate", "prompt")));
await this.Device.SendPhoto(bmp, "Test", btn);
await this.Device.SendPhoto(bmp, "Test", "", btn);
}
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net5" />
<package id="System.Net.Requests" version="4.3.0" targetFramework="net5" />
<package id="Telegram.Bot" version="14.12.0" targetFramework="net5" />
</packages>
@@ -6,12 +6,12 @@ using System.Threading.Tasks;
using Telegram.Bot.Types;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.Base
namespace TelegramBotBase.Args
{
/// <summary>
/// Base class for given system call results
/// Base class for given bot command results
/// </summary>
public class SystemCallEventArgs : EventArgs
public class BotCommandEventArgs : EventArgs
{
public String Command { get; set; }
@@ -26,13 +26,13 @@ namespace TelegramBotBase.Base
public Message OriginalMessage { get; set; }
public SystemCallEventArgs()
public BotCommandEventArgs()
{
}
public SystemCallEventArgs(String Command, List<String> Parameters, Message Message, long DeviceId, DeviceSession Device)
public BotCommandEventArgs(String Command, List<String> Parameters, Message Message, long DeviceId, DeviceSession Device)
{
this.Command = Command;
this.Parameters = Parameters;
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Controls.Hybrid;
using TelegramBotBase.Form;
namespace TelegramBotBase.Args
{
/// <summary>
/// Button get clicked event
/// </summary>
public class ButtonClickedEventArgs : EventArgs
{
public ButtonBase Button { get; set; }
public int Index { get; set; }
public object Tag { get; set; }
public ButtonRow Row { get; set; }
public ButtonClickedEventArgs()
{
}
public ButtonClickedEventArgs(ButtonBase button)
{
this.Button = button;
this.Index = -1;
}
public ButtonClickedEventArgs(ButtonBase button, int Index)
{
this.Button = button;
this.Index = Index;
}
public ButtonClickedEventArgs(ButtonBase button, int Index, ButtonRow row)
{
this.Button = button;
this.Index = Index;
this.Row = row;
}
}
}
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Text;
using TelegramBotBase.Controls.Hybrid;
using TelegramBotBase.Form;
namespace TelegramBotBase.Args
{
public class CheckedChangedEventArgs : EventArgs
{
/// <summary>
/// Contains the index of the row where the button is inside.
/// Contains -1 when it is a layout button or not found.
/// </summary>
public int Index { get; set; }
/// <summary>
/// Contains all buttons within this row, excluding the checkbox.
/// </summary>
public ButtonRow Row { get; set; }
/// <summary>
/// Contains the new checked status of the row.
/// </summary>
public bool Checked { get; set; }
public CheckedChangedEventArgs()
{
}
public CheckedChangedEventArgs(ButtonRow row, int Index, bool Checked)
{
this.Row = row;
this.Index = Index;
this.Checked = Checked;
}
}
}
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;
using Telegram.Bot.Types.Enums;
using TelegramBotBase.Base;
namespace TelegramBotBase.Args
{
public class GroupChangedEventArgs : EventArgs
{
public MessageType Type { get; set; }
public MessageResult OriginalMessage { get; set; }
public GroupChangedEventArgs(MessageType type, MessageResult message)
{
this.Type = type;
this.OriginalMessage = message;
}
}
}
@@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TelegramBotBase.Base
namespace TelegramBotBase.Args
{
public class InitEventArgs : EventArgs
{
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TelegramBotBase.Args
{
public class LoadStateEventArgs
{
public Dictionary<String,object> Values { get; set; }
public LoadStateEventArgs()
{
Values = new Dictionary<string, object>();
}
public List<String> Keys
{
get
{
return Values.Keys.ToList();
}
}
public String Get(String key)
{
return Values[key].ToString();
}
public int GetInt(String key)
{
int i = 0;
if (int.TryParse(Values[key].ToString(), out i))
return i;
return 0;
}
public double GetDouble(String key)
{
double d = 0;
if (double.TryParse(Values[key].ToString(), out d))
return d;
return 0;
}
public bool GetBool(String key)
{
bool b = false;
if (bool.TryParse(Values[key].ToString(), out b))
return b;
return false;
}
public object GetObject(String key)
{
return Values[key];
}
}
}
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using TelegramBotBase.Base;
namespace TelegramBotBase.Args
{
public class MemberChangeEventArgs : EventArgs
{
public List<User> Members { get; set; }
public MessageType Type { get; set; }
public MessageResult Result { get; set; }
public MemberChangeEventArgs()
{
this.Members = new List<User>();
}
public MemberChangeEventArgs(MessageType type, MessageResult result, params User[] members)
{
this.Type = type;
this.Result = result;
this.Members = members.ToList();
}
}
}
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types;
namespace TelegramBotBase.Args
{
public class MessageDeletedEventArgs
{
public int MessageId
{
get;set;
}
public MessageDeletedEventArgs(int messageId)
{
this.MessageId = messageId;
}
}
}
@@ -7,7 +7,7 @@ using TelegramBotBase.Sessions;
namespace TelegramBotBase.Base
{
public class MessageIncomeResult : EventArgs
public class MessageIncomeEventArgs : EventArgs
{
public long DeviceId { get; set; }
@@ -16,7 +16,7 @@ namespace TelegramBotBase.Base
public MessageResult Message { get; set; }
public MessageIncomeResult(long DeviceId, DeviceSession Device, MessageResult message)
public MessageIncomeEventArgs(long DeviceId, DeviceSession Device, MessageResult message)
{
this.DeviceId = DeviceId;
this.Device = Device;
@@ -5,7 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types;
namespace TelegramBotBase.Base
namespace TelegramBotBase.Args
{
public class MessageReceivedEventArgs
{
@@ -5,7 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using Telegram.Bot.Types;
namespace TelegramBotBase.Base
namespace TelegramBotBase.Args
{
public class MessageSentEventArgs
{
@@ -19,9 +19,16 @@ namespace TelegramBotBase.Base
public Message Message { get; set; }
public MessageSentEventArgs(Message message)
/// <summary>
/// Contains the element, which has called the method.
/// </summary>
public Type Origin { get; set; }
public MessageSentEventArgs(Message message, Type Origin)
{
this.Message = message;
this.Origin = Origin;
}
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TelegramBotBase.Args
{
public class PromptDialogCompletedEventArgs
{
public object Tag { get; set; }
public String Value { get; set; }
}
}
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TelegramBotBase.Args
{
public class RenderViewEventArgs : EventArgs
{
public int CurrentView { get; set; }
public RenderViewEventArgs(int ViewIndex)
{
CurrentView = ViewIndex;
}
}
}
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TelegramBotBase.Args
{
public class SaveStateEventArgs
{
public Dictionary<String, object> Values { get; set; }
public SaveStateEventArgs()
{
Values = new Dictionary<string, object>();
}
public void Set(String key, String value)
{
Values[key] = value;
}
public void SetInt(String key, int value)
{
Values[key] = value;
}
public void SetBool(String key, bool value)
{
Values[key] = value;
}
public void SetDouble(String key, double value)
{
Values[key] = value;
}
public void SetObject(String key, object value)
{
Values[key] = value;
}
}
}
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TelegramBotBase.Base;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.Args
{
public class SaveStatesEventArgs
{
public StateContainer States { get; set; }
public SaveStatesEventArgs(StateContainer states)
{
this.States = states;
}
}
}
@@ -7,13 +7,13 @@ using TelegramBotBase.Sessions;
namespace TelegramBotBase.Base
{
public class SessionBeginResult : EventArgs
public class SessionBeginEventArgs : EventArgs
{
public long DeviceId { get; set; }
public DeviceSession Device { get; set; }
public SessionBeginResult(long DeviceId, DeviceSession Device)
public SessionBeginEventArgs(long DeviceId, DeviceSession Device)
{
this.DeviceId = DeviceId;
this.Device = Device;
@@ -5,7 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.Base
namespace TelegramBotBase.Args
{
public class SystemExceptionEventArgs : EventArgs
{
@@ -6,7 +6,7 @@ using System.Threading.Tasks;
using Telegram.Bot.Types;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.Base
namespace TelegramBotBase.Args
{
public class UnhandledCallEventArgs : EventArgs
{
+17
View File
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TelegramBotBase.Attributes
{
/// <summary>
/// Declares that this class should not be getting serialized
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class IgnoreState : Attribute
{
}
}
+15
View File
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TelegramBotBase.Attributes
{
/// <summary>
/// Declares that the field or property should be save and recovered an restart.
/// </summary>
public class SaveState : Attribute
{
public String Key { get; set; }
}
}
@@ -1,29 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Form;
namespace TelegramBotBase.Base
{
/// <summary>
/// Button get clicked event
/// </summary>
public class ButtonClickedEventArgs : EventArgs
{
public ButtonBase Button { get; set; }
public ButtonClickedEventArgs()
{
}
public ButtonClickedEventArgs(ButtonBase button)
{
this.Button = button;
}
}
}
+37 -1
View File
@@ -15,18 +15,44 @@ namespace TelegramBotBase.Base
public int ID { get; set; }
public String ControlID
{
get
{
return "#c" + this.ID.ToString();
}
}
/// <summary>
/// Defines if the control should be rendered and invoked with actions
/// </summary>
public bool Enabled { get; set; } = true;
public virtual async Task Action(MessageResult result)
/// <summary>
/// Get invoked when control will be added to a form and invoked.
/// </summary>
/// <returns></returns>
public virtual void Init()
{
}
public virtual async Task Load(MessageResult result)
{
}
public virtual async Task Action(MessageResult result, String value = null)
{
}
public virtual async Task Render(MessageResult result)
{
@@ -35,6 +61,16 @@ namespace TelegramBotBase.Base
}
public virtual async Task Hidden(bool FormClose)
{
}
/// <summary>
/// Will be called on a cleanup.
/// </summary>
/// <returns></returns>
public virtual async Task Cleanup()
{
+109 -16
View File
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.InputFiles;
@@ -13,7 +15,11 @@ namespace TelegramBotBase.Base
/// </summary>
public class DataResult : ResultBase
{
public Telegram.Bot.Args.MessageEventArgs RawMessageData { get; set; }
//public Telegram.Bot.Args.MessageEventArgs RawMessageData { get; set; }
public UpdateResult UpdateData { get; set; }
public Contact Contact
{
@@ -63,11 +69,20 @@ namespace TelegramBotBase.Base
}
}
public Telegram.Bot.Types.Enums.MessageType Type
{
get
{
return this.RawMessageData?.Message?.Type ?? Telegram.Bot.Types.Enums.MessageType.Unknown;
return this.Message?.Type ?? Telegram.Bot.Types.Enums.MessageType.Unknown;
}
}
public override Message Message
{
get
{
return this.UpdateData?.Message;
}
}
@@ -85,45 +100,123 @@ namespace TelegramBotBase.Base
}
}
public DataResult(Telegram.Bot.Args.MessageEventArgs rawdata)
public DataResult(UpdateResult update)
{
this.RawMessageData = rawdata;
this.Message = rawdata.Message;
this.UpdateData = update;
}
public DataResult(MessageResult message)
{
this.RawMessageData = message.RawMessageData;
this.Message = message.Message;
}
public async Task<InputOnlineFile> DownloadDocument()
{
var encryptedContent = new System.IO.MemoryStream(this.Document.FileSize);
var encryptedContent = new System.IO.MemoryStream(this.Document.FileSize.Value);
var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, encryptedContent);
return new InputOnlineFile(encryptedContent, this.Document.FileName);
}
/// <summary>
/// Downloads a file and saves it to the given path.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public async Task DownloadDocument(String path)
{
var file = await this.Client.TelegramClient.GetFileAsync(this.Document.FileId);
FileStream fs = new FileStream(path, FileMode.Create);
await this.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs);
fs.Close();
fs.Dispose();
}
/// <summary>
/// Downloads the document and returns an byte array.
/// </summary>
/// <returns></returns>
public async Task<byte[]> DownloadRawDocument()
{
MemoryStream ms = new MemoryStream();
await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, ms);
return ms.ToArray();
}
/// <summary>
/// Downloads a document and returns it as string. (txt,csv,etc) Default encoding ist UTF8.
/// </summary>
/// <returns></returns>
public async Task<String> DownloadRawTextDocument()
{
return await DownloadRawTextDocument(Encoding.UTF8);
}
/// <summary>
/// Downloads a document and returns it as string. (txt,csv,etc)
/// </summary>
/// <returns></returns>
public async Task<String> DownloadRawTextDocument(Encoding encoding)
{
MemoryStream ms = new MemoryStream();
await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Document.FileId, ms);
ms.Position = 0;
var sr = new StreamReader(ms, encoding);
return sr.ReadToEnd();
}
public async Task<InputOnlineFile> DownloadVideo()
{
var encryptedContent = new System.IO.MemoryStream(this.Video.FileSize);
var encryptedContent = new System.IO.MemoryStream(this.Video.FileSize.Value);
var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Video.FileId, encryptedContent);
return new InputOnlineFile(encryptedContent, "");
}
public async Task DownloadVideo(String path)
{
var file = await this.Client.TelegramClient.GetFileAsync(this.Video.FileId);
FileStream fs = new FileStream(path, FileMode.Create);
await this.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs);
fs.Close();
fs.Dispose();
}
public async Task<InputOnlineFile> DownloadAudio()
{
var encryptedContent = new System.IO.MemoryStream(this.Audio.FileSize.Value);
var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(this.Audio.FileId, encryptedContent);
return new InputOnlineFile(encryptedContent, "");
}
public async Task DownloadAudio(String path)
{
var file = await this.Client.TelegramClient.GetFileAsync(this.Audio.FileId);
FileStream fs = new FileStream(path, FileMode.Create);
await this.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs);
fs.Close();
fs.Dispose();
}
public async Task<InputOnlineFile> DownloadPhoto(int index)
{
var photo = this.Photos[index];
var encryptedContent = new System.IO.MemoryStream(photo.FileSize);
var encryptedContent = new System.IO.MemoryStream(photo.FileSize.Value);
var file = await this.Client.TelegramClient.GetInfoAndDownloadFileAsync(photo.FileId, encryptedContent);
return new InputOnlineFile(encryptedContent, "");
}
public async Task DownloadPhoto(int index, String path)
{
var photo = this.Photos[index];
var file = await this.Client.TelegramClient.GetFileAsync(photo.FileId);
FileStream fs = new FileStream(path, FileMode.Create);
await this.Client.TelegramClient.DownloadFileAsync(file.FilePath, fs);
fs.Close();
fs.Dispose();
}
}
}
+191 -11
View File
@@ -4,7 +4,9 @@ using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Args;
using TelegramBotBase.Base;
using TelegramBotBase.Form.Navigation;
using TelegramBotBase.Sessions;
using static TelegramBotBase.Base.Async;
@@ -16,6 +18,8 @@ namespace TelegramBotBase.Form
public class FormBase : IDisposable
{
public NavigationController NavigationController { get; set; }
public DeviceSession Device { get; set; }
public MessageClient Client { get; set; }
@@ -28,7 +32,7 @@ namespace TelegramBotBase.Form
public List<ControlBase> Controls { get; set; }
private EventHandlerList Events = new EventHandlerList();
public EventHandlerList Events = new EventHandlerList();
private static object __evInit = new object();
@@ -54,8 +58,11 @@ namespace TelegramBotBase.Form
if (this.Events[__evInit] == null)
return;
var handler = this.Events[__evInit].GetInvocationList().Cast<AsyncEventHandler<InitEventArgs>>();
foreach(var h in handler)
var handler = this.Events[__evInit]?.GetInvocationList().Cast<AsyncEventHandler<InitEventArgs>>();
if (handler == null)
return;
foreach (var h in handler)
{
await Async.InvokeAllAsync<InitEventArgs>(h, this, e);
}
@@ -83,7 +90,10 @@ namespace TelegramBotBase.Form
if (this.Events[__evOpened] == null)
return;
var handler = this.Events[__evOpened].GetInvocationList().Cast<AsyncEventHandler<EventArgs>>();
var handler = this.Events[__evOpened]?.GetInvocationList().Cast<AsyncEventHandler<EventArgs>>();
if (handler == null)
return;
foreach (var h in handler)
{
await Async.InvokeAllAsync<EventArgs>(h, this, e);
@@ -106,18 +116,24 @@ namespace TelegramBotBase.Form
}
}
public async Task OnClosed(EventArgs e)
{
if (this.Events[__evClosed] == null)
return;
var handler = this.Events[__evClosed].GetInvocationList().Cast<AsyncEventHandler<EventArgs>>();
var handler = this.Events[__evClosed]?.GetInvocationList().Cast<AsyncEventHandler<EventArgs>>();
if (handler == null)
return;
foreach (var h in handler)
{
await Async.InvokeAllAsync<EventArgs>(h, this, e);
}
}
/// <summary>
/// Form has been closed (left)
/// </summary>
@@ -134,6 +150,16 @@ namespace TelegramBotBase.Form
}
}
/// <summary>
/// Get invoked when a modal child from has been closed.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task ReturnFromModal(ModalDialog modal)
{
}
/// <summary>
/// Pre to form close, cleanup all controls
@@ -143,7 +169,7 @@ namespace TelegramBotBase.Form
{
foreach (var b in this.Controls)
{
await b.Cleanup();
b.Cleanup().Wait();
}
}
@@ -152,6 +178,33 @@ namespace TelegramBotBase.Form
}
/// <summary>
/// Gets invoked if a message was sent or an action triggered
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task LoadControls(MessageResult message)
{
//Looking for the control by id, if not listened, raise event for all
if (message.RawData?.StartsWith("#c") ?? false)
{
var c = this.Controls.FirstOrDefault(a => a.ControlID == message.RawData.Split('_')[0]);
if (c != null)
{
await c.Load(message);
return;
}
}
foreach (var b in this.Controls)
{
if (!b.Enabled)
continue;
await b.Load(message);
}
}
/// <summary>
/// Gets invoked if the form gets loaded and on every message belongs to this context
/// </summary>
@@ -162,19 +215,44 @@ namespace TelegramBotBase.Form
}
/// <summary>
/// Gets invoked, when a messages has been edited.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public virtual async Task Edited(MessageResult message)
{
}
/// <summary>
/// Gets invoked if the user clicked a button.
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task ActionControls(MessageResult message)
public virtual async Task ActionControls(MessageResult message)
{
//Looking for the control by id, if not listened, raise event for all
if (message.RawData.StartsWith("#c"))
{
var c = this.Controls.FirstOrDefault(a => a.ControlID == message.RawData.Split('_')[0]);
if (c != null)
{
await c.Action(message, message.RawData.Split('_')[1]);
return;
}
}
foreach (var b in this.Controls)
{
if (!b.Enabled)
continue;
await b.Action(message);
if (message.Handled)
return;
}
}
@@ -203,7 +281,7 @@ namespace TelegramBotBase.Form
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task RenderControls(MessageResult message)
public virtual async Task RenderControls(MessageResult message)
{
foreach (var b in this.Controls)
{
@@ -231,7 +309,7 @@ namespace TelegramBotBase.Form
/// </summary>
/// <param name="newForm"></param>
/// <returns></returns>
public async Task NavigateTo(FormBase newForm, params object[] args)
public virtual async Task NavigateTo(FormBase newForm, params object[] args)
{
DeviceSession ds = this.Device;
if (ds == null)
@@ -245,20 +323,122 @@ namespace TelegramBotBase.Form
newForm.Client = this.Client;
newForm.Device = ds;
await newForm.OnInit(new InitEventArgs(args));
//Notify prior to close
foreach (var b in this.Controls)
{
if (!b.Enabled)
continue;
await this.CloseControls();
await b.Hidden(true);
}
this.CloseControls().Wait();
await this.OnClosed(new EventArgs());
await newForm.OnInit(new InitEventArgs(args));
await newForm.OnOpened(new EventArgs());
}
/// <summary>
/// Opens this form modal, but don't closes the original ones
/// </summary>
/// <param name="newForm"></param>
/// <returns></returns>
public virtual async Task OpenModal(ModalDialog newForm, params object[] args)
{
DeviceSession ds = this.Device;
if (ds == null)
return;
var parentForm = this;
ds.FormSwitched = true;
ds.PreviousForm = ds.ActiveForm;
ds.ActiveForm = newForm;
newForm.Client = parentForm.Client;
newForm.Device = ds;
newForm.ParentForm = parentForm;
newForm.Closed += async (s, en) =>
{
await CloseModal(newForm, parentForm);
};
foreach (var b in this.Controls)
{
if (!b.Enabled)
continue;
await b.Hidden(false);
}
await newForm.OnInit(new InitEventArgs(args));
await newForm.OnOpened(new EventArgs());
}
public async Task CloseModal(ModalDialog modalForm, FormBase oldForm)
{
DeviceSession ds = this.Device;
if (ds == null)
return;
if (modalForm == null)
throw new Exception("No modal form");
ds.FormSwitched = true;
ds.PreviousForm = ds.ActiveForm;
ds.ActiveForm = oldForm;
}
/// <summary>
/// Adds a control to the formular and sets its ID and Device.
/// </summary>
/// <param name="control"></param>
public void AddControl(ControlBase control)
{
//Duplicate check
if (this.Controls.Contains(control))
throw new ArgumentException("Control has been already added.");
control.ID = this.Controls.Count + 1;
control.Device = this.Device;
this.Controls.Add(control);
control.Init();
}
/// <summary>
/// Removes control from the formular and runs a cleanup on it.
/// </summary>
/// <param name="control"></param>
public void RemoveControl(ControlBase control)
{
if (!this.Controls.Contains(control))
return;
control.Cleanup().Wait();
this.Controls.Remove(control);
}
/// <summary>
/// Removes all controls.
/// </summary>
public void RemoveAllControls()
{
foreach(var c in this.Controls)
{
c.Cleanup().Wait();
this.Controls.Remove(c);
}
}
/// <summary>
+89 -51
View File
@@ -5,7 +5,13 @@ using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Bot.Exceptions;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Extensions.Polling;
namespace TelegramBotBase.Base
{
@@ -15,16 +21,23 @@ namespace TelegramBotBase.Base
public class MessageClient
{
public String APIKey { get; set; }
public Telegram.Bot.TelegramBotClient TelegramClient { get; set; }
public ITelegramBotClient TelegramClient { get; set; }
private EventHandlerList __Events { get; set; } = new EventHandlerList();
private static object __evOnMessageLoop = new object();
private static object __evOnMessage = new object();
private static object __evOnMessageEdit = new object();
private static object __evCallbackQuery = new object();
CancellationTokenSource __cancellationTokenSource;
public MessageClient(String APIKey)
{
@@ -43,13 +56,22 @@ namespace TelegramBotBase.Base
Prepare();
}
public MessageClient(String APIKey, Uri proxyUrl)
public MessageClient(String APIKey, Uri proxyUrl, NetworkCredential credential = null)
{
this.APIKey = APIKey;
var proxy = new WebProxy(proxyUrl);
var proxy = new WebProxy(proxyUrl)
{
Credentials = credential
};
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, proxy);
var httpClient = new HttpClient(
new HttpClientHandler { Proxy = proxy, UseProxy = true }
);
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, httpClient);
Prepare();
}
@@ -66,11 +88,17 @@ namespace TelegramBotBase.Base
var proxy = new WebProxy(proxyHost, proxyPort);
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, proxy);
var httpClient = new HttpClient(
new HttpClientHandler { Proxy = proxy, UseProxy = true }
);
this.TelegramClient = new Telegram.Bot.TelegramBotClient(APIKey, httpClient);
Prepare();
}
public MessageClient(String APIKey, Telegram.Bot.TelegramBotClient Client)
{
this.APIKey = APIKey;
@@ -84,80 +112,90 @@ namespace TelegramBotBase.Base
{
this.TelegramClient.Timeout = new TimeSpan(0, 0, 30);
this.TelegramClient.OnMessage += TelegramClient_OnMessage;
this.TelegramClient.OnCallbackQuery += TelegramClient_OnCallbackQuery;
}
private async void TelegramClient_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs e)
public void StartReceiving()
{
//Skip empty messages by default
if (e.Message == null)
return;
__cancellationTokenSource = new CancellationTokenSource();
try
{
var mr = new MessageResult(e);
mr.Client = this;
OnMessage(mr);
}
catch
var receiverOptions = new ReceiverOptions
{
AllowedUpdates = { } // receive all update types
};
}
this.TelegramClient.StartReceiving(HandleUpdateAsync, HandleErrorAsync, receiverOptions, __cancellationTokenSource.Token);
}
private async void TelegramClient_OnCallbackQuery(object sender, Telegram.Bot.Args.CallbackQueryEventArgs e)
public void StopReceiving()
{
try
{
var ar = new MessageResult(e);
ar.Client = this;
OnAction(ar);
__cancellationTokenSource.Cancel();
}
catch
{
public Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
OnMessageLoop(new UpdateResult(update, null));
return Task.CompletedTask;
}
public Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{
if (exception is ApiRequestException exAPI)
{
Console.WriteLine($"Telegram API Error:\n[{exAPI.ErrorCode}]\n{exAPI.Message}");
}
else
{
Console.WriteLine(exception.ToString());
}
return Task.CompletedTask;
}
/// <summary>
/// This will return the current list of bot commands.
/// </summary>
/// <returns></returns>
public async Task<BotCommand[]> GetBotCommands()
{
return await this.TelegramClient.GetMyCommandsAsync();
}
/// <summary>
/// This will set your bot commands to the given list.
/// </summary>
/// <param name="botcommands"></param>
/// <returns></returns>
public async Task SetBotCommands(List<BotCommand> botcommands)
{
await this.TelegramClient.SetMyCommandsAsync(botcommands);
}
#region "Events"
public event EventHandler<MessageResult> Message
public event Async.AsyncEventHandler<UpdateResult> MessageLoop
{
add
{
this.__Events.AddHandler(__evOnMessage, value);
this.__Events.AddHandler(__evOnMessageLoop, value);
}
remove
{
this.__Events.RemoveHandler(__evOnMessage, value);
this.__Events.RemoveHandler(__evOnMessageLoop, value);
}
}
public void OnMessage(MessageResult result)
public void OnMessageLoop(UpdateResult update)
{
(this.__Events[__evOnMessage] as EventHandler<MessageResult>)?.Invoke(this, result);
}
public event EventHandler<MessageResult> Action
{
add
{
this.__Events.AddHandler(__evCallbackQuery, value);
}
remove
{
this.__Events.RemoveHandler(__evCallbackQuery, value);
}
}
public void OnAction(MessageResult result)
{
(this.__Events[__evCallbackQuery] as EventHandler<MessageResult>)?.Invoke(this, result);
(this.__Events[__evOnMessageLoop] as Async.AsyncEventHandler<UpdateResult>)?.Invoke(this, update);
}
+59 -32
View File
@@ -3,15 +3,16 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Types;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.Base
{
public class MessageResult : ResultBase
{
public Telegram.Bot.Args.MessageEventArgs RawMessageData { get; set; }
public Telegram.Bot.Args.CallbackQueryEventArgs RawCallbackData { get; set; }
public Telegram.Bot.Types.Update UpdateData { get; set; }
/// <summary>
/// Returns the Device/ChatId
@@ -20,12 +21,19 @@ namespace TelegramBotBase.Base
{
get
{
return this.RawMessageData?.Message?.Chat.Id ?? this.RawCallbackData?.CallbackQuery.Message?.Chat.Id ?? 0;
return this.UpdateData?.Message?.Chat?.Id
?? this.UpdateData?.EditedMessage?.Chat.Id
?? this.UpdateData?.CallbackQuery.Message?.Chat.Id
?? Device?.DeviceId
?? 0;
}
}
public DeviceSession Device
{ get; set; }
{
get;
set;
}
/// <summary>
/// The message id
@@ -34,7 +42,10 @@ namespace TelegramBotBase.Base
{
get
{
return this.Message?.MessageId ?? this.RawCallbackData?.CallbackQuery?.Message?.MessageId ?? 0;
return this.UpdateData?.Message?.MessageId
?? this.Message?.MessageId
?? this.UpdateData?.CallbackQuery?.Message?.MessageId
?? 0;
}
}
@@ -42,7 +53,7 @@ namespace TelegramBotBase.Base
{
get
{
return this.RawMessageData?.Message?.Text ?? "";
return this.UpdateData?.Message?.Text ?? "";
}
}
@@ -50,7 +61,27 @@ namespace TelegramBotBase.Base
{
get
{
return this.RawMessageData?.Message?.Text ?? "";
return this.UpdateData?.Message?.Text ?? "";
}
}
public Telegram.Bot.Types.Enums.MessageType MessageType
{
get
{
return Message?.Type ?? Telegram.Bot.Types.Enums.MessageType.Unknown;
}
}
public Message Message
{
get
{
return this.UpdateData?.Message
?? this.UpdateData?.EditedMessage
?? this.UpdateData?.ChannelPost
?? this.UpdateData?.EditedChannelPost
?? this.UpdateData?.CallbackQuery?.Message;
}
}
@@ -61,14 +92,14 @@ namespace TelegramBotBase.Base
{
get
{
return (this.RawCallbackData != null);
return (this.UpdateData.CallbackQuery != null);
}
}
/// <summary>
/// Is this a system call ? Starts with a slash '/' and a command
/// Is this a command ? Starts with a slash '/' and a command
/// </summary>
public bool IsSystemCall
public bool IsBotCommand
{
get
{
@@ -79,11 +110,11 @@ namespace TelegramBotBase.Base
/// <summary>
/// Returns a List of all parameters which has been sent with the command itself (i.e. /start 123 456 789 => 123,456,789)
/// </summary>
public List<String> SystemCallParameters
public List<String> BotCommandParameters
{
get
{
if (!IsSystemCall)
if (!IsBotCommand)
return new List<string>();
//Split by empty space and skip first entry (command itself), return as list
@@ -92,26 +123,31 @@ namespace TelegramBotBase.Base
}
/// <summary>
/// Returns just the System call command (i.e. /start 1 2 3 => /start)
/// Returns just the command (i.e. /start 1 2 3 => /start)
/// </summary>
public String SystemCommand
public String BotCommand
{
get
{
if (!IsSystemCall)
if (!IsBotCommand)
return null;
return this.MessageText.Split(' ')[0];
}
}
/// <summary>
/// Returns if this message will be used on the first form or not.
/// </summary>
public bool IsFirstHandler { get; set; } = true;
public bool Handled { get; set; } = false;
public String RawData
{
get
{
return this.RawCallbackData?.CallbackQuery?.Data;
return this.UpdateData?.CallbackQuery?.Data;
}
}
@@ -138,16 +174,9 @@ namespace TelegramBotBase.Base
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public async Task ConfirmAction(String message = "")
public async Task ConfirmAction(String message = "", bool showAlert = false, String urlToOpen = null)
{
try
{
await this.Client.TelegramClient.AnswerCallbackQueryAsync(this.RawCallbackData.CallbackQuery.Id, message);
}
catch
{
}
await this.Device.ConfirmAction(this.UpdateData.CallbackQuery.Id, message, showAlert, urlToOpen);
}
public override async Task DeleteMessage()
@@ -162,17 +191,15 @@ namespace TelegramBotBase.Base
}
}
public MessageResult(Telegram.Bot.Args.MessageEventArgs rawdata)
internal MessageResult()
{
this.RawMessageData = rawdata;
this.Message = rawdata.Message;
}
public MessageResult(Telegram.Bot.Args.CallbackQueryEventArgs callback)
public MessageResult(Telegram.Bot.Types.Update update)
{
this.RawCallbackData = callback;
this.Message = callback.CallbackQuery.Message;
this.UpdateData = update;
}
}
+3 -2
View File
@@ -3,10 +3,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telegram.Bot;
namespace TelegramBotBase.Base
{
public class ResultBase
public class ResultBase : EventArgs
{
public MessageClient Client { get; set; }
@@ -20,7 +21,7 @@ namespace TelegramBotBase.Base
}
}
public Telegram.Bot.Types.Message Message { get; set; }
public virtual Telegram.Bot.Types.Message Message { get; set; }
/// <summary>
/// Deletes the current message
+34
View File
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TelegramBotBase.Base
{
public partial class StateContainer
{
public List<StateEntry> States { get; set; }
public List<long> ChatIds
{
get
{
return States.Where(a => a.DeviceId > 0).Select(a => a.DeviceId).ToList();
}
}
public List<long> GroupIds
{
get
{
return States.Where(a => a.DeviceId < 0).Select(a => a.DeviceId).ToList();
}
}
public StateContainer()
{
this.States = new List<StateEntry>();
}
}
}
+44
View File
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Text;
namespace TelegramBotBase.Base
{
[DebuggerDisplay("Device: {DeviceId}, {FormUri}")]
public class StateEntry
{
/// <summary>
/// Contains the DeviceId of the entry.
/// </summary>
public long DeviceId { get; set; }
/// <summary>
/// Contains the Username (on privat chats) or Group title on groups/channels.
/// </summary>
public String ChatTitle { get; set; }
/// <summary>
/// Contains additional values to save.
/// </summary>
public Dictionary<String, object> Values { get; set; }
/// <summary>
/// Contains the full qualified namespace of the form to used for reload it via reflection.
/// </summary>
public String FormUri {get;set;}
/// <summary>
/// Contains the assembly, where to find that form.
/// </summary>
public String QualifiedName { get; set; }
public StateEntry()
{
this.Values = new Dictionary<string, object>();
}
}
}
+59
View File
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telegram.Bot.Types;
using TelegramBotBase.Sessions;
namespace TelegramBotBase.Base
{
public class UpdateResult : ResultBase
{
public UpdateResult(Update rawData, DeviceSession device)
{
RawData = rawData;
Device = device;
}
/// <summary>
/// Returns the Device/ChatId
/// </summary>
public override long DeviceId
{
get
{
return this.RawData?.Message?.Chat?.Id
?? this.RawData?.CallbackQuery?.Message?.Chat?.Id
?? Device?.DeviceId
?? 0;
}
}
public Update RawData { get; set; }
public override Message Message
{
get
{
return RawData?.Message
?? RawData?.EditedMessage
?? RawData?.ChannelPost
?? RawData?.EditedChannelPost
?? RawData?.CallbackQuery?.Message;
}
}
public DeviceSession Device
{
get;
set;
}
}
}
+183 -260
View File
@@ -3,10 +3,19 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using TelegramBotBase.Args;
using TelegramBotBase.Attributes;
using TelegramBotBase.Base;
using TelegramBotBase.Enums;
using TelegramBotBase.Factories.MessageLoops;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
using TelegramBotBase.Sessions;
namespace TelegramBotBase
@@ -15,8 +24,7 @@ namespace TelegramBotBase
/// Bot base class for full Device/Context and Messagehandling
/// </summary>
/// <typeparam name="T"></typeparam>
public class BotBase<T>
where T : FormBase
public class BotBase
{
public MessageClient Client { get; set; }
@@ -33,7 +41,10 @@ namespace TelegramBotBase
/// <summary>
/// Contains System commands which will be available at everytime and didnt get passed to forms, i.e. /start
/// </summary>
public List<String> SystemCalls { get; set; }
public List<BotCommand> BotCommands { get; set; }
#region "Events"
private EventHandlerList __Events = new EventHandlerList();
@@ -43,114 +54,52 @@ namespace TelegramBotBase
private static object __evSystemCall = new object();
public delegate Task SystemCallEventHandler(object sender, SystemCallEventArgs e);
public delegate Task BotCommandEventHandler(object sender, BotCommandEventArgs e);
private static object __evException = new object();
private static object __evUnhandledCall = new object();
#endregion
/// <summary>
/// Skips all messages during running (good for big delay updates)
/// Enable the SessionState (you need to implement on call forms the IStateForm interface)
/// </summary>
public bool SkipAllMessages { get; set; } = false;
public IStateMachine StateMachine { get; set; }
/// <summary>
/// Loggs all messages and sent them to the event handler
/// Offers functionality to manage the creation process of the start form.
/// </summary>
public bool LogAllMessages { get; set; } = false;
public IStartFormFactory StartFormFactory { get; set; }
/// <summary>
/// How often could a form navigate to another (within one user action/call/message)
/// Contains the message loop factory, which cares about "message-management."
/// </summary>
private const int NavigationMaximum = 10;
public IMessageLoopFactory MessageLoopFactory { get; set; }
/// <summary>
/// Simple start of your Bot with the APIKey
/// All internal used settings.
/// </summary>
/// <param name="apiKey"></param>
public BotBase(String apiKey)
public Dictionary<eSettings, uint> SystemSettings { get; private set; }
public BotBase()
{
this.APIKey = apiKey;
this.SystemSettings = new Dictionary<eSettings, uint>();
this.Client = new Base.MessageClient(this.APIKey);
SetSetting(eSettings.MaxNumberOfRetries, 5);
SetSetting(eSettings.NavigationMaximum, 10);
SetSetting(eSettings.LogAllMessages, false);
SetSetting(eSettings.SkipAllMessages, false);
SetSetting(eSettings.SaveSessionsOnConsoleExit, false);
this.SystemCalls = new List<string>();
this.BotCommands = new List<BotCommand>();
this.Sessions = new SessionBase();
this.Sessions.Client = this.Client;
this.Sessions.BotBase = this;
}
/// <summary>
/// Simple start of your Bot with the APIKey and a proxyAdress
/// </summary>
/// <param name="apiKey"></param>
/// <param name="proxyBaseAddress">i.e. https://127.0.0.1:10000</param>
public BotBase(String apiKey, System.Net.Http.HttpClient proxy)
{
this.APIKey = apiKey;
this.Client = new Base.MessageClient(this.APIKey, proxy);
this.SystemCalls = new List<string>();
this.Sessions = new SessionBase();
this.Sessions.Client = this.Client;
}
/// <summary>
/// Simple start of your Bot with the APIKey and a TelegramBotClient instance.
/// </summary>
/// <param name="apiKey"></param>
/// <param name="client"></param>
public BotBase(String apiKey, TelegramBotClient client)
{
this.APIKey = apiKey;
this.Client = new Base.MessageClient(this.APIKey, client);
this.SystemCalls = new List<string>();
this.Sessions = new SessionBase();
this.Sessions.Client = this.Client;
}
/// <summary>
/// Simple start of your Bot with the APIKey and a proxyAdress
/// </summary>
/// <param name="apiKey"></param>
/// <param name="proxyBaseAddress">i.e. https://127.0.0.1:10000</param>
public BotBase(String apiKey, String proxyBaseAddress)
{
this.APIKey = apiKey;
var url = new Uri(proxyBaseAddress);
this.Client = new Base.MessageClient(this.APIKey, url);
this.SystemCalls = new List<string>();
this.Sessions = new SessionBase();
this.Sessions.Client = this.Client;
}
/// <summary>
/// Simple start of your Bot with the APIKey and a proxyAdress
/// </summary>
/// <param name="apiKey"></param>
/// <param name="proxyHost">i.e. 127.0.0.1</param>
/// <param name="proxyPort">i.e. 10000</param>
public BotBase(String apiKey, String proxyHost, int proxyPort)
{
this.APIKey = apiKey;
this.Client = new Base.MessageClient(this.APIKey, proxyHost, proxyPort);
this.SystemCalls = new List<string>();
this.Sessions = new SessionBase();
this.Sessions.Client = this.Client;
}
/// <summary>
/// Start your Bot
@@ -160,13 +109,61 @@ namespace TelegramBotBase
if (this.Client == null)
return;
this.Client.Message += Client_Message;
this.Client.Action += Client_Action;
this.Client.MessageLoop += Client_MessageLoop;
this.Client.TelegramClient.StartReceiving();
if (this.StateMachine != null)
{
this.Sessions.LoadSessionStates(this.StateMachine);
}
//Enable auto session saving
if (this.GetSetting(eSettings.SaveSessionsOnConsoleExit, false))
{
TelegramBotBase.Tools.Console.SetHandler(() =>
{
this.Sessions.SaveSessionStates();
});
}
DeviceSession.MaxNumberOfRetries = this.GetSetting(eSettings.MaxNumberOfRetries, 5);
this.Client.StartReceiving();
}
private async Task Client_MessageLoop(object sender, UpdateResult e)
{
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
if (ds == null)
{
ds = this.Sessions.StartSession(e.DeviceId).GetAwaiter().GetResult();
e.Device = ds;
ds.LastMessage = e.RawData.Message;
OnSessionBegins(new SessionBeginEventArgs(e.DeviceId, ds));
}
var mr = new MessageResult(e.RawData);
int i = 0;
//Should formulars get navigated (allow maximum of 10, to dont get loops)
do
{
i++;
//Reset navigation
ds.FormSwitched = false;
await MessageLoopFactory.MessageLoop(this, ds, e, mr);
mr.IsFirstHandler = false;
} while (ds.FormSwitched && i < this.GetSetting(eSettings.NavigationMaximum, 10));
}
/// <summary>
/// Stop your Bot
/// </summary>
@@ -175,7 +172,12 @@ namespace TelegramBotBase
if (this.Client == null)
return;
this.Client.TelegramClient.StopReceiving();
this.Client.MessageLoop -= Client_MessageLoop;
this.Client.StopReceiving();
this.Sessions.SaveSessionStates();
}
/// <summary>
@@ -194,197 +196,115 @@ namespace TelegramBotBase
}
}
private void Client_Message(object sender, MessageResult e)
{
if (this.SkipAllMessages)
return;
try
{
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
e.Device = ds;
if (LogAllMessages)
/// <summary>
/// This will invoke the full message loop for the device even when no "userevent" like message or action has been raised.
/// </summary>
/// <param name="DeviceId">Contains the device/chat id of the device to update.</param>
public async Task InvokeMessageLoop(long DeviceId)
{
OnMessage(new MessageIncomeResult(e.DeviceId, ds, e));
var mr = new MessageResult();
await InvokeMessageLoop(DeviceId, mr);
}
ds?.OnMessageReceived(new MessageReceivedEventArgs(e.Message));
Client_TryMessage(sender, e);
}
catch (Telegram.Bot.Exceptions.ApiRequestException ex)
{
}
catch (Exception ex)
{
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
OnException(new SystemExceptionEventArgs(e.Message.Text, ds?.DeviceId ?? -1, ds, ex));
}
}
private async void Client_TryMessage(object sender, MessageResult e)
{
DeviceSession ds = e.Device;
if (ds == null)
{
ds = await this.Sessions.StartSession<T>(e.DeviceId);
e.Device = ds;
ds.LastMessage = e.Message;
OnSessionBegins(new SessionBeginResult(e.DeviceId, ds));
}
ds.LastAction = DateTime.Now;
ds.LastMessage = e.Message;
//Is this a systemcall ?
if (e.IsSystemCall && this.SystemCalls.Contains(e.SystemCommand))
{
var sce = new SystemCallEventArgs(e.SystemCommand, e.SystemCallParameters, e.Message, ds.DeviceId, ds);
await OnSystemCall(sce);
if (sce.Handled)
return;
}
FormBase activeForm = null;
int i = 0;
//Should formulars get navigated (allow maximum of 10, to dont get loops)
do
{
i++;
//Reset navigation
ds.FormSwitched = false;
activeForm = ds.ActiveForm;
//Pre Loading Event
await activeForm.PreLoad(e);
//Loading Event
await activeForm.Load(e);
//Is Attachment ? (Photo, Audio, Video, Contact, Location, Document)
if (e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Contact | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Document | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Location |
e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Photo | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Video | e.Message.Type == Telegram.Bot.Types.Enums.MessageType.Audio)
{
await activeForm.SentData(new DataResult(e));
}
//Render Event
if (!ds.FormSwitched)
await activeForm.Render(e);
} while (ds.FormSwitched && i < NavigationMaximum);
}
private void Client_Action(object sender, MessageResult e)
/// <summary>
/// This will invoke the full message loop for the device even when no "userevent" like message or action has been raised.
/// </summary>
/// <param name="DeviceId">Contains the device/chat id of the device to update.</param>
/// <param name="e"></param>
public async Task InvokeMessageLoop(long DeviceId, MessageResult e)
{
try
{
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
DeviceSession ds = this.Sessions.GetSession(DeviceId);
e.Device = ds;
if (LogAllMessages)
{
OnMessage(new MessageIncomeResult(e.DeviceId, ds, e));
}
Client_TryAction(sender, e);
await MessageLoopFactory.MessageLoop(this, ds, new UpdateResult(e.UpdateData, ds), e);
//await Client_Loop(this, e);
}
catch (Exception ex)
{
DeviceSession ds = this.Sessions.GetSession(e.DeviceId);
OnException(new SystemExceptionEventArgs(e.Message.Text, ds?.DeviceId ?? -1, ds, ex));
DeviceSession ds = this.Sessions.GetSession(DeviceId);
OnException(new SystemExceptionEventArgs(e.Message.Text, DeviceId, ds, ex));
}
}
private async void Client_TryAction(object sender, MessageResult e)
/// <summary>
/// Will get invoke on an unhandled call.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void MessageLoopFactory_UnhandledCall(object sender, UnhandledCallEventArgs e)
{
DeviceSession ds = e.Device;
if (ds == null)
{
ds = await this.Sessions.StartSession<T>(e.DeviceId);
e.Device = ds;
OnUnhandledCall(e);
}
ds.LastAction = DateTime.Now;
ds.LastMessage = e.Message;
FormBase activeForm = null;
int i = 0;
//Should formulars get navigated (allow maximum of 10, to dont get loops)
do
/// <summary>
/// This method will update all local created bot commands to the botfather.
/// </summary>
public async Task UploadBotCommands()
{
i++;
//Reset navigation
ds.FormSwitched = false;
activeForm = ds.ActiveForm;
//Pre Loading Event
await activeForm.PreLoad(e);
//Loading Event
await activeForm.Load(e);
//Action Event
if (!ds.FormSwitched)
{
//Send Action event to controls
await activeForm.ActionControls(e);
//Send Action event to form itself
await activeForm.Action(e);
if (!e.Handled)
{
var uhc = new UnhandledCallEventArgs(e.Message.Text, e.RawData, ds.DeviceId, e.MessageId, e.Message, ds);
OnUnhandledCall(uhc);
if (uhc.Handled)
{
if (ds.FormSwitched)
{
continue;
}
else
{
break;
}
}
await this.Client.SetBotCommands(this.BotCommands);
}
}
//Render Event
if (!ds.FormSwitched)
/// <summary>
/// Could set a variety of settings to improve the bot handling.
/// </summary>
/// <param name="set"></param>
/// <param name="Value"></param>
public void SetSetting(eSettings set, uint Value)
{
await activeForm.RenderControls(e);
await activeForm.Render(e);
this.SystemSettings[set] = Value;
}
} while (ds.FormSwitched && i < NavigationMaximum);
/// <summary>
/// Could set a variety of settings to improve the bot handling.
/// </summary>
/// <param name="set"></param>
/// <param name="Value"></param>
public void SetSetting(eSettings set, bool Value)
{
this.SystemSettings[set] = (Value ? 1u : 0u);
}
/// <summary>
/// Could get the current value of a setting
/// </summary>
/// <param name="set"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public uint GetSetting(eSettings set, uint defaultValue)
{
if (!this.SystemSettings.ContainsKey(set))
return defaultValue;
return this.SystemSettings[set];
}
/// <summary>
/// Could get the current value of a setting
/// </summary>
/// <param name="set"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public bool GetSetting(eSettings set, bool defaultValue)
{
if (!this.SystemSettings.ContainsKey(set))
return defaultValue;
return this.SystemSettings[set] == 0u ? false : true;
}
#region "Events"
/// <summary>
/// Will be called if a session/context gets started
/// </summary>
public event EventHandler<SessionBeginResult> SessionBegins
public event EventHandler<SessionBeginEventArgs> SessionBegins
{
add
{
@@ -396,16 +316,16 @@ namespace TelegramBotBase
}
}
public void OnSessionBegins(SessionBeginResult e)
public void OnSessionBegins(SessionBeginEventArgs e)
{
(this.__Events[__evSessionBegins] as EventHandler<SessionBeginResult>)?.Invoke(this, e);
(this.__Events[__evSessionBegins] as EventHandler<SessionBeginEventArgs>)?.Invoke(this, e);
}
/// <summary>
/// Will be called on incomming message
/// </summary>
public event EventHandler<MessageIncomeResult> Message
public event EventHandler<MessageIncomeEventArgs> Message
{
add
{
@@ -417,22 +337,22 @@ namespace TelegramBotBase
}
}
public void OnMessage(MessageIncomeResult e)
public void OnMessage(MessageIncomeEventArgs e)
{
(this.__Events[__evMessage] as EventHandler<MessageIncomeResult>)?.Invoke(this, e);
(this.__Events[__evMessage] as EventHandler<MessageIncomeEventArgs>)?.Invoke(this, e);
}
/// <summary>
/// Will be called if a system call gets raised
/// Will be called if a bot command gets raised
/// </summary>
public event SystemCallEventHandler SystemCall;
public event BotCommandEventHandler BotCommand;
public async Task OnSystemCall(SystemCallEventArgs e)
public async Task OnBotCommand(BotCommandEventArgs e)
{
if (this.SystemCall != null)
await SystemCall(this, e);
if (this.BotCommand != null)
await BotCommand(this, e);
}
/// <summary>
@@ -476,5 +396,8 @@ namespace TelegramBotBase
(this.__Events[__evUnhandledCall] as EventHandler<UnhandledCallEventArgs>)?.Invoke(this, e);
}
#endregion
}
}
+324
View File
@@ -0,0 +1,324 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using Telegram.Bot;
using Telegram.Bot.Types;
using TelegramBotBase.Base;
using TelegramBotBase.Builder.Interfaces;
using TelegramBotBase.Commands;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
using TelegramBotBase.Localizations;
using TelegramBotBase.States;
namespace TelegramBotBase.Builder
{
public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage, IStartFormSelectionStage, IBuildingStage, INetworkingSelectionStage, IBotCommandsStage, ISessionSerializationStage, ILanguageSelectionStage
{
String _apiKey = null;
IStartFormFactory _factory = null;
MessageClient _client = null;
List<BotCommand> _botcommands = new List<BotCommand>();
IStateMachine _statemachine = null;
IMessageLoopFactory _messageloopfactory = null;
private BotBaseBuilder()
{
}
public static IAPIKeySelectionStage Create()
{
return new BotBaseBuilder();
}
#region "Step 1 (Basic Stuff)"
public IMessageLoopSelectionStage WithAPIKey(string apiKey)
{
this._apiKey = apiKey;
return this;
}
public IBuildingStage QuickStart(string apiKey, Type StartForm)
{
this._apiKey = apiKey;
this._factory = new Factories.DefaultStartFormFactory(StartForm);
DefaultMessageLoop();
NoProxy();
OnlyStart();
NoSerialization();
DefaultLanguage();
return this;
}
public IBuildingStage QuickStart<T>(string apiKey)
where T : FormBase
{
this._apiKey = apiKey;
this._factory = new Factories.DefaultStartFormFactory(typeof(T));
DefaultMessageLoop();
NoProxy();
OnlyStart();
NoSerialization();
DefaultLanguage();
return this;
}
public IBuildingStage QuickStart(string apiKey, IStartFormFactory StartFormFactory)
{
this._apiKey = apiKey;
this._factory = StartFormFactory;
DefaultMessageLoop();
NoProxy();
OnlyStart();
NoSerialization();
DefaultLanguage();
return this;
}
#endregion
#region "Step 2 (Message Loop)"
public IStartFormSelectionStage DefaultMessageLoop()
{
_messageloopfactory = new Factories.MessageLoops.FormBaseMessageLoop();
return this;
}
public IStartFormSelectionStage CustomMessageLoop(IMessageLoopFactory messageLoopClass)
{
_messageloopfactory = messageLoopClass;
return this;
}
public IStartFormSelectionStage CustomMessageLoop<T>()
where T : class, new()
{
_messageloopfactory = typeof(T).GetConstructor(new Type[] { })?.Invoke(new object[] { }) as IMessageLoopFactory;
return this;
}
#endregion
#region "Step 3 (Start Form/Factory)"
public INetworkingSelectionStage WithStartForm(Type startFormClass)
{
this._factory = new Factories.DefaultStartFormFactory(startFormClass);
return this;
}
public INetworkingSelectionStage WithStartForm<T>()
where T : FormBase, new()
{
this._factory = new Factories.DefaultStartFormFactory(typeof(T));
return this;
}
public INetworkingSelectionStage WithStartFormFactory(IStartFormFactory factory)
{
this._factory = factory;
return this;
}
#endregion
#region "Step 4 (Network Settings)"
public IBotCommandsStage WithProxy(string proxyAddress)
{
var url = new Uri(proxyAddress);
_client = new MessageClient(_apiKey, url);
_client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBotCommandsStage NoProxy()
{
_client = new MessageClient(_apiKey);
_client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBotCommandsStage WithBotClient(TelegramBotClient tgclient)
{
_client = new MessageClient(_apiKey, tgclient);
_client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBotCommandsStage WithHostAndPort(string proxyHost, int proxyPort)
{
_client = new MessageClient(_apiKey, proxyHost, proxyPort);
_client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBotCommandsStage WithHttpClient(HttpClient tgclient)
{
_client = new MessageClient(_apiKey, tgclient);
_client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
#endregion
#region "Step 5 (Bot Commands)"
public ISessionSerializationStage NoCommands()
{
return this;
}
public ISessionSerializationStage OnlyStart()
{
_botcommands.Start("Starts the bot");
return this;
}
public ISessionSerializationStage DefaultCommands()
{
_botcommands.Start("Starts the bot");
_botcommands.Help("Should show you some help");
_botcommands.Settings("Should show you some settings");
return this;
}
public ISessionSerializationStage CustomCommands(Action<List<BotCommand>> action)
{
action?.Invoke(_botcommands);
return this;
}
#endregion
#region "Step 6 (Serialization)"
public ILanguageSelectionStage NoSerialization()
{
return this;
}
public ILanguageSelectionStage UseSerialization(IStateMachine machine)
{
this._statemachine = machine;
return this;
}
public ILanguageSelectionStage UseJSON(string path)
{
this._statemachine = new JSONStateMachine(path);
return this;
}
public ILanguageSelectionStage UseSimpleJSON(string path)
{
this._statemachine = new SimpleJSONStateMachine(path);
return this;
}
public ILanguageSelectionStage UseXML(string path)
{
this._statemachine = new XMLStateMachine(path);
return this;
}
#endregion
#region "Step 7 (Language)"
public IBuildingStage DefaultLanguage()
{
return this;
}
public IBuildingStage UseEnglish()
{
Localizations.Default.Language = new Localizations.English();
return this;
}
public IBuildingStage UseGerman()
{
Localizations.Default.Language = new Localizations.German();
return this;
}
public IBuildingStage Custom(Localization language)
{
Localizations.Default.Language = language;
return this;
}
#endregion
public BotBase Build()
{
var bb = new BotBase();
bb.APIKey = _apiKey;
bb.StartFormFactory = _factory;
bb.Client = _client;
bb.Sessions.Client = bb.Client;
bb.BotCommands = _botcommands;
bb.StateMachine = _statemachine;
bb.MessageLoopFactory = _messageloopfactory;
bb.MessageLoopFactory.UnhandledCall += bb.MessageLoopFactory_UnhandledCall;
return bb;
}
}
}
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Text;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Builder.Interfaces
{
public interface IAPIKeySelectionStage
{
/// <summary>
/// Sets the API Key which will be used by the telegram bot client.
/// </summary>
/// <param name="apiKey"></param>
/// <returns></returns>
IMessageLoopSelectionStage WithAPIKey(String apiKey);
/// <summary>
/// Quick and easy way to create a BotBase instance.
/// Uses: DefaultMessageLoop, NoProxy, OnlyStart, NoSerialization, DefaultLanguage
/// </summary>
/// <param name="apiKey"></param>
/// <param name="StartForm"></param>
/// <returns></returns>
IBuildingStage QuickStart(String apiKey, Type StartForm);
/// <summary>
/// Quick and easy way to create a BotBase instance.
/// Uses: DefaultMessageLoop, NoProxy, OnlyStart, NoSerialization, DefaultLanguage
/// </summary>
/// <param name="apiKey"></param>
/// <returns></returns>
IBuildingStage QuickStart<T>(String apiKey) where T : FormBase;
/// <summary>
/// Quick and easy way to create a BotBase instance.
/// Uses: DefaultMessageLoop, NoProxy, OnlyStart, NoSerialization, DefaultLanguage
/// </summary>
/// <param name="apiKey"></param>
/// <param name="StartFormFactory"></param>
/// <returns></returns>
IBuildingStage QuickStart(String apiKey, IStartFormFactory StartFormFactory);
}
}

Some files were not shown because too many files have changed in this diff Show More