hi, i’ve a problem indefinite with this script that i created:
:log info "SMS to Telegram script started";
:local smsPhone;
:local smsMessage;
:local smsContent;
:foreach i in=[/tool sms inbox find] do={
:set smsPhone [/tool sms inbox get $i phone];
:set smsMessage [/tool sms inbox get $i message];
:set smsContent "$smsPhone $smsMessage";
/tool fetch url="https://api.telegram.org/botXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/sendmessage?chat_id=-XXXXXXXXX&text=$smsContent";
}
/tool sms inbox remove $i;
:log info "SMS to Telegram script complete";
When the script start, telegram massage is ok but the sms into inbox folder is stil present, and in log info “SMS to Telegram script complete” does not appear.
In the inbox folder, through Winbox interface, there is the sms and it’s no easy to delete.
Where is the problem?
I think that is here:
From your code, this instruction /tool sms inbox remove $i; should be inside the foreach cycle, as $i it’s valid inside.
You have it outside the curly bracket, so the removal does not happen; move it one line up inside the curly bracket.
OK, in the second code you have the remove statement inside the foreach, which is what I mentioned in the previous message.
Have you tried to modify your original code like this below ?
:log info "SMS to Telegram script started";
:local smsPhone;
:local smsMessage;
:local smsContent;
:foreach i in=[/tool sms inbox find] do={
:set smsPhone [/tool sms inbox get $i phone];
:set smsMessage [/tool sms inbox get $i message];
:set smsContent "$smsPhone $smsMessage";
/tool fetch url="https://api.telegram.org/botXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/sendmessage?chat_id=-XXXXXXXXX&text=$smsContent";
/tool sms inbox remove $i;
}
:log info "SMS to Telegram script complete";
Because every line that contains $i must be inside the foreach cycle, like you did in your second code when using the email.
Ok, I don’t use this sms feature so I don’t have experience with specific sms option.
Usually I send messages directly to Telegram without storing them anywhere else, as I don’t use any sms or other sort of messaging.
Armando
I guess you have to do some urlencoding for your sms message…
If you want a working solution have a look at this: RouterOS Scripts - Forward received SMS
This requires the installation of global scripts on top, see main README.