Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


Telegram bot


jgab

Recommended Posts

This is a simple Telegram library for scenes. There are many examples of using Telegram to send notifications in the forum but this allows for easily writing interactive scenes.

It's still a bit rough and untested but I post it here if someone wants to play with it and maybe build a more competent bot.

 

Please login or register to see this code.

To start a session 

Please login or register to see this code.

'callback' is a function you define that is called with incoming messages

To send message

Please login or register to see this code.

A simple callback can look like this:

Please login or register to see this code.

Here we just echo the message back to the client using the telegram:msg() function.

The 'id' in the callback contains the chat_id and BOT_KEY  that tells us where to send back the message and has to be provided as the first argument to telegram:msg.

The system persists the chat_ids and BOT_KEYS to a fibaro global (telegram._userVar) and we can thus create an id with username and BOT_KEY

Please login or register to see this code.

which is useful if we need to send a notification when no incoming message has arrived yet.

We can also specify a custom pop-up keyboard with the message to allow for selection of pre-defined values.

 

So, this makes it easy to write interactive chat bots to control the home

Please login or register to see this code.

It allows us to 'list' specified devices and globals and their values

Get the value of any device in the home table, and optionally set values

Get the value of fibaro globals, and optionally set predefined values

This is possible to extend this example for more advanced things like starting scenes etc.

Please login or register to see this image.

/monthly_2019_08/List.png.3be8e18f55bbee40acdeddebb056cacc.png" />kitchen.png.a67882209d08cf3ab94f016d5affe771.pngtest.png.41d04c4ae51c7762cb03238974e00510.png

Edited by jgab
  • Like 4
  • Thanks 2
Link to comment
Share on other sites

@jgabThank You very much for your invention. It`s very usefull function for comunication with our HC2 by Telegram bot. Do you know how send photo from cameras instaled in HC2 by Telegram bot ? I try it to do but I don`t know how. ?

Link to comment
Share on other sites

  • Topic Author
  • 2 hours ago, Jacek Stark said:

    @jgabThank You very much for your invention. It`s very usefull function for comunication with our HC2 by Telegram bot. Do you know how send photo from cameras instaled in HC2 by Telegram bot ? I try it to do but I don`t know how. ?

    If we  emailed the photo to a service that could provide a url to the photo we could display it in Telegram. Unfortunately, I'm not aware of any such service (I looked at IFTTT but couldn't find anything).

    Sometimes cameras that you connect to the HC2 has their own service whereas they can upload pictures to somewhere and provide an url. If you can get an URL to the picture (that is visible outside your home) we could send it to the Telegram client.

     

    Link to comment
    Share on other sites

    15 hours ago, Jacek Stark said:

    @jgabThank You very much for your invention. It`s very usefull function for comunication with our HC2 by Telegram bot. Do you know how send photo from cameras instaled in HC2 by Telegram bot ? I try it to do but I don`t know how. ?

     

    It is possible by using a Telegram bot email. Read about it: 

     

    Link to comment
    Share on other sites

  • Topic Author
  • 6 hours ago, FSE said:

     

    It is possible by using a Telegram bot email. Read about it: 

     

     

    Well, it's because this company (that I don't know) has a server that stores your pictures and forward them to telegram...

    ...and pictures will appear in their bot session - it's just a notification service.

    Link to comment
    Share on other sites

    2 minutes ago, jgab said:

     

    Well, it's because this company (that I don't know) has a server that stores your pictures and forward them to telegram...

    ...and pictures will appear in their bot session - it's just a notification service.

    if you do not like to get their bot, still you can build your own, register telegram bot as fibaro user with email and push to this user email photos from cameras.

     

    Link to comment
    Share on other sites

    I know about metode send photo from server by url. I would like to send photo direct from HC2 using metode meta-data.

    Link to comment
    Share on other sites

    • 1 month later...

    Hi

    There is a problem to use your solution. When I started it first I got message
    [DEBUG] 01:56:23: 2019-09-08 01:56:23.237746 [ fatal] LUA error: /opt/fibaro/scenes/4.lua:18: attempt to index field '_users' (a nil value)

    line 18 is first in function _recordUser

    Please login or register to see this code.

    Have you any idea?

    Link to comment
    Share on other sites

  • Topic Author
  • Maybe, did you turn off _persist ? There is a bug there around ~line 32,. It should be:

    Please login or register to see this code.

    Fixed it in original post.

     

    Link to comment
    Share on other sites

    Hi 

    thank you for your help

    No,  _persist=true in initialization of telegram variable

    Also tried your fix, still have same error

    [DEBUG] 19:22:35: 2019-09-08 19:22:35.486166 [ fatal] LUA error: /opt/fibaro/scenes/4.lua:18: attempt to index field '_users' (a nil value)
     

    All modifications I did - added my code for command processing

    Please login or register to see this code.

     

    Edited by KovVlad
    Link to comment
    Share on other sites

  • Topic Author
  •  

    Try

    Please login or register to see this code.

    If I use 

    Please login or register to see this code.

    with the above change your code works for me, but I guess you have another Telegram server. But it should behave the same?

    I'm not an expert on Telegram but try this.

    /J

    Edited by jgab
    • Thanks 1
    Link to comment
    Share on other sites

    1 hour ago, jgab said:

    Try

    Please login or register to see this code.

    Now it works, I think

    Great Thanks

    Seems that there is problem with my callback function, will fix :)

     

    1 hour ago, jgab said:

    If I use 

    Please login or register to see this code.

    with the above change your code works for me, but I guess you have another Telegram server. But it should behave the same?

    I'm not an expert on Telegram but try this.

    Just my proxy, forget

    Edited by KovVlad
    Link to comment
    Share on other sites

    • 2 weeks later...

    JFYI: 

    hint

    Please login or register to see this code.

    Please login or register to see this image.

    /monthly_2019_09/image.png.88a25dc3249543a5774d9860170adb5d.png" />

    Edited by 10der
    Link to comment
    Share on other sites

    • 1 year later...

    I tryied the Battery Telegram and inserted my api.Token from telegram. I stil get only "operation is cancelled. what dit i do rong? ?

    Link to comment
    Share on other sites

    Next error is

     

    [DEBUG] 13:39:06: 2020-11-25 13:39:06.093800 [ fatal] Unknown exception: /usr/share/lua/5.2/json/decode/util.lua:35: unexpected character @ character: 1 0:1 [F] line:
    [DEBUG] 13:39:06: F

    can someone help me please, thx

    Link to comment
    Share on other sites

  • Topic Author
  • On 11/25/2020 at 1:45 PM, Kage said:

    Next error is

     

    [DEBUG] 13:39:06: 2020-11-25 13:39:06.093800 [ fatal] Unknown exception: /usr/share/lua/5.2/json/decode/util.lua:35: unexpected character @ character: 1 0:1 [F] line:
    [DEBUG] 13:39:06: F

    can someone help me please, thx

     

    Sorry, it was almos 1.5 years since I wrote this code. I have retired my HC2 and only develop with the HC3 currently.

    In the original post I wrote "It's still a bit rough and untested but I post it here if someone wants to play with it and maybe build a more competent bot." - but no one seems to have been interested in developing it further...

    I've have started on a Telegram QA for HC3 but not completed it yet as it is not a priority for me at the moment.

    Link to comment
    Share on other sites

    Jan,

    in your QA I see the messages  tyoed in my Bot in HC3

    HC3 shows also Hello 

    Cant test further in proces off migration HC3 to HC3 (I hope that it wil solve my problems)

    //Sjakie

    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Reply to this topic...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...