Welcome to the Athom Community Forum! This is a place to share ideas, ask for help and discuss about Homey in general.

Please read Welcome to the forum to get started.

Add a way to cummunicate with a Telegram Bot

Hi,

On my Fibaro system I use a several Telegram Bots to receive logs and alerts. It's easy to setup and easy to use!
(see http://forum.fibaro.com/index.php?/topic/20581-sending-messages-from-your-hc2-to-your-telegram-account/ for more information)

To bad I don't have the programming skills to make one by my self, but I love to see this implemented!

If you have any questions regarding setting up a Telegram Bot, just ask.

Have a nice weekend!
«1345678

Comments

  • I think its relatively easy to make! That sad, i don't have any skills either. did some search and most of the programming  is already done.

    https://github.com/yagop/node-telegram-bot-api

    Would be nice to have!
  • jordenjorden Member
    I have almost no experience with Telegram, but looked into this. Was looking into a way to have Homey report stuff to me or execute stuff by sending commands using Telegram.

    However, the creation of a bot on the website of Telegram failed when I tried... no matter what I did. So at that moment I abandoned my ideas :)
  • Just created one! very easy, just install telegram and start talking to the botfather. So with your skills it has to take a few minutes to create an app @jorden ; B) o:) 

    I would love to test it :smile: 


  • jordenjorden Member
    Looking at it now, I think I didn't want to create a bot... but an application instead, so you can send messages to Telegram from Homey. But since that's also possible using the Homey app, I didn't proceed after the website of Telegram failed me :)

    A bot is a whole different case. Haven't looked into that extensively yet, but I think there are a lot of possibilities.

    What kind of stuff would you like to do with the Telegram bot? I myself was thinking of using commands, that can be linked to triggers.
    For example, you send the Telegram bot:
    /trigger test123 321test

    And then in Homey you can have a Telegram trigger on 'test123' with a token 'data1' that contains 321test.


    But I'd love to hear your own ideas!
  • VonkenboerVonkenboer Member
    edited July 2016
    A small how-to then:
    1 - In your Telegram messenger app or desktop software search for BotFather.
    2 - Select Botfather for give it instructions. These instructions are summarized with the /-button
    3 - Create a new bot with /newbot. It will ask for a name and a username (must end with .....bot).
    4 - When created a <token> is given to you. Save this somewhere.
    5 - Set your privacy settings with /setprivacy (is default on enabled, which is ok, but just to be sure....)
    6 - Search for new bot with the username.
    7 - Select begin or type /start
    8 - Send the bot a message (this is crucial!)
    9 - In a webbrowser get your user ID by sending: https://api.telegram.org/bot<token>/getUpdates
    10 - Get your user-ID from json output of the message you send to the bot.
    11 - Test it in your browser by sending a example in your browser:
    https://api.telegram.org/bot<token>/sendMessage?chat_id=<user-ID>&text=<text>
    That's all you need! To communicate with the bot you need the token and to get messages, only for you, you need your user-ID (also called chat-ID)

    If you want to /deletebot or change the settings, this done by the BotFather. When you don't know your token you can /revoke it and get a /newtoken

    Hope this will help.
  • jordenjorden Member
    @Vonkenboer thanks, that indeed is all I need in a nutshell :) I could even generate webhooks, so it connects to the Athom webhook and we would only need one bot to access all Homeys..

    Now just need some ideas of use cases.
  • VonkenboerVonkenboer Member
    edited July 2016
    jorden said:
    A bot is a whole different case. Haven't looked into that extensively yet, but I think there are a lot of possibilities.

    What kind of stuff would you like to do with the Telegram bot? I myself was thinking of using commands, that can be linked to triggers.
    For example, you send the Telegram bot:
    /trigger test123 321test

    At the moment I use it for all my communication (only text messages) from the Fibaro HC2. Like logs en states, on different bots (one with popup/sound/vibration and one silent just for logs) .

    It would be nice to have the ability to send text to different bots to begin with. Secondly pictures or video's  (from ip-cams) and the ability to send instruction to the Homey (Telegrambots can use webhooks).

    For more info: https://core.telegram.org/bots/api

    Thanks for looking into this. I hope I got you interested... ;)
  • jordenjorden Member
    Got working on this already. Not completely sure yet where to go, but for now I have this:

    - The app on Homey sets up a unique identification key.
    - You need to add the bot and send a message (for example, /register uniquekey12345) to the bot to register your Homey to it.
    - After that, you can send any message to the bot in the same chat to get the message in Homey.
    - I'm working on the possibility to send a message to the Telegram chat from inside Homey, after registering Homey to the bot.

    Any requests?  :#
  • jorden said:
    Got working on this already. Not completely sure yet where to go, but for now I have this:

    - The app on Homey sets up a unique identification key.
    - You need to add the bot and send a message (for example, /register uniquekey12345) to the bot to register your Homey to it.
    - After that, you can send any message to the bot in the same chat to get the message in Homey.
    - I'm working on the possibility to send a message to the Telegram chat from inside Homey, after registering Homey to the bot.

    Any requests?  :#
    Nice! Thanks for taking time and effort to get this working!

    BTW, what do you mean "The app on Homey sets up a unique identification key"? Do you mean the bot-token can be inserted?
  • jordenjorden Member
    edited July 2016
    No, I decided to make one bot for all users. Which means less work for users to setup.

    But I needed to find a way to connect a user ID on telegram to a Homey. So by setting up a unique ID on Homey, and then 'registering' that ID on the chat, that is possible. Homey will remember which user it should listen to on the chat :)

    The source code will be open so it can be used easily to create a new app if you would prefer it working differently.
  • VonkenboerVonkenboer Member
    edited July 2016
    jorden said:
    No, I decided to make one bot for all users. Which means less work for users to setup.

    But I needed to find a way to connect a user ID on telegram to a Homey. So by setting up a unique ID on Homey, and then 'registering' that ID on the chat, that is possible. Homey will remember which user it should listen to on the chat

    The source code will be open so it can be used easily to create a new app if you would prefer it working differently.
    I don't think that's a wise decision, IMHO, because the bot-owner could see all communications. Please add an option to choose your own bot. Personally it would be a reason not to use your app....

    Please keep in mind privacy and security as fundamental. Just my opinion.
  • jordenjorden Member
    I'll think about it! I think this way is the easiest way for users, but I think I'll create an 'advanced option' so you can enter the bot ID of your own bot. It's not that hard to create it I think. Just have to keep in mind that you will need to setup a webhook to the Athom webhook as well, which is another step to set up as a user, but then again, it's not that hard to do.
  • VonkenboerVonkenboer Member
    edited July 2016
    Thanks jorden for your understanding and good work. It's highly appreciated!

    The reason I mentioned privacy and security is to minimize the consequences when something goes wrong or when breached.

    Can you explain why you need a webhook? Is this used for receiving instructions from the Telegram-bot to the Homey?

  • jordenjorden Member
    The webhook is the best way for 'immediate action'.

    For example:
    If I need to know which messages were received, I would need to 'poll' Telegram: https://api.telegram.org/bot<token>/getUpdates
    I could do that every x seconds from Homey, for example every 10 seconds.

    However, then I have to do 'something' from every Homey that has the app installed, 6 times a minute, 360 times an hour, 8640 times a day... while probably you will only need it about 0 - 10 times a day when a message is sent.
    Besides that, it will take up to 10 seconds to get the message to the Homey.

    By using a webhook, it is far more optimised:
    When Telegram receives a message sent to the bot, it will hook into the webhook of Athom which will then send it to the right Homey.
    Before one second has passed, I can send back an answer. I just tested that, a ping-pong mechanism is already in place :) (Send 'ping' to the bot, will be received by Homey, Homey sends back 'pong' which will be received in the Telegram chat).

    I am making it dynamic now, so you can enter your own bot token which has preference over the 'general' bot token. Will write up some instructions on the settings page.
  • Clear explanation! I'm impressed!
  • jordenjorden Member
    Got it working with custom bots, including the description.

    Next thing is that I need to make sure that you can 'unregister' from the bot from the settings page, I'm already half way there. But it will be a lot of testing still.

    I expect to be able to submit it to the app store by the end of the week!
  • I just registered to github and saw your first Telegram version. I really don't know (yet) how to use github or make/use an application, so I'm try to learn how things are done. At a first glance, I have a lot, very lot, very very very lot to learn! I'm complete noob... ;)

    It's fun looking over your shoulder to watch things evolve though.. :smile: 
  • jordenjorden Member
    Actually, you need to do a lot to get this app working... the Athom webhook client ID and secret, and Telegram token are all "hidden" on Github (Not uploaded at all). When I add the app to the app store, I will have to enter these details into the app store because they are secret and only work in the background.

    I think I got it working, so tomorrow I will do another test with adding and deleting a custom bot, and if that works it will be submitted to the app store for approval.
  • Wow! I'm very impressed! This is way over my head ...
  • jordenjorden Member
    Although not 100% streamlined on the settings page, I think it's good enough for version 0.0.1 so I have submitted it to the app store for approval.
  • spoellyspoelly Member
    edited July 2016
    very nice! looking forward trying the app.
  • aloftaloft Member
    Great work, looking forward to it as well. 
  • @jorden great you picked up this project so soon! Looking forward to test it.

    Already registered my own bot.
  • jorden said:
    Although not 100% streamlined on the settings page, I think it's good enough for version 0.0.1 so I have submitted it to the app store for approval.
    Nice!

    Just to be a pain in the ass ;) Can your app be used for more then one bot?
  • jordenjorden Member
    No, currently just one bot. Perhaps in the future I will update it so you can have more bots than one. And ofcourse, you are free to play around with the source code at https://github.com/jordenc/org.telegram.api.bot (just give me a heads up and I'll tell you how to fully set it up with the Athom webhooks and a hidden file that contains info on the Athom webhook and your 'default' bot ID... both are required for testing/running)
  • jorden said:
    No, currently just one bot. Perhaps in the future I will update it so you can have more bots than one. And ofcourse, you are free to play around with the source code at https://github.com/jordenc/org.telegram.api.bot (just give me a heads up and I'll tell you how to fully set it up with the Athom webhooks and a hidden file that contains info on the Athom webhook and your 'default' bot ID... both are required for testing/running)
    Before I can play around with your source code I have to learn, from the beginning, how to write and maintain node.js coding..... :)


    Noob question: Isn't it possible to install apps in Homey twice?

  • jordenjorden Member
    Only if you make a copy of the app and rename it (in app.json only I guess)
  • Okay, clear. Thanks!
  • aloftaloft Member
    jorden said:
    Only if you make a copy of the app and rename it (in app.json only I guess)
    Yes, that works perfectly. I use it for development, just rename it in app.json and it will appear as a second app. 
  • jordenjorden Member
    The app is published, have fun!
Sign In or Register to comment.