{"id":24987009,"url":"https://github.com/hitthecodelabs/telegramerroralerts","last_synced_at":"2025-03-29T10:47:33.820Z","repository":{"id":197057101,"uuid":"697870184","full_name":"hitthecodelabs/TelegramErrorAlerts","owner":"hitthecodelabs","description":"An efficient method for logging errors and notifying developers via Telegram when an error occurs","archived":false,"fork":false,"pushed_at":"2023-09-28T18:32:03.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-04T11:44:08.004Z","etag":null,"topics":["apscheduler","error-handling","python","telegram","telegram-api"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hitthecodelabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-09-28T16:31:56.000Z","updated_at":"2023-12-02T02:11:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"cfa180b9-fb58-4ea1-8bda-1cdd1352b7a6","html_url":"https://github.com/hitthecodelabs/TelegramErrorAlerts","commit_stats":null,"previous_names":["hitthecodelabs/pyscheduler_tutorial"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hitthecodelabs%2FTelegramErrorAlerts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hitthecodelabs%2FTelegramErrorAlerts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hitthecodelabs%2FTelegramErrorAlerts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hitthecodelabs%2FTelegramErrorAlerts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hitthecodelabs","download_url":"https://codeload.github.com/hitthecodelabs/TelegramErrorAlerts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246174531,"owners_count":20735413,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apscheduler","error-handling","python","telegram","telegram-api"],"created_at":"2025-02-04T11:35:32.021Z","updated_at":"2025-03-29T10:47:33.787Z","avatar_url":"https://github.com/hitthecodelabs.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Automated Error Handling with Telegram Alerts\n\nThe purpose of this guide is to demonstrate an efficient method for logging errors and notifying developers via Telegram when an error occurs. This method uses the `apscheduler` library to periodically check for updates in the error log and sends notifications using the `python-telegram-bot` library.\n\n## Prerequisites\n\n1. Python 3.x installed.\n2. `apscheduler` library installed.\n3. `python-telegram-bot` library installed.\n4. A Telegram Bot Token and a channel ID (or user ID) to send messages.\n\n## Steps:\n\n1. **Error Logging**:\n   Whenever an error occurs in your code, catch the exception and save the detailed error message into a `.txt` file.\n\n```python\nimport sys\nimport traceback\n\ntry:\n    ### code\nexcept Exception as e:\n    exc_type, exc_obj, exc_tb = sys.exc_info()\n    error_l = exc_tb.tb_lineno\n    cadena_error = str(exc_type) + \" =\u003e \" + str(exc_obj)\n    error_info = traceback.format_exc()\n\n    # Append the error details to the error_log.txt\n    with open('error_log.txt', 'a') as f:\n        f.write(cadena_error + \"\\n\" + error_info + \"\\n\\n\")\n```\n\n2. **Initialize the Telegram Bot**:\n   Define a function to send messages using the Telegram Bot. Replace `token`, `chat_id`, and other parameters with your own.\n\n```python\nimport telegram\n\ndef Bot():\n    token = \"YOUR_TELEGRAM_BOT_TOKEN\"\n    bot = telegram.Bot(token=token)\n\n    # Check for new error messages\n    with open('error_log.txt', 'r') as f:\n        error_messages = f.readlines()\n\n    # If new errors exist, send them to the Telegram channel\n    for error_msg in error_messages:\n        bot.send_message(chat_id='YOUR_CHANNEL_OR_USER_ID', text=error_msg)\n\n    # Clear the error log\n    with open('error_log.txt', 'w') as f:\n        f.truncate()\n```\n\n## How to Find the Telegram Channel ID\n\nTo find the ID of a Telegram channel (or group/user):\n\n1. Start a conversation with your bot or add it to the desired channel.\n2. Visit the following URL in your browser:\n\n```\nhttps://api.telegram.org/botYOUR_TELEGRAM_BOT_TOKEN/getupdates\n```\n\nReplace `YOUR_TELEGRAM_BOT_TOKEN` with your actual bot token.\n\n3. In the resulting JSON output, locate the `chat` object. The `id` inside this object is the channel ID. It will look something like this:\n\n```json\n\"chat\": {\n    \"id\": -1101845415028,\n    ...\n}\n```\n\n4. Use this `id` as the `chat_id` in your code.\n\n3. **Schedule the Bot to Run Periodically**:\n   Use `apscheduler` to run the `Bot` function every 10 minutes (or 600 seconds).\n\n```python\nfrom apscheduler.schedulers.blocking import BlockingScheduler\n\nscheduler = BlockingScheduler()\nscheduler.add_job(Bot, 'interval', seconds=600)\nscheduler.start()\n```\n\n## Running the Code\n\n1. Replace placeholders like `YOUR_TELEGRAM_BOT_TOKEN` and `YOUR_CHANNEL_OR_USER_ID` with your actual values.\n2. Save the combined code in a Python script, e.g., `error_handler.py`.\n3. Run the script using the command: `python error_handler.py`.\n\n## Conclusion\n\nThis setup allows for automated error logging and instant notifications via Telegram. It's an efficient way to stay updated on issues that might arise in your application and resolve them promptly. \n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhitthecodelabs%2Ftelegramerroralerts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhitthecodelabs%2Ftelegramerroralerts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhitthecodelabs%2Ftelegramerroralerts/lists"}