Skip to content

Adding support for Telegram as a Notifier (#72)#73

Merged
juftin merged 3 commits intomainfrom
0.3.0
Feb 28, 2022
Merged

Adding support for Telegram as a Notifier (#72)#73
juftin merged 3 commits intomainfrom
0.3.0

Conversation

@juftin
Copy link
Owner

@juftin juftin commented Feb 28, 2022

Linking #72

  • Adding support for Telegram as a Notifier

  • Marking messages as escaped or not

  • Bumping minor version due to adding new feature

  • Fixing linting errors

* Adding support for Telegram as a Notifier

* Marking messages as escaped or not

* Bumping minor version due to adding new feature

* Fixing linting errors
@juftin juftin added the enhancement New feature or request label Feb 28, 2022
@juftin juftin self-assigned this Feb 28, 2022
response = requests.post(url=TelegramConfig.API_ENDPOINT,
headers=telegram_headers,
json=message_json)
if response.status_code != 200:
Copy link
Owner Author

Choose a reason for hiding this comment

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

@stjohnjohnson one follow up question, would it be okay to raise an error here for a successful response?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's certainly okay to do that. I was copying how it was done for PushBullet: https://github.com/juftin/camply/blob/main/camply/notifications/pushbullet.py#L68-L69

But it seems Pushover raises an error: https://github.com/juftin/camply/blob/main/camply/notifications/pushover.py#L69-L71

Copy link
Owner Author

Choose a reason for hiding this comment

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

Gotcha - good catch. I made a couple very small tweaks to the notifiers. Let me know if everything looks good and we'll publish

@stjohnjohnson
Copy link
Contributor

stjohnjohnson commented Feb 28, 2022 via email

@juftin juftin merged commit 7b64c22 into main Feb 28, 2022
@juftin juftin deleted the 0.3.0 branch February 28, 2022 05:53
@Jgunde
Copy link

Jgunde commented Jun 3, 2022

I'm having trouble getting Telegram notifications working. I have version camply 0.3.1 installed through pip. I'm using a Python script. I set os.environ["TELEGRAM_BOT_TOKEN"] and os.environ["TELEGRAM_CHAT_ID"]. I was confused how to get the chat id but I found this: https://codesandbox.io/s/get-telegram-chat-id-q3qkk
But I'm still getting an error: "[ ERROR]: Telegram is not configured properly..."

@juftin
Copy link
Owner Author

juftin commented Jun 3, 2022

I'm having trouble getting Telegram notifications working. I have version camply 0.3.1 installed through pip. I'm using a Python script. I set os.environ["TELEGRAM_BOT_TOKEN"] and os.environ["TELEGRAM_CHAT_ID"]. I was confused how to get the chat id but I found this: https://codesandbox.io/s/get-telegram-chat-id-q3qkk But I'm still getting an error: "[ ERROR]: Telegram is not configured properly..."

Hey @Jgunde that error gets triggered by missing Environment Variables for Telegram: TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID. You mentioned you're setting those with os.environ but where are you doing that exactly? Without knowing a little more my first guess is that you're not properly setting the Environment Variables. Apologies if I'm off here but that's the first thing I would check.

if any([TelegramConfig.BOT_TOKEN is None,
TelegramConfig.BOT_TOKEN == "",
TelegramConfig.CHAT_ID is None,
TelegramConfig.CHAT_ID == ""]):
warning_message = (
"Telegram is not configured properly. To send Telegram messages "
"make sure to run `camply configure` or set the "
"proper environment variables: `TELEGRAM_BOT_TOKEN` and `TELEGRAM_CHAT_ID`.")
logger.error(warning_message)
raise EnvironmentError(warning_message)

If I were running localy (I use Linux and Mac), I would do something like this to test:

export TELEGRAM_BOT_TOKEN="abcdefghijklmnop"
export TELEGRAM_CHAT_ID="1234"
camply campsites \
    --rec-area 2991 \
    --start-date 2022-09-10 \
    --end-date 2022-09-21 \
    --continuous \
    --notifications telegram

When I run that command it actually gets past the error you mentioned and throws a 404 Error (since that's a bad bot token)

Traceback (most recent call last):
  File "/Users/justinflannery/.local/pipx/venvs/camply/lib/python3.7/site-packages/camply/notifications/telegram.py", line 67, in send_message
    response.raise_for_status()
  File "/Users/justinflannery/.local/pipx/venvs/camply/lib/python3.7/site-packages/requests/models.py", line 960, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.telegram.org/botabcdefghijklmnop/sendMessage

@juftin
Copy link
Owner Author

juftin commented Jun 3, 2022

Also one more follow up... I didn't notice that you're using a python script. I'm not exactly sure how it works with imports and os.environ but I'm guessing that maybe you're doing something like

import os
import camply

os.environ["TELEGRAM_CHAT_ID"] = "fasdfahsdjfhsd"

But what you want to do is set the environment variable before importing camply, or even better: set the environment variable on your system before running the python script.

@Jgunde
Copy link

Jgunde commented Jun 3, 2022

It worked! As you mentioned, you need to set the environmental variables BEFORE importing camply.
Here's how I set them:
os.environ["TELEGRAM_BOT_TOKEN"] = "12345:NVIW93h__nfw83nfNei_8h3uf"
os.environ["TELEGRAM_CHAT_ID"] = "678910"

Maybe there's a better way but I used this to find the chat id: https://codesandbox.io/s/get-telegram-chat-id-q3qkk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants