Telegram: Process Answer

logo

IMPORTANT: this rule template builds rules including a Blockly script action involving blocks from the Telegram block library. Make sure you have it installed.

This rule template will build the skeleton of a rule processing answers to a Telegram query.
A query is a message from a bot with buttons similar to this:

Clicking on a button will update items provided they are linked to the proper channels of the Telegram bot Thing.

This rule template, combined with the Telegram block library, can be used to build complex conversations with a Telegram bot.

The rule will build a Blockly action looking like this ({{placeholders}} will be replaced with the actual values you select when creating the rule from the template):

image

You should keep the “reply on Telegram” blocks (change the message as you like) but can add more blocks to the alternative branches of the “if” block to perform actions as desired.
You can even use the “ask on Telegram” block to continue the conversation - just be sure to choose another replyId, and create another rule from this template to respond to this new replyId!

Language: Nashorn JavaScript

Dependencies: see above

Changelog

Version 0.1

  • initial release

Resources

uid: "telegram:answer"
label: "Telegram: Process Answer"
description: React when a query on Telegram was answered.
configDescriptions:
  - context: thing
    description: The Telegram bot Thing
    label: Bot Thing
    name: botThingUid
    required: true
    type: TEXT
    options: []
  - context: item
    description: The item linked to the bot's Reply Id channel
    label: Reply Id Item
    name: replyIdItem
    required: true
    type: TEXT
  - description: The reply Id to react to. Must be the same as the one you specified in the query.
    label: Reply ID
    name: replyId
    required: true
    type: TEXT
  - context: item
    description: The item linked to the bot's lastMessageText
    label: Last Message Item
    name: lastMessageItem
    required: true
    type: TEXT
  - defaultValue: Yes
    description: The answer to look for as the first alternative.
    label: Alternative 1 Answer
    name: alternative1Choice
    required: true
    type: TEXT
  - defaultValue: No
    description: The answer to look for as the second alternative. If you need more alternatives, add a branch to the 'if' block of the action after creating the rule.
    label: Alternative 2 Answer
    name: alternative2Choice
    required: true
    type: TEXT
triggers:
  - id: "1"
    configuration:
      itemName: "{{replyIdItem}}"
      state: "{{replyId}}"
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    label: Process Telegram answer
    description: Blockly script to identify the answer and process it
    configuration:
      blockSource: |
        <xml xmlns="https://developers.google.com/blockly/xml">
          <variables>
            <variable id="]NdHjLpa-(_]Q)uEvvGJ">botThingUid</variable>
            <variable id="3m-I-4g.$.+wA4otUR9p">answer</variable>
            <variable id="@;%RDo@e:+G[MLM#5Qo9">replyId</variable>
          </variables>
          <block type="variables_set" id="$X9%vG|sZkGv61|Ae[;=" x="81" y="40">
            <field name="VAR" id="]NdHjLpa-(_]Q)uEvvGJ">botThingUid</field>
            <value name="VALUE">
              <block type="oh_thing" id="]i8NnRB|=:`Eu4d@oCqG">
                <field name="thingUid">{{botThingUid}}</field>
              </block>
            </value>
            <next>
              <block type="variables_set" id="Y;Nu;v3Y2jXEOu(`2R8B">
                <field name="VAR" id="3m-I-4g.$.+wA4otUR9p">answer</field>
                <value name="VALUE">
                  <block type="oh_getitem_state" id="qrq0u086y%tIOdk^qQma">
                    <value name="itemName">
                      <shadow type="oh_item" id="EEKzhXffN~8D-VR0PwZP">
                        <field name="itemName">{{lastMessageItem}}</field>
                      </shadow>
                    </value>
                  </block>
                </value>
                <next>
                  <block type="variables_set" id="?h8mePt?e^tut^a@k$CK">
                    <field name="VAR" id="@;%RDo@e:+G[MLM#5Qo9">replyId</field>
                    <value name="VALUE">
                      <block type="text" id="L]_7`E#5dp_uph@ua9yY">
                        <field name="TEXT">{{replyId}}</field>
                      </block>
                    </value>
                    <next>
                      <block type="controls_if" id="rLB8FB#hiSS%3BS$q*%C">
                        <mutation elseif="1" else="1"></mutation>
                        <value name="IF0">
                          <block type="logic_compare" id="),/|b(LM#+t9M1^CwjsZ">
                            <field name="OP">EQ</field>
                            <value name="A">
                              <block type="variables_get" id="8XG+(h^?dam`kF^FVV(7">
                                <field name="VAR" id="3m-I-4g.$.+wA4otUR9p">answer</field>
                              </block>
                            </value>
                            <value name="B">
                              <block type="text" id="eiAd$E0wz2y4y2ve{C+?">
                                <field name="TEXT">{{alternative1Choice}}</field>
                              </block>
                            </value>
                          </block>
                        </value>
                        <statement name="DO0">
                          <block type="telegram_answer" id="J*O2%+Q}G7yFmavTUJcB">
                            <value name="MESSAGE">
                              <shadow type="text" id="$=g=]-$1{?B2D!H#sq6O">
                                <field name="TEXT">Ok!</field>
                              </shadow>
                              <block type="text_join" id="5$OP84^l*@mHRFx5]8on" inline="true">
                                <mutation items="2"></mutation>
                                <value name="ADD0">
                                  <block type="text" id="NqDd)E`].@.UV5GlgSka">
                                    <field name="TEXT">You answered: </field>
                                  </block>
                                </value>
                                <value name="ADD1">
                                  <block type="variables_get" id="%JFm4%Edxzz#mop+SoFo">
                                    <field name="VAR" id="3m-I-4g.$.+wA4otUR9p">answer</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <value name="REPLY_ID">
                              <block type="variables_get" id="{h#8V)-Hx-gf=``n#7-X">
                                <field name="VAR" id="@;%RDo@e:+G[MLM#5Qo9">replyId</field>
                              </block>
                            </value>
                            <value name="BOT">
                              <shadow type="oh_thing" id="MCk,03mgcGk4I]0N(jrK">
                                <field name="thingUid">MyThing</field>
                              </shadow>
                              <block type="variables_get" id="D^?g`s|I^5)-Bh$|$kUt">
                                <field name="VAR" id="]NdHjLpa-(_]Q)uEvvGJ">botThingUid</field>
                              </block>
                            </value>
                          </block>
                        </statement>
                        <value name="IF1">
                          <block type="logic_compare" id=",uI]%B[%A74:/FB=Wy~9">
                            <field name="OP">EQ</field>
                            <value name="A">
                              <block type="variables_get" id="vq|P|@/}?bVxRqo*pn*z">
                                <field name="VAR" id="3m-I-4g.$.+wA4otUR9p">answer</field>
                              </block>
                            </value>
                            <value name="B">
                              <block type="text" id="pEDFa2K5SE?pq{}VVqnS">
                                <field name="TEXT">{{alternative2Choice}}</field>
                              </block>
                            </value>
                          </block>
                        </value>
                        <statement name="DO1">
                          <block type="telegram_answer" id="Y%P^M%f#Wi!8pory[|gy">
                            <value name="MESSAGE">
                              <shadow type="text">
                                <field name="TEXT">Ok!</field>
                              </shadow>
                              <block type="text_join" id="lb3ENrWk]RqHL(lM~mwJ" inline="true">
                                <mutation items="2"></mutation>
                                <value name="ADD0">
                                  <block type="text" id="th3?e@FU!.n8z?]P%6w)">
                                    <field name="TEXT">You answered: </field>
                                  </block>
                                </value>
                                <value name="ADD1">
                                  <block type="variables_get" id=")SJX+rNQ~YdVF$NAP;XU">
                                    <field name="VAR" id="3m-I-4g.$.+wA4otUR9p">answer</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <value name="REPLY_ID">
                              <block type="variables_get" id="n0K?hd@DMWvZIjp?yCm3">
                                <field name="VAR" id="@;%RDo@e:+G[MLM#5Qo9">replyId</field>
                              </block>
                            </value>
                            <value name="BOT">
                              <shadow type="oh_thing">
                                <field name="thingUid">MyThing</field>
                              </shadow>
                              <block type="variables_get" id="S+Wg)%DU+oqch9cXw!?h">
                                <field name="VAR" id="]NdHjLpa-(_]Q)uEvvGJ">botThingUid</field>
                              </block>
                            </value>
                          </block>
                        </statement>
                        <statement name="ELSE">
                          <block type="telegram_answer" id="1RvKVI)8}S?rap$aa}o!">
                            <value name="MESSAGE">
                              <shadow type="text">
                                <field name="TEXT">Ok!</field>
                              </shadow>
                              <block type="text_join" id="HD2X|R93R9B:Bf6knIaq" inline="true">
                                <mutation items="2"></mutation>
                                <value name="ADD0">
                                  <block type="text" id=",7yp2H,};$nKL1jC2_iw">
                                    <field name="TEXT">Invalid answer: </field>
                                  </block>
                                </value>
                                <value name="ADD1">
                                  <block type="variables_get" id="l%uo-zo4c#fl?f=X6nPw">
                                    <field name="VAR" id="3m-I-4g.$.+wA4otUR9p">answer</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <value name="REPLY_ID">
                              <block type="variables_get" id="cQrnV*y)Uy!%)Q%q0}`[">
                                <field name="VAR" id="@;%RDo@e:+G[MLM#5Qo9">replyId</field>
                              </block>
                            </value>
                            <value name="BOT">
                              <shadow type="oh_thing">
                                <field name="thingUid">MyThing</field>
                              </shadow>
                              <block type="variables_get" id="lOZ-B!]R}6EpaD!U~H7r">
                                <field name="VAR" id="]NdHjLpa-(_]Q)uEvvGJ">botThingUid</field>
                              </block>
                            </value>
                          </block>
                        </statement>
                      </block>
                    </next>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </xml>      
      type: application/javascript
      script: >
        var botThingUid, answer, replyId;


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



        botThingUid = '{{botThingUid}}';

        answer = itemRegistry.getItem('{{lastMessageItem}}').getState();

        replyId = '{{replyId}}';

        if (answer == '{{alternative1Choice}}') {
          things.getActions('telegram', botThingUid).sendTelegramAnswer(replyId, 'You answered: ' + String(answer));
        } else if (answer == '{{alternative2Choice}}') {
          things.getActions('telegram', botThingUid).sendTelegramAnswer(replyId, 'You answered: ' + String(answer));
        } else {
          things.getActions('telegram', botThingUid).sendTelegramAnswer(replyId, 'Invalid answer: ' + String(answer));
        }
    type: script.ScriptAction

(NOTE: The blockSource XML has been indented for clarity but altering the script will minify it in a single line again)

4 Likes