Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sendToTelegram in proxy and notifier #232

Open
wants to merge 30 commits into
base: dev
Choose a base branch
from

Conversation

phillamy
Copy link
Contributor

@phillamy phillamy commented Nov 4, 2021

Let me know when u have time to take a look

@phillamy
Copy link
Contributor Author

Cyphernode What features do you want to add to your cyphernode

Added feature toggle in setup for Telegram

@Kexkey
Copy link
Collaborator

Kexkey commented Nov 12, 2021

Nice!!

@phillamy
Copy link
Contributor Author

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
Copy link
Collaborator

Kexkey commented Mar 21, 2022

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?

@phillamy
Copy link
Contributor Author

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
Copy link
Collaborator

Kexkey commented Apr 26, 2022

Oops #260 is a prerequisite to this! :-D

@@ -276,11 +276,14 @@ services:
- .env/notifier.env
volumes:
- "<%= logs_datapath %>:/cnlogs"
- "<%= proxy_datapath %>:/proxy/db"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this for pgpass? Let's only mount that specific file... and not in /proxy/db but something more relevant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Will do

@phillamy
Copy link
Contributor Author

phillamy commented May 4, 2022

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
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants