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

Telegram integration without webhook but with chat-id #689

Open
stevenengland opened this issue Aug 4, 2022 · 12 comments
Open

Telegram integration without webhook but with chat-id #689

stevenengland opened this issue Aug 4, 2022 · 12 comments
Labels

Comments

@stevenengland
Copy link

Hi, I like the selfhosted version of healthchecks. But I don't want a public availability to use the Telegram integration (webhook). It would be so much easier if one could only provide the bot token and an chat id as the target. Uptime Kuma for example does it exactly this way. Healthchecks would then need to call the API endpoint https://core.telegram.org/bots/api#sendmessage with the given data.

@cuu508
Copy link
Member

cuu508 commented Aug 4, 2022

The problem is that sending and receiving the "/start" command requires the Healthchecks instance to be at least partially open to the world, right?

One workaround you can use right now is to create the Telegram integration manually from admin. Put the bot token in the TELEGRAM_TOKEN env var, and create the integration in Django admin. For the integration to work, you will need to set the Project, Kind and Value fields:

image

The value of the id field is the chat id. The bot must already be invited to that chat.

@stevenengland
Copy link
Author

Mh, I might be wrong but I think you don't need the start command at all if the bot is invited to the group and is able to read messages from there that ar not commands (seperate bot setting). The only "problem" then is to find out the chat id by yourself because otherwise you would need to call getUpdates from within healthchecks to automate this too.

Thank you for your workaround. I'll look into this.

@stevenengland
Copy link
Author

Workaround works :) Can you name a dedicated command to realize the same? So I can add it to my IaC Runbook (Ansible).

@cuu508
Copy link
Member

cuu508 commented Aug 4, 2022

There currently isn't a command or API call to automate this.

@cuu508
Copy link
Member

cuu508 commented Aug 4, 2022

PS. Except for running a SQL INSERT query, which would be even more of a workaround :-)

@stevenengland
Copy link
Author

Ok. I thought of something like this which I use to pre-install an admin user to my derived image:

RUN \
    /opt/healthchecks/manage.py migrate && \
    /opt/healthchecks/manage.py shell -c "from django.contrib.auth.models import User; User.objects.create_superuser('ad******', 'ad*********@***********', '********************"

@DerDanilo
Copy link

The value of the id field is the chat id. The bot must already be invited to that chat.

Can we add this as alternative to the Telegram integration setup? When clicking to add the integration that it gives the option to provide the chat-id instead?

@setop
Copy link

setop commented Aug 26, 2022

The thing is, to avoid spam, a bot can't talk to a user directly. The user has to initiate the conversation. That is talk first, send the first message. Hense the usage of "/start" in the onboarding of tg integration of HC.

@stevenengland
Copy link
Author

The thing is, to avoid spam, a bot can't talk to a user directly. The user has to initiate the conversation. That is talk first, send the first message. Hense the usage of "/start" in the onboarding of tg integration of HC.

The /start thing is okay so far. But the Webhook thing should not be the only option to setup Telegram. As mentioned, providing a chat id is all it takes to set up HC <-> Telegram. If someone (like me) would like to set this up without the webhook it is still no problem. Create a group, invite the bot, text around including the /start command, use the bot API to find out the chat id and provide it to HC. That is a bit more manual but does not require webhooks.

@cuu508
Copy link
Member

cuu508 commented Oct 7, 2022

Can we add this as alternative to the Telegram integration setup? When clicking to add the integration that it gives the option to provide the chat-id instead?

I'm not planning to work on this myself, but would accept a PR.

@cuu508 cuu508 changed the title [Feature] - Telegram integration without webhook but with chat-id Telegram integration without webhook but with chat-id Oct 7, 2022
@cuu508 cuu508 added the feature label Oct 7, 2022
@kiler129
Copy link

kiler129 commented Jan 31, 2023

There's an easy workaround for this issue - simply use ... a webhook to Telegram API.

1. Create your bot by messaging @BotFather

  • Start by sending a message /newbot and follow the prompts
  • You will get a bot token looking like 1234567:foobarbaz

2. Get chat_id

  • Visit https://api.telegram.org/bot<BOT_TOKEN>/getUpdates, replacing <BOT_TOKEN> (including < and >) with your token (e.g. 1234567:foobarbaz)
    • You will see a JSON like below (formatter here with JSONLint for clarity, API returns a single line)
    • Your chat_id is the one in chat => id (444 in this example)
     {
         "ok": true,
          "result": [{
     	    "update_id": 111,
     	    "message": {
     		    "message_id": 222,
     		    "from": {
     		  	    //...
     		    },
     		    "chat": {
     		 	    "id": 444,
     			    "first_name": "John",
     			    "last_name": "Doe",
     			    //...
     		    },
     		    //...
    

3. Configure HealthChecks

  • Integrations > Webhook > Add Integration
  • Configure as follows:
    • URL for both:
      • POST
      • https://api.telegram.org/bot<BOT_TOKEN>/sendMessage (replace <BOT_TOKEN> with your token from step 1)
    • Request Headers for both:
    Content-Type: application/json
    
    • Request Body for DOWN:
    {
      "chat_id": <YOUR_CHAT_ID>,
      "text": "❌ <b>HealthChecks:</b> \"$NAME\" FAILED @ $NOW",
      "parse_mode": "HTML",
      "no_webpage": true
    }
    • Request Body for UP:
    {
      "chat_id": <YOUR_CHAT_ID>,
      "text": "✅ <b>HealthChecks:</b> \"$NAME\" OK @ $NOW",
      "parse_mode": "HTML",
      "no_webpage": true
    }
  1. Test it in the integrations tab ;)
    • It should work perfectly
    • If you see an error message stating that it failed with error "400" the JSON has some syntax error - check it with JSONLint
    • Note: @cuu508 - would it be possible to show the API response, at least on the initial test?

Example configuration:
image

@jjscaria
Copy link

jjscaria commented Jun 23, 2023

In addition to the above, you can also add message_thread_id for targeting a topic in a group. You can find topic id in the topic info. It will be the digits after the last /.

Ex: t.me/c/12345678/13

"message_thread_id": 13

All available parameters are here: https://core.telegram.org/bots/api#sendmessage

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

No branches or pull requests

6 participants