alertmanager icon indicating copy to clipboard operation
alertmanager copied to clipboard

Issue with Telegram bot ID

Open basskitty opened this issue 3 years ago • 16 comments

I just updated to Alertmanager 0.24 and tried switching to the new native Telegram integration but stumbled over an issue. My bot has a "-" character in it's ID (I do not know how common this is) which leads to the following error:

alertmanager[10628]: ts=2022-03-28T14:28:45.218Z caller=notify.go:732 level=warn component=dispatcher receiver=telegram_native integration=telegram[0] msg="Notify attempt failed, will retry later" attempts=1 err="telegram: Bad Request: can't parse entities: Character '-' is reserved and must be escaped with the preceding '\\' (400)"

When escaped, there's no error, but also no notifications being sent. My current Telegram integration (Sachet) is working, so the bot and IDs itself are correct.

Also, a minor thing: the api_url parameter seems to be optional, but not providing it leads to a validation error: Checking '/etc/alertmanager/alertmanager.yml' FAILED: missing api_url on telegram_config I had to specify the URL in the receiver config.

receivers:
  - name: 'telegram_native'
    telegram_configs:
    - bot_token: 'token'
      chat_id: ID
      send_resolved: true
      api_url: "https://api.telegram.org"

basskitty avatar Mar 28 '22 14:03 basskitty

This is not a problem with the bot token. The message itself contains the un-escaped character. I was suffering the same until I switched parse_mode to HTML.

The Telegram bot API gives the details on what needs to be escaped.

eworm-de avatar Mar 29 '22 14:03 eworm-de

Yes. Indeed. I ran into this a couple of weeks ago and running the notifier receiver with HTML was my solution as well! :+1: Closing then.

metalmatze avatar Mar 30 '22 09:03 metalmatze

Using a different parse mode is a workaround, not a solution. Please re-open!

eworm-de avatar Mar 30 '22 09:03 eworm-de

Actually, after @roidelapluie pinged me on this: I think we should set the parse_mode: HTML since it doesn't even work with the default telegram template otherwise. If anyone wants to send a PR to feel free to do so. Otherwise, I'll take care of it eventually.

metalmatze avatar Mar 30 '22 09:03 metalmatze

Sure. Opening again, until we have a PR that updates the default parse_mode.

metalmatze avatar Mar 30 '22 09:03 metalmatze

I think just changing the default is not enough. Given that MarkdownV2 and Markdown fail both these should not be allowed at all.

eworm-de avatar Mar 30 '22 10:03 eworm-de

Both are still valid and people are free to change the templates way from an HTML based template to a markdown based template in their environment.

metalmatze avatar Mar 30 '22 10:03 metalmatze

But is there anything doing the escaping?

eworm-de avatar Mar 30 '22 11:03 eworm-de

I'll add a few words

I think the best way is to escape special symbols, like https://github.com/go-telegram-bot-api/telegram-bot-api/blob/fde58e56ae1e2629ee6b4a77a7feb0ddf38340ee/bot.go#L700-L726

UPD: because there is a limit from the Telegram API side https://core.telegram.org/bots/api#markdownv2-style

agvol avatar Apr 09 '22 12:04 agvol

Hey there!

I got an error:

level=warn component=dispatcher receiver=default-telegram integration=telegram[0] msg="Notify attempt failed, will retry later" attempts=1 err="telegram: chat not found (400)"

Which is correct as I did specify chat_id as an positive integer, if I specify it as telegram shows me (negative integer) I get the unmarshall error:

level=error component=configuration msg="Loading configuration file failed" file=/etc/alertmanager/config/alertmanager.yaml err="yaml: unmarshal errors:\n  line 8: cannot unmarshal !!str `-100151...` into int64"

Please advise

Config looks like this:

global:
  resolve_timeout: 5m
receivers:
- name: default-telegram
  telegram_configs:
  - api_url: https://api.telegram.org
    bot_token: <bot_token>
    chat_id: <chat_id_without_quotes_positive_int>
    disable_notifications: false
    http_config:
      follow_redirects: true
    send_resolved: true
    parse_mode: ""
route:
  continue: false
  group_by:
  - job
  group_interval: 5m
  group_wait: 30s
  receiver: default-telegram
  repeat_interval: 4h
  routes:
  - receiver: default-telegram
templates:
- /etc/alertmanager/config/*.tmpl

Tried all the parse modes just in case

xadcoh avatar Apr 28 '22 16:04 xadcoh

Not sure if I should use the positive number, but i am also getting unmarshall error using negative number.

jsalatiel avatar May 02 '22 02:05 jsalatiel

@metalmatze any help on this ? ( avoiding the unmarshall error )

jsalatiel avatar May 17 '22 00:05 jsalatiel

Hey there!

I got an error:

level=warn component=dispatcher receiver=default-telegram integration=telegram[0] msg="Notify attempt failed, will retry later" attempts=1 err="telegram: chat not found (400)"

Which is correct as I did specify chat_id as an positive integer, if I specify it as telegram shows me (negative integer) I get the unmarshall error:

level=error component=configuration msg="Loading configuration file failed" file=/etc/alertmanager/config/alertmanager.yaml err="yaml: unmarshal errors:\n  line 8: cannot unmarshal !!str `-100151...` into int64"

Please advise

Config looks like this:

global:
  resolve_timeout: 5m
receivers:
- name: default-telegram
  telegram_configs:
  - api_url: https://api.telegram.org
    bot_token: <bot_token>
    chat_id: <chat_id_without_quotes_positive_int>
    disable_notifications: false
    http_config:
      follow_redirects: true
    send_resolved: true
    parse_mode: ""
route:
  continue: false
  group_by:
  - job
  group_interval: 5m
  group_wait: 30s
  receiver: default-telegram
  repeat_interval: 4h
  routes:
  - receiver: default-telegram
templates:
- /etc/alertmanager/config/*.tmpl

Tried all the parse modes just in case

Tested with the following configuration and there is no error, my telegram bot was able to send to alets/resolved-alerts to the chat group:

telegram_configs: - send_resolved: true bot_token: XXXXXXXXX:YYYYYYYYYYYYYYYYYYYYYYY chat_id: -6XXXXXXX api_url: https://api.telegram.org parse_mode: ""

Hope the above helps

penghon avatar Jun 19 '22 01:06 penghon

I don't manage to make it work, and the values are correct, since I was using another telegram bot with those tokens.

    - name: tg
      telegram_configs:
      - bot_token: "XXXXXXXXXX:YYYYYYYYYYYYYYYYYYYYYYYYYYYY"
        chat_id: -100XXXXXXXXX
        api_url: "https://api.telegram.org"
        send_resolved: true
        ```

titansmc avatar Jan 30 '24 15:01 titansmc

I had a typo, sorry. It is working like:

    - name: 'tg'
      telegram_configs:
      - bot_token: "YYYYYY:ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
        chat_id: -XXXXXXXXXX
        send_resolved: true

titansmc avatar Jan 31 '24 09:01 titansmc