SatoshiPortal / cyphernode

Modular Bitcoin full-node microservices API server architecture and utilities toolkit to build scalable, secure and featureful apps and services without trusted third parties
MIT License
362 stars 68 forks source link

Added sendToTelegram in proxy and notifier #232

Open phillamy opened 2 years ago

phillamy commented 2 years ago

Let me know when u have time to take a look

phillamy commented 2 years ago

Cyphernode What features do you want to add to your cyphernode

Added feature toggle in setup for Telegram

Kexkey commented 2 years ago

Nice!!

phillamy commented 2 years ago

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.

Kexkey commented 2 years ago

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

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:

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?

phillamy commented 2 years ago

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.

Kexkey commented 2 years ago

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

phillamy commented 2 years ago

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 commented 2 years ago

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