martomi / chiadog

A watch dog providing a peace in mind that your Chia farm is running smoothly 24/7.
MIT License
457 stars 120 forks source link

Telegram unit tests not passing #107

Closed hpuac closed 3 years ago

hpuac commented 3 years ago

Describe the bug

Hey folks, I created a telegram bot and got my chat ID but when I try to run the unit tests I get the following output:

TELEGRAM_BOT_TOKEN=MYTOKENHERE TELEGRAM_CHAT_ID=MYIDHERE python3 -m unittest tests.notifier.test_telegram_notifier
WARNING:root:Problem sending event to user, code: 400
FWARNING:root:Problem sending event to user, code: 400
WARNING:root:Problem sending event to user, code: 400
FWARNING:root:Problem sending event to user, code: 400
F
======================================================================
FAIL: testTelegramHighPriorityNotifications (tests.notifier.test_telegram_notifier.TestTelegramNotifier)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/myuser/chiadog/tests/notifier/test_telegram_notifier.py", line 38, in testTelegramHighPriorityNotifications
    self.assertTrue(success)
AssertionError: False is not true

======================================================================
FAIL: testTelegramLowPriorityNotifications (tests.notifier.test_telegram_notifier.TestTelegramNotifier)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/myuser/chiadog/tests/notifier/test_telegram_notifier.py", line 28, in testTelegramLowPriorityNotifications
    self.assertTrue(success)
AssertionError: False is not true

======================================================================
FAIL: testTelegramNormalPriorityNotifications (tests.notifier.test_telegram_notifier.TestTelegramNotifier)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/myuser/chiadog/tests/notifier/test_telegram_notifier.py", line 33, in testTelegramNormalPriorityNotifications
    self.assertTrue(success)
AssertionError: False is not true

----------------------------------------------------------------------
Ran 3 tests in 0.370s

FAILED (failures=3)

Do you have any idea what went wrong? The bot token and chat ID was copy pasted so it can't be a typo. And the regular tests (python3 -m unittest) are passing.

Environment:

St3ffn commented 3 years ago

My setup matches yours. For me everything is working like a charm. What I did to make it work.

  1. set the environment variables TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID.

Please keep in mind that you might need single ticks ' instead of double ticks " for the export.

export TELEGRAM_BOT_TOKEN='MySuperSecretToken:asdasdsad'
export TELEGRAM_CHAT_ID='MyChatId'
  1. simply run python3 -m unittest tests.notifier.test_telegram_notifier

If you debug and get and HTTP 400 response you need to double check your env varibales, espacially your TELEGRAM_CHAT_ID.

You can also send a message with the following command to try it out

curl -X POST \
     -H 'Content-Type: application/json' \
     -d '{"chat_id": "${TELEGRAM_CHAT_ID}", "text": "This is a test from curl", "disable_notification": true}' \
     https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage

Also keep in mind that you need to start the bot. You will find out all opened chats of your bot by calling https://api.telegram.org/$TELEGRAM_BOT_TOKEN/getUpdates. Simply result->INDEX->...CHAT->ID

Hopefully this will be helpful

hpuac commented 3 years ago

Also keep in mind that you need to start the bot.

@St3ffn That was the missing piece, I feel so dumb now! Thx a lot! So for anyone running into the same problem: After creating the bot with BotFather you have to click on it (t.me/your_bot link in the BotFather message) and then start it :sweat_smile: