Telegram Actions

logo

This block library allows to interact with a Telegram bot though the offered Thing Actions.
You need the Telegram binding and a properly configured bot Thing.

Blocks

Individual messages

image

The first three blocks can send individual messages.

Queries/answers

image

The last two blocks allow to build conversations with the bot through queries and answers. The queries can display buttons and another rule will react to the user’s reply using those buttons.

image

You can use the Telegram: Process Answer rule template to set up such a rule (with Blockly too), offering a complete Blockly set of tools for Telegram.

Changelog

Version 0.1

  • initial release

Resources

uid: telegram
tags: []
props:
  parameters: []
  parameterGroups: []
component: BlockLibrary
config:
  name: Telegram
slots:
  blocks:
    - component: BlockType
      config:
        args0:
          - name: MESSAGE
            type: input_value
          - name: BOT
            type: input_value
        colour: "#2ea5d8"
        helpUrl: ""
        inputsInline: true
        lastDummyAlign0: right
        message0: send on Telegram %1 from bot %2
        nextStatement: ""
        previousStatement: ""
        tooltip: ""
        type: message
      slots:
        code:
          - component: BlockCodeTemplate
            config:
              template: >
                {{utility:things}}.getActions('telegram', {{input:BOT}}).sendTelegram({{input:MESSAGE}});
        toolbox:
          - component: PresetInput
            config:
              fields:
                TEXT: What's up?
              name: MESSAGE
              shadow: true
              type: text
          - component: PresetInput
            config:
              name: BOT
              shadow: true
              type: oh_thing
    - component: BlockType
      config:
        args0:
          - name: ATTACHMENT_TYPE
            type: field_dropdown
            align: right
            options:
              - - photo
                - Photo
              - - animation
                - Animation
              - - video
                - Video
          - name: URL
            type: input_value
            align: right
          - name: CAPTION
            type: input_value
            align: right
          - name: BOT
            type: input_value
            align: right
        lastDummyAlignRight0: true
        colour: "#2ea5d8"
        helpUrl: ""
        inputsInline: false
        lastDummyAlign0: right
        message0: send %1 on Telegram %2 with caption %3 from bot %4
        nextStatement: ""
        previousStatement: ""
        tooltip: ""
        type: message_attachment
      slots:
        code:
          - component: BlockCodeTemplate
            config:
              template: >
                {{utility:things}}.getActions('telegram', {{input:BOT}}).sendTelegram{{field:ATTACHMENT_TYPE}}({{input:URL}}, {{input:CAPTION}});
        toolbox:
          - component: PresetInput
            config:
              fields:
                TEXT: URL or filename
              name: URL
              shadow: true
              type: text
          - component: PresetInput
            config:
              fields:
                TEXT: caption
              name: CAPTION
              shadow: true
              type: text
          - component: PresetInput
            config:
              name: BOT
              shadow: true
              type: oh_thing
    - component: BlockType
      config:
        args0:
          - name: URL
            type: input_value
            align: right
          - name: USERNAME
            type: input_value
            align: right
          - name: PASSWORD
            type: input_value
            align: right
          - name: CAPTION
            type: input_value
            align: right
          - name: BOT
            type: input_value
            align: right
        lastDummyAlignRight0: true
        colour: "#2ea5d8"
        helpUrl: ""
        inputsInline: false
        lastDummyAlign0: right
        message0: send photo on Telegram %1 username %2 password %3 with caption %4 from bot %5
        nextStatement: ""
        previousStatement: ""
        tooltip: ""
        type: message_attachment_photo_with_creds
      slots:
        code:
          - component: BlockCodeTemplate
            config:
              template: >
                {{utility:things}}.getActions('telegram', {{input:BOT}}).sendTelegramPhoto({{input:URL}}, {{input:CAPTION}}, {{input:USERNAME}}, {{input:PASSWORD}});
        toolbox:
          - component: PresetInput
            config:
              fields:
                TEXT: URL or filename
              name: URL
              shadow: true
              type: text
          - component: PresetInput
            config:
              fields:
                TEXT: caption
              name: CAPTION
              shadow: true
              type: text
          - component: PresetInput
            config:
              fields:
                TEXT: user
              name: USERNAME
              shadow: true
              type: text
          - component: PresetInput
            config:
              fields:
                TEXT: pass
              name: PASSWORD
              shadow: true
              type: text
          - component: PresetInput
            config:
              name: BOT
              shadow: true
              type: oh_thing
    - component: Separator
      config:
        gap: 48
    - component: BlockType
      config:
        args0:
          - name: MESSAGE
            type: input_value
            align: right
          - name: BOT
            type: input_value
            align: right
          - name: REPLY_ID
            type: input_value
            align: right
          - name: BUTTONS
            type: field_input
            align: right
        lastDummyAlignRight0: true
        colour: "#2ea5d8"
        helpUrl: ""
        inputsInline: false
        lastDummyAlign0: right
        message0: ask on Telegram %1 from bot %2 use reply id %3 with buttons %4
        nextStatement: ""
        previousStatement: ""
        tooltip: ""
        type: query
      slots:
        code:
          - component: BlockCodeTemplate
            config:
              template: >
                {{utility:things}}.getActions('telegram', {{input:BOT}}).sendTelegramQuery({{input:MESSAGE}}, {{input:REPLY_ID}}, {{field:BUTTONS}});
        toolbox:
          - component: PresetInput
            config:
              fields:
                TEXT: Turn off the lights?
              name: MESSAGE
              shadow: true
              type: text
          - component: PresetField
            config:
              name: BUTTONS
              value: "'Yes', 'No'"
          - component: PresetInput
            config:
              fields:
                TEXT: replyId
              name: REPLY_ID
              shadow: true
              type: text
          - component: PresetInput
            config:
              name: BOT
              shadow: true
              type: oh_thing
    - component: BlockType
      config:
        args0:
          - name: MESSAGE
            type: input_value
            align: right
          - name: REPLY_ID
            type: input_value
            align: right
          - name: BOT
            type: input_value
            align: right
        lastDummyAlignRight0: true
        colour: "#2ea5d8"
        helpUrl: ""
        inputsInline: false
        lastDummyAlign0: right
        lastDummyAlign1: right
        message0: reply on Telegram %1 to reply id %2 from bot %3
        nextStatement: ""
        previousStatement: ""
        tooltip: ""
        type: answer
      slots:
        code:
          - component: BlockCodeTemplate
            config:
              template: >
                {{utility:things}}.getActions('telegram', {{input:BOT}}).sendTelegramAnswer({{input:REPLY_ID}}, {{input:MESSAGE}});
        toolbox:
          - component: PresetInput
            config:
              fields:
                TEXT: Ok!
              name: MESSAGE
              shadow: true
              type: text
          - component: PresetInput
            config:
              fields:
                TEXT: replyId
              name: REPLY_ID
              shadow: true
              type: text
          - component: PresetInput
            config:
              name: BOT
              shadow: true
              type: oh_thing
  utilities:
    - component: UtilityJavaType
      config:
        javaClass: org.openhab.core.model.script.actions.Things
        name: things

9 Likes

@ysc: You are awesome! Thank you very much……Have a nice Christmas!

2 Likes

Really cool :smiley:
But is it possible to add the chatId field?

Hi,

I Installed the telegram actions, however I cannot find them in blockly. Anything else I need to do?

Currently on openHAB 3.2.0 release

thanks
Daniel

delete browser cache

Hey @ysc i have spotted a problem over here, but i am not sure if it is blockly in general or the library. (Therefore cc @stefan.hoehn )

I am trying to add a newline in my telegram messages i want to send.
But when i do this with the \n newline commant it will get esccaped in the javascript generation.

Generated code:

var messageText, i, currentItemLabel, currentItemState;

var things = Java.type('org.openhab.core.model.script.actions.Things');


messageText = 'Tankstellenpreise';
var i_list = Java.from(itemRegistry.getItem('gTankstellenPreise').members);
for (var i_index in i_list) {
  i = i_list[i_index];
  currentItemLabel = i.getLabel();
  currentItemState = i.getState();
  messageText += String(['\\n',currentItemLabel,'*',currentItemState,'* '].join(''));
}
things.getActions('telegram', 'telegram:telegramBot:2ea60ad14c').sendTelegram(messageText);

So the \\n will make the newline get handled as a regular text which produces a little mess.

image

I am not sure if the block templates generate this in general.
Probably we would have to introduce a rawText block that does not get escaped for something like this.

Jerome, that seems to be a known blockly issue that others encountered as well:

https://groups.google.com/g/blockly/c/1g2rfcgm-aM

Basically the solution was to provide a special CRLF block like this one

it would be pretty easy to provide that if we wanted.

1 Like

I think something like this would make sense.
This should be not a too specific usecase.

Yes, a “line break” block would seem to be a reasonable and valued addition to the toolbox!
The non-openHAB categories are not set in stone, we have already added block types to them as we deemed fit - like maps and RGB-to-HSL color conversion.

I have a working block in place in aprivate library already.
Contribution to the repo will be done in the next days. :slight_smile:

Happy to help on the contribution, Jerome.

1 Like

Hi,

i hope anybody can help me:

I use the library for asking and anwsering question via telegram. So far it looks good but there is one problem:

After processing the anwser of a question im sending a reply. The goal is that the question disapears:

The block looks like this:

grafik

But it did not work. The question remains on telegram and i see the following log:

messageId not defined; action skipped.

openHAB 4.1.1

when I use blockly rule to determine thing status and send notification to telegram I have a problem

Script execution of rule with UID ‘scratchpad’ failed: org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (getThing) on org.openhab.core.model.script.actions.Things failed due to: Unknown identifier: getThing

Generated code is here

var currentState;

var things = Java.type('org.openhab.core.model.script.actions.Things');


currentState = things.getThing('yandexstation:scenarios:71769518f3:7f3fda28b7').status;
if (currentState == 'ONLINE') {
  things.getActions('telegram', 'telegram:telegramBot:ce29dc2f29').sendTelegram('✅ Яндекс-сценарии [1] доступны');
} else {
  things.getActions('telegram', 'telegram:telegramBot:ce29dc2f29').sendTelegram('‼️❌ Яндекс-сценарии [1] не доступны');
}

The problem is here

var things = Java.type('org.openhab.core.model.script.actions.Things');

Telegram block generates variable things which already exists in openhab-js and has different methods.

  utilities:
    - component: UtilityJavaType
      config:
        javaClass: org.openhab.core.model.script.actions.Things
        name: things

I renamed the variable to thingsHelper and put it everywhere in {{utility:things}} instead of things
{{utility:things}}

new block code is here

uid: telegram
tags:
  - marketplace:130507
props:
  parameters: []
  parameterGroups: []
timestamp: Jan 11, 2024, 12:07:39 PM
component: BlockLibrary
config:
  name: Telegram
slots:
  blocks:
    - component: BlockType
      config:
        args0:
          - name: MESSAGE
            type: input_value
          - name: BOT
            type: input_value
        colour: "#2ea5d8"
        helpUrl: ""
        inputsInline: true
        lastDummyAlign0: right
        message0: send on Telegram %1 from bot %2
        nextStatement: ""
        previousStatement: ""
        tooltip: ""
        type: message
      slots:
        code:
          - component: BlockCodeTemplate
            config:
              template: >
                {{utility:thingsHelper}}.getActions('telegram', {{input:BOT}}).sendTelegram({{input:MESSAGE}});
        toolbox:
          - component: PresetInput
            config:
              fields:
                TEXT: What's up?
              name: MESSAGE
              shadow: true
              type: text
          - component: PresetInput
            config:
              name: BOT
              shadow: true
              type: oh_thing
    - component: BlockType
      config:
        args0:
          - name: ATTACHMENT_TYPE
            type: field_dropdown
            align: right
            options:
              - - photo
                - Photo
              - - animation
                - Animation
              - - video
                - Video
          - name: URL
            type: input_value
            align: right
          - name: CAPTION
            type: input_value
            align: right
          - name: BOT
            type: input_value
            align: right
        lastDummyAlignRight0: true
        colour: "#2ea5d8"
        helpUrl: ""
        inputsInline: false
        lastDummyAlign0: right
        message0: send %1 on Telegram %2 with caption %3 from bot %4
        nextStatement: ""
        previousStatement: ""
        tooltip: ""
        type: message_attachment
      slots:
        code:
          - component: BlockCodeTemplate
            config:
              template: >
                {{utility:thingsHelper}}.getActions('telegram', {{input:BOT}}).sendTelegram{{field:ATTACHMENT_TYPE}}({{input:URL}}, {{input:CAPTION}});
        toolbox:
          - component: PresetInput
            config:
              fields:
                TEXT: URL or filename
              name: URL
              shadow: true
              type: text
          - component: PresetInput
            config:
              fields:
                TEXT: caption
              name: CAPTION
              shadow: true
              type: text
          - component: PresetInput
            config:
              name: BOT
              shadow: true
              type: oh_thing
    - component: BlockType
      config:
        args0:
          - name: URL
            type: input_value
            align: right
          - name: USERNAME
            type: input_value
            align: right
          - name: PASSWORD
            type: input_value
            align: right
          - name: CAPTION
            type: input_value
            align: right
          - name: BOT
            type: input_value
            align: right
        lastDummyAlignRight0: true
        colour: "#2ea5d8"
        helpUrl: ""
        inputsInline: false
        lastDummyAlign0: right
        message0: send photo on Telegram %1 username %2 password %3 with caption %4 from bot %5
        nextStatement: ""
        previousStatement: ""
        tooltip: ""
        type: message_attachment_photo_with_creds
      slots:
        code:
          - component: BlockCodeTemplate
            config:
              template: >
                {{utility:thingsHelper}}.getActions('telegram', {{input:BOT}}).sendTelegramPhoto({{input:URL}}, {{input:CAPTION}}, {{input:USERNAME}}, {{input:PASSWORD}});
        toolbox:
          - component: PresetInput
            config:
              fields:
                TEXT: URL or filename
              name: URL
              shadow: true
              type: text
          - component: PresetInput
            config:
              fields:
                TEXT: caption
              name: CAPTION
              shadow: true
              type: text
          - component: PresetInput
            config:
              fields:
                TEXT: user
              name: USERNAME
              shadow: true
              type: text
          - component: PresetInput
            config:
              fields:
                TEXT: pass
              name: PASSWORD
              shadow: true
              type: text
          - component: PresetInput
            config:
              name: BOT
              shadow: true
              type: oh_thing
    - component: Separator
      config:
        gap: 48
    - component: BlockType
      config:
        args0:
          - name: MESSAGE
            type: input_value
            align: right
          - name: BOT
            type: input_value
            align: right
          - name: REPLY_ID
            type: input_value
            align: right
          - name: BUTTONS
            type: field_input
            align: right
        lastDummyAlignRight0: true
        colour: "#2ea5d8"
        helpUrl: ""
        inputsInline: false
        lastDummyAlign0: right
        message0: ask on Telegram %1 from bot %2 use reply id %3 with buttons %4
        nextStatement: ""
        previousStatement: ""
        tooltip: ""
        type: query
      slots:
        code:
          - component: BlockCodeTemplate
            config:
              template: >
                {{utility:thingsHelper}}.getActions('telegram', {{input:BOT}}).sendTelegramQuery({{input:MESSAGE}}, {{input:REPLY_ID}}, {{field:BUTTONS}});
        toolbox:
          - component: PresetInput
            config:
              fields:
                TEXT: Turn off the lights?
              name: MESSAGE
              shadow: true
              type: text
          - component: PresetField
            config:
              name: BUTTONS
              value: "'Yes', 'No'"
          - component: PresetInput
            config:
              fields:
                TEXT: replyId
              name: REPLY_ID
              shadow: true
              type: text
          - component: PresetInput
            config:
              name: BOT
              shadow: true
              type: oh_thing
    - component: BlockType
      config:
        args0:
          - name: MESSAGE
            type: input_value
            align: right
          - name: REPLY_ID
            type: input_value
            align: right
          - name: BOT
            type: input_value
            align: right
        lastDummyAlignRight0: true
        colour: "#2ea5d8"
        helpUrl: ""
        inputsInline: false
        lastDummyAlign0: right
        lastDummyAlign1: right
        message0: reply on Telegram %1 to reply id %2 from bot %3
        nextStatement: ""
        previousStatement: ""
        tooltip: ""
        type: answer
      slots:
        code:
          - component: BlockCodeTemplate
            config:
              template: >
                {{utility:thingsHelper}}.getActions('telegram', {{input:BOT}}).sendTelegramAnswer({{input:REPLY_ID}}, {{input:MESSAGE}});
        toolbox:
          - component: PresetInput
            config:
              fields:
                TEXT: Ok!
              name: MESSAGE
              shadow: true
              type: text
          - component: PresetInput
            config:
              fields:
                TEXT: replyId
              name: REPLY_ID
              shadow: true
              type: text
          - component: PresetInput
            config:
              name: BOT
              shadow: true
              type: oh_thing
  utilities:
    - component: UtilityJavaType
      config:
        javaClass: org.openhab.core.model.script.actions.Things
        name: thingsHelper

I get the same with:


Results in:

[WARN ] [nhab.automation.script.ui.9ab45dceca] - CCU change detected.
2024-01-31 21:17:53.523 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (getThing) on org.openhab.core.model.script.actions.Things failed due to: Unknown identifier: getThing

I just digged a little deeper and the solution is to change

things.getThing('homematic:bridge:3014F711A0001F58A992F54E').status

to

things.getThingStatusInfo('homematic:bridge:3014F711A0001F58A992F54E').status)

Unfortunately, this can’t be done in Blockly rules, since they are not editable. So my workaround is to create the rule using blocky, copy the code, replace the function and then insert the code as non-blockly JS script.

you mean you turn the whole rule back to JavaScript?
You can use the inline-script-block where you can enter any JavaScript.