sms to telegram

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:

:set smsContent "$smsPhone $smsMessage";

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.


Armando.

I try, but this isn’t the problem.

:set smsContent “$smsPhone $smsMessage”;

If i try the script without $smsMessage on the smscontent, i haven’t any problem.
What’s right syntax?
samuele

I am heartbroken. I’m starting to think it’s a bug… i don’t know why after get body message of SMS,it’s ineradicable in this script, but in this

:log info "SMS to Email script started";
delay 2;
:foreach i in=[/tool sms inbox find] do={
/tool e-mail send to=info@********.com subject="$[/tool sms inbox get $i phone] $[/tool sms inbox get $i timestamp]" body=[/tool sms inbox get number=$i message];
/tool sms inbox remove $i;
}
:log info "SMS to Email script complete";

it’s ok.

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.

Armando

Yes of course…
After get $i message, the script freeze, only for it! It read the content of sms but it don’t know what to remove

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

[flash=][/flash][flash=][/flash]Now it’s ok but there is a problem with messages that have the new line in the text

:log info “SMS (Max35 characters) to Telegram script started”;
:local smsPhone;
:local smsMessage;
:local smsContent;

:foreach i in=[/tool sms inbox find] do={
/tool fetch url="> https://api.telegram.org/bot*******************/sendmessage?chat_id=**********&text=$smsContent> ";
:set smsPhone [/tool sms inbox get $i phone];
:set smsMessage [:pick [/tool sms inbox get $i message] 0 35];
:set smsContent “$smsPhone $smsMessage”;

/tool sms inbox remove $i;
}

:log info “SMS to Telegram script complete”;

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.