Scripting error

Hello Folks

Can decent coders please advise on fix for the following script..
RB3011UIAS-RM

I used Chatgtp for the following code however I cannot see anything wrong myself.
VS Code with RouterOS (routeros) plugin cannot seem to find any faults during debugging too.
Error : executing script from scheduler failed, please check it manually (Winbox Log)

Idea of this was to use in scheduler to backup router information..
Script was to send DHCP Information via telegram and Email.
In addition it to send backup files - .backup / export.rsc and log.txt file.. and Supout.rif file..

=========
Device Report :
IP Address :
MAC Address :
IP Comment :
Device Identify - Router’s tag
Device Serial :
Current Firmware :
Latest Stable Firmware :
Netwatch State :



Noted : I want to implement telegram chat bot too… for cross interaction.
However i dont seem to manage it…

Please need help with other ideas for future plans.

1 →
However i too wanted to do 2MFA via email/telegram.. Got Google Authenticator on phone.
The idea is to have bot show the following information.
I made a telegram group with topics however could not get bot to send messages to set topics.

DHCP Lease - Active listing remotely of devices online.
Connection Tracking/Rogue IP - Notice of login attempts to router or other section of network ( valid and invalid) - > Username + Password IP address + Time
Logs - Active copy of current log
Internet-Allow list ( Would wanted to use telegram to interact with this list to enable/disable address list )
TrustedIPs - List of active trusted connections
Wake-Device - Want to use wake on lan / send alert notice to Security Camera/gate

2 →
Wireguard to network from external network - NATTED WAN uplink from ISP
Plan to vpn into home network which would allow internet access from there on..

  • Not sure if one can sub-dial another vpn as work allowed VPN from fixed public ip

Once can locate other hardware - was looking to perhaps use L2TP with other mikrotik devices.

3 - > Need more advanced firewalls / scripts
Using 3CX SMB want to access control via VOIP phones / Wiegand Access Panels


# Variables
:local ipAddress "";
:local macAddress "";
:local ipComment "";
:local deviceIdentity [/system identity get name];
:local deviceSerial [/system routerboard get serial-number];
:local currentFirmware [/system routerboard get current-firmware];
:local latestFirmware [/system routerboard get upgrade-firmware];
:local netwatchState "";

# Telegram configuration
:local telegramToken "xxxxxxxxx:xxxxxxxxxxxx"; # Change to API Token
:local chatID "-xxxxxxx"; # Change to Chat ID

# Email configuration
:local emailTo "admin@address.com";   # Replace with your email recipient

# Generate Backup File
:local backupFileName ("router-backup-" . [/system clock get date] . ".backup");
:execute {
    /system backup save name=$backupFileName;
}

# Generate Export File
:local exportFileName ("router-export-" . [/system clock get date] . ".rsc");
:execute {
    /export file=$exportFileName;
}

# Generate Supout File
:local supoutFileName ("router-supout-" . [/system clock get date] . ".rif");
:execute {
    /system sup-output name=$supoutFileName;
}

# Generate Log File
:local logFileName ("router-log-" . [/system clock get date] . ".txt");
:execute {
    /log print file=$logFileName;
}

# Get DHCP Lease information
:foreach lease in=[/ip dhcp-server lease find] do={
    :set ipAddress [/ip dhcp-server lease get $lease address];
    :set macAddress [/ip dhcp-server lease get $lease mac-address];
    :set ipComment [/ip dhcp-server lease get $lease comment];
}

# Get Netwatch State
:foreach state in=[/tool netwatch find] do={
    :set netwatchState [/tool netwatch get $state status];
}

# Compose message body
:local messageBody ("Device Report:\n" .
"IP Address: $ipAddress\n" .
"MAC Address: $macAddress\n" .
"IP Comment: $ipComment\n" .
"Device Identity: $deviceIdentity\n" .
"Device Serial: $deviceSerial\n" .
"Current Firmware: $currentFirmware\n" .
"Latest Stable Firmware: $latestFirmware\n" .
"Netwatch State: $netwatchState");

# Compose email subject
:local emailSubject ("MikroTik Device Report - IP: $ipAddress");

# Send Email with attachments
:do {
    /tool e-mail send to=$emailTo subject=$emailSubject body=$messageBody file=$backupFileName;
    /tool e-mail send to=$emailTo subject=$emailSubject body=$messageBody file=$exportFileName;
    /tool e-mail send to=$emailTo subject=$emailSubject body=$messageBody file=$supoutFileName;
    /tool e-mail send to=$emailTo subject=$emailSubject body=$messageBody file=$logFileName;
    :log info "Email sent successfully with attachments to $emailTo.";
} on-error={
    :log error "Failed to send email with attachments. Check email settings.";
}

# Send Files via Telegram
:local sendFileTelegram do={
    :local filePath [/file get $1 name];
    :local encodedFilePath [:uriEncode $filePath];
    /tool fetch url=("https://api.telegram.org/bot" . $telegramToken . "/sendDocument?chat_id=" . $chatID) keep-result=no http-method=post file=$1;
}

# Send Backup File via Telegram
:do {
    $sendFileTelegram $backupFileName;
} on-error={
    :log error "Failed to send backup file via Telegram.";
}

# Send Export File via Telegram
:do {
    $sendFileTelegram $exportFileName;
} on-error={
    :log error "Failed to send export file via Telegram.";
}

# Send Supout File via Telegram
:do {
    $sendFileTelegram $supoutFileName;
} on-error={
    :log error "Failed to send supout file via Telegram.";
}

# Send Log File via Telegram
:do {
    $sendFileTelegram $logFileName;
} on-error={
    :log error "Failed to send log file via Telegram.";
}

# Cleanup files after sending
:foreach file in=[$backupFileName $exportFileName $supoutFileName $logFileName] do={
    /file remove $file;
}
:log info "Temporary files cleaned up.";

The title is wrong, the correct title is “fix for me SchaitGPT script”

Why don’t you ask ChatGPT for help?

First you get a dog and take it out to s—t on the street, then you ask pedestrians who pass by to clean it up?

Just search the forum for the solution/solutions. There were dozens of that kind questions and solutions. AI just quotes them as it’s own creativity but you should do it by yourself if you want to tailor it to your needs. just to know what are you doing.

Well, “:urlEncode” is not a thing, among other problems - like all the :execute may not be needed. But there a lot of existing backup scripts example already.

Also, VSCode plugin does not do full syntax checking, so it’s be hit-or-miss at spotting errors. Only /system/script/edit in RouterOS terminal will show actual syntax errors.


This is more basic programming thing. What I’d recommend is looking script functions. For example, inside the ChatGPT crap is $sendFileTelegram function. While the ChatGPT uses one function, the rest is just pretty unstructured.
So if you have more ambitious plans, you’re better off learning to structure your code into discrete functions. Then use those functions in various scheduled/on-event/etc other needs beyond backup. By having modular functions, you can test them independently of some larger script. i.e. if you know the parts works, it’s easy to debug when combined for more complex things like you 2FA via telegram. Although I’m pretty sure Google might turn up examples of that too…