Send Telegram Message Rule

I’m trying to send a Telegram Message if an item was changed. This is the code I have so far but it throws a lot of errors and I can’t seem to find a way to fix that.

when
    Item TelegramMessage changed
then
    val telegramAction = getActions("telegram","telegram:telegramBot:f0848403c8")
    telegramAction.sendTelegram(<ChatId>L, "Test")
end
   1. The method or field rule is undefined; line 1, column 0, length 4
   2. The method or field when is undefined; line 2, column 46, length 4
   3. The method or field changed is undefined; line 3, column 76, length 7
   4. The method or field then is undefined; line 4, column 84, length 4
   5. The method or field end is undefined; line 7, column 224, length 3
   6. This expression is not allowed in this context, since it doesn't cause any side effects.; line 1, column 5, length 38
   7. This expression is not allowed in this context, since it doesn't cause any side effects.; line 3, column 55, length 4
   8. This expression is not allowed in this context, since it doesn't cause any side effects.; line 3, column 60, length 15

It’s executed like this:

I’m using openHAB 3.1.0 if that’s necessary.

The structure that you see when a rule is fully written out:

rule
  when
    ...
  then
    ...
end

Is only for use if you are writing the rules in text files. When you are creating the rules using the UI (as you appear to be doing from the screenshot) then the different parts are taken care of in the different sections of the rule creation wizard. The when is what triggers the rule and you already have that specified by by filling out the When part of the wizard (in this case your When TelegramMessage received a command). So, you don’t need to include the when or specific trigger in the script.

The "then" is also not required because that will be built as part of the UI processing the rule. So, in the end, the only part of the written rule that you need to incorperate into your script is the actual set of statements that get run:

    val telegramAction = getActions("telegram","telegram:telegramBot:f0848403c8")
    telegramAction.sendTelegram(<ChatId>L, "Test")

You’re trying to copy-paste a complete text file rule into the action portion of a GUI based rule? Don’t do that.
The GUI is replacing all the when-then-end stuff so you just want two lines in your script action.

Oh, thank you very much for the fast answer!