cyphernode icon indicating copy to clipboard operation
cyphernode copied to clipboard

Added sendToTelegram in proxy and notifier

Open phillamy opened this issue 4 years ago • 8 comments

Let me know when u have time to take a look

phillamy avatar Nov 04 '21 21:11 phillamy

Cyphernode What features do you want to add to your cyphernode

Added feature toggle in setup for Telegram

phillamy avatar Nov 10 '21 19:11 phillamy

Nice!!

Kexkey avatar Nov 12 '21 02:11 Kexkey

Added TG optional feature + TG bot parameters "URL, API Key, conversation ID)in setup.sh. TG testing is done on startup with a noop call to TG via a Mosquitto_rr.

In order to post a message to TG from CN, use the notifier_telegram function. See cyphernode/proxy_docker/app/tests/test-telegram.sh

TG doc to configure the bot can be found here : doc/TELEGRAM.md Notifier: Moved environnement variables from docker-compose.yaml to notifier.env.

phillamy avatar Nov 22 '21 14:11 phillamy

This is pretty cool, I rebased locally and tested it. Here are some improvements I thought about, let me know what you think:

  • The manual process of creating a bot in TG, creating a group/channel/chat that the bot would join, manually making a curl request to getUpdates and parse the response for the chat_id is a pita.
  • Unfortunately, there's no way to automate the bot creation process, but we can help the Cyphernode operator...
  • For the first version of telegram support, let's assume the operator has a private chat with its bot. The bot will only notify him instead of a group.
  • We could add a script in CN that would automatically do the getUpdates request until the chat_id is parsable.
  • The chat_id would then be saved and telegram notifications would be ready to use.
  • We need to put the chat_id in the database, since we don't want to modify the config files while CN is running. There's a table called cyphernode_props for that.

Ultimately (another enhancement/project), I think it would be a good idea to put all the CN configurations in the database, except for the ones needed for docker to start the services. And an endpoint to reload the configs without restarting CN.


So the operator would have to:

  • /newbot the BotFather
  • Enter bot name, ie cnbot
  • Click the t.me/cnbot link to create a private chat with the bot
  • Say hello to cnbot

Then the getUpdates would return something:

{"ok":true,"result":[{"update_id":42345678,"message":{"message_id":4,"from":{"id":323456789,"is_bot":false,"first_name":"Kexkey","username":"Kexkey","language_code":"en"},"chat":{"id":323456789,"first_name":"Kexkey","username":"Kexkey","type":"private"},"date":1647630018,"text":"hello"}}]}

The chat_id here is 323456789 and that would automatically be inserted in cyphernode_props.

What do you think?

Kexkey avatar Mar 21 '22 21:03 Kexkey

We now have 3 parameters for TG during CN setup: "telegram_bot_url": "https://api.telegram.org/bot", "telegram_api_key": "123", "telegram_chat_id": 123

To make things easier, we could use the default value for the telegram_bot_url - not ask the user and add it into the DB config table you mentionned cyphernode_props.

Setup: Please go to TG App and start chatting with the BotFather. Send the message "/newbot" to create a new Bot:

<<BotFather answer: Done! Congratulations on your new bot. You will find it at t.me/yourbotbot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you've finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.

Use this token to access the HTTP API: 5372720109:AAHy5o4AXE43DXdzDJiFk7ti3N268GX1D04 Keep your token secure and store it safely, it can be used by anyone to control your bot.

Copy this API KEY, go back in the setup and enter it. The user comes back to the setup script and pastes the TG API KEY. The key is saved in the DB.

Next, the scripts actively waits while curling the TG server to get the telegram_chat_id

Please go back into TG, Open a chat with the new bot and say "Wasssup bot" in the chat

The curl is something like https://api.telegram.org/botTELEGRAM_API_KEY/getUpdates. Once everything is working (the message.chat.id is found), saves the chat_id in the DB.

The end

There doesn't seem to be a way around to avoid going back and forth from the setup to TG, back to setup to paste the API KEY and TG again to send a message.

phillamy avatar Apr 12 '22 21:04 phillamy

Oops https://github.com/SatoshiPortal/cyphernode/pull/260 is a prerequisite to this! :-D

Kexkey avatar Apr 26 '22 01:04 Kexkey

For new installations, I added a wait loop to avoid errors in psql log saying Table did not exist - Database was being created In notifier, I added a readloop for new messages that breaks out when config needs to be reloaded.

phillamy avatar May 04 '22 15:05 phillamy

If you have time to take a look at this PR , I cleaned up to tg setup output to look more like start.sh

phillamy avatar May 13 '22 17:05 phillamy