OH3 Make Telegram Bot chat with me

  • Platform information:

    • Hardware: Raspberry Pi 4
    • OS: openhabian 3.0.0
    • Java Runtime Environment: openjdk version “11.0.9”
    • openHAB version: 3.0.0
  • Issue of the topic:

What I have: A TelegramBot Binding

What I want it to do: Chat with me

As a starting point I would like my TelegramBot to answer me, if I send it “hello”. It should answer “Hello, how are you?”

  • Please post configurations (if applicable):
    • Items configuration related to the issue

Here’s my items configuration:

  • Rules code related to the issue

As a beginner, I am assuming, that my problem can/should be solved as a rule. So here is what I tried:

The code for this rule looks as follows:

triggers:
  - id: "1"
    configuration:
      itemName: TelegramBotPatrick_LastMessageText
      command: Hello
    type: core.ItemCommandTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >-2
           val telegramAction = getActions("telegram","telegram:telegramBot:2999fd19f4")
           telegramAction.sendTelegram(460213524L, "what ever you want to send") 
    type: script.ScriptAction

That is my approach, which is currently not working. The Telegram Bot is not answering at all.

Is a rule the right tool here? Can I simply add the send command as an Action or do I need an additional script, which is executed by the Trigger?

1 Like

Yes but you should consider using text files whenever the rule contents gets more complicated then a couple of lines. Please don’t take snapshots but copy/type the code.

That’s exactly what you specified: if you send “Hello”, the bot answers with a static string.
So it IS working, isn’t it.

1 Like

No, it is not, because it is NOT responding at all.

Screen shots are basically unreadable and showing just the list of Items is rarely going to be sufficient.
We potentially need to see all the settings on each of those Items. As it is that teeny tiny screen shot is illegible. I couldn’t even tell you the names of your Items.

Again, screen shots are next to worthless. You found the code tab so why not paste the text here so it can be read?

Is the rule triggering at all? What is the LastMessateText Item linked to? What’s it’s state? Is it changing to that state with a Command or an Update? What is the TelegramBotPatrick linked to? What happens if you command it outside the rule?

Then you fooled us by writing

So take more care please when specifying your problem please.
Few or noone will be willing to help you when they have to start guessing at times.

Other than that, get your guidance to solve the problem alongside the questions that Rich has raised in his response.

Your approach will not work because “TelegramBotPatrick” is a Group Item (as per your second screenshot) and not your Telegram Bot.
As per your first screenshot your Bot is this “telegram:telegramBot:2999fd19f4” and there should be somwhere a chat ID also!
You can use this Code ,

triggers:
  - id: "1"
    configuration:
      itemName: TelegramBot_LastMessageText
      state: Hallo
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >-2
           val telegramAction = getActions("telegram","telegram:telegramBot:2999fd19f4")
           telegramAction.sendTelegram(xxxxxxxxxL,"what ever you want to send") 
    type: script.ScriptAction

xxxxxxxxxxL is your Chat ID with the L on the end!!!

1 Like

Thanks for the replies so far! I will use all your hints in the future.

I updated my code now like you suggested @Rolli.

triggers:
  - id: "1"
    configuration:
      itemName: TelegramBotPatrick_LastMessageText
      command: Hello
    type: core.ItemCommandTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: >-2
           val telegramAction = getActions("telegram","telegram:telegramBot:2999fd19f4")
           telegramAction.sendTelegram(460213524L, "what ever you want to send") 
    type: script.ScriptAction

First of all the action must call a script and can’t be called like the trigger just from an Item. That is good to know.

Unfortunately it is still not working. I send “Hello” to my Bot, but it is not responding anything.

The state of the Item is definitely “Hello”.

Youre Trigger ist still a command, it should be state!
And the Type should be core.ItemStateUpdateTrigger.

See the Code in my last Post!

1 Like

That was it! Thank you so much @Rolli

Another silly question: Is there a list with the available types?

If you are working in the UI, all of the supported events are presented when you create the trigger. So if you select Item Event, pick the Item you will be given a form to fill out to indicate what type of event and other related information (e.g. like what state it changed from and to). You could create one of each type to see what they turn into in the YAML, but you don’t really need to know that type of stuff. That’s kind of the point of the UI. You don’t need to know that selecting Item Event -> some Item -> receives an event translates to core.ItemStateUpdate in the YAML.

But you do need to know the different between an update, command, and change event so that you select the right one. You can look at Rules | openHAB. Anything in that section can be selected as a trigger for the rule in the UI.

1 Like