PowerBot or not just Mikrotik…

Hello friends!

Due to the nature of my work, I have to write a little bit of PowerShell. As a result, was born Telegram bot in this language too.

This is a template or “fish”. The filling already depends on your imagination. It was designed for heavy loads, multi-threaded message processing is implemented.

But through -AsJob we didn't go. Everything was done on Runspaces.

Here Here these two approaches are compared, Runspaces really faster.

Let's get to the point. Let's see how it works, I'll try to be brief.

Script Create-Runspace.ps1 creates four execution spaces, one for each handler:

psGetUpdate – receives messages
psMessageHandler – processes text messages
psCallbackHandler – processes button presses
psInlineHandler – processes inline requests

Folder pbScriptBlocks contains code with the logic of each.

psGetUpdate receives updates and puts them in thread-safe hashtable. Each message type has its own table. Through them, threads interact with each other. There are also common tables for debugging and logging.

#очередь сообщений типа Message 
$queueMessageHash = [hashtable]::Synchronized(@{})

#очередь сообщений типа Inline 
$queueInlineHash = [hashtable]::Synchronized(@{}) 

Flow psInlineHandler for each message creates a child execution space. The maximum number is defined in the settings as a parameter inlineRunspacesMax.

The remaining child thread handlers are not created, but can be done in a similar way.

All settings are stored in BotSettings.jsonthere is a script to create it BotSettings.ps1there you need to enter your data and run it.

Handlers are infinite loops that read from the table. queueCommandsHash stop flag.

Such flags are set at the end of the script. Create-Runspace.ps1.

$queueCommandsHash['inlineNonStop']=$false

So put a breakpoint on line 143. Otherwise the bot will start the threads and then stop them immediately.

Function Get-Runspaces deletes streams from memory. I honestly stole it from the Internet and adapted it a little.

Handler logs are saved to a table queueDebugHashat the end they are unloaded into a json file of the same name.

At the start, this form will arrive.

Main window

Main window

When you click on the form buttons, you will see something like this in the console.

Debugging in console

Debugging in console

If you click on Usersthen we will get a list of users in the menu. Search is runningtype the name in English and it will filter the result.

IN InlineHandler.ps1 This is written as an example and has a comment.

# Usage example – delete

It was conceived as an infrastructure management bot.

The concept should be roughly clear from the structure itself.

There is a form for DNS, you can call it manually.

The template turned out to be quite universal.

You can also make a separate thread for sending messages, but that's up to you, whoever needs it…

Code published Here. All the best!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *