{"id":13486739,"url":"https://github.com/sashgorokhov/python-telegram-handler","last_synced_at":"2025-03-27T21:30:54.363Z","repository":{"id":51130542,"uuid":"56550225","full_name":"sashgorokhov/python-telegram-handler","owner":"sashgorokhov","description":"A python logging handler that sends logs via Telegram Bot Api.","archived":true,"fork":false,"pushed_at":"2024-02-09T11:13:14.000Z","size":37,"stargazers_count":91,"open_issues_count":7,"forks_count":29,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-19T07:41:30.873Z","etag":null,"topics":["logging","python","telegram"],"latest_commit_sha":null,"homepage":null,"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/sashgorokhov.png","metadata":{"files":{"readme":"README.rst","changelog":"HISTORY.rst","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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-04-18T23:51:35.000Z","updated_at":"2024-09-03T18:11:35.000Z","dependencies_parsed_at":"2024-06-18T21:53:04.312Z","dependency_job_id":null,"html_url":"https://github.com/sashgorokhov/python-telegram-handler","commit_stats":{"total_commits":37,"total_committers":6,"mean_commits":6.166666666666667,"dds":0.1351351351351351,"last_synced_commit":"9df6b6c63bef95b25cd794e2f6dd21ac6591d0fa"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sashgorokhov%2Fpython-telegram-handler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sashgorokhov%2Fpython-telegram-handler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sashgorokhov%2Fpython-telegram-handler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sashgorokhov%2Fpython-telegram-handler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sashgorokhov","download_url":"https://codeload.github.com/sashgorokhov/python-telegram-handler/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245927155,"owners_count":20695178,"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":["logging","python","telegram"],"created_at":"2024-07-31T18:00:50.483Z","updated_at":"2025-03-27T21:30:54.034Z","avatar_url":"https://github.com/sashgorokhov.png","language":"Python","funding_links":[],"categories":["Python","Bots","一般机器人（bots）"],"sub_categories":["Bot Libs","机器人类库"],"readme":"python-telegram-handler\n***********************\n\n.. image:: https://img.shields.io/pypi/status/python-telegram-handler.svg\n    :target: https://github.com/sashgorokhov/python-telegram-handler\n\n.. image:: https://img.shields.io/pypi/pyversions/python-telegram-handler.svg\n    :target: https://pypi.python.org/pypi/python-telegram-handler\n\n.. image:: https://badge.fury.io/py/python-telegram-handler.svg \n    :target: https://badge.fury.io/py/python-telegram-handler \n\n.. image:: https://travis-ci.org/sashgorokhov/python-telegram-handler.svg?branch=master \n    :target: https://travis-ci.org/sashgorokhov/python-telegram-handler \n\n.. image:: https://codecov.io/github/sashgorokhov/python-telegram-handler/coverage.svg?branch=master \n    :target: https://codecov.io/github/sashgorokhov/python-telegram-handler?branch=master \n\n.. image:: https://codeclimate.com/github/sashgorokhov/python-telegram-handler/badges/gpa.svg\n   :target: https://codeclimate.com/github/sashgorokhov/python-telegram-handler\n   :alt: Code Climate\n\n.. image:: https://img.shields.io/github/license/sashgorokhov/python-telegram-handler.svg \n    :target: https://raw.githubusercontent.com/sashgorokhov/python-telegram-handler/master/LICENSE \n\n\nA python logging handler that sends logs via Telegram Bot Api\n\nInstallation\n============\n\nVia pip:\n\n.. code-block:: shell\n\n    pip install python-telegram-handler\n\nUsage\n=====\n\nRegister a new telegram bot and obtain a ``authentication token``. (Instructions here https://core.telegram.org/bots#3-how-do-i-create-a-bot)\n\nAfter that, you must obtain a ``chat_id``. You can do that using included simple script. Start a new conversation with newly created bot, write something to it (it is important to initiate conversation first).\n\nAlso, there is an ability for handler to automatically retrieve ``chat_id``. This will be done on handler initialization. But you still have to start a conversation with bot. Be aware: if program stops, server restarts, or something else will happen - handler will try to retrieve chat id from telegram, and may fail, if it will not find a NEW message from you. So i recommend you to use a script below for obtaining chat id. \n\nThen run a command:\n\n.. code-block:: shell\n\n    python -m telegram_handler \u003cyour token here\u003e\n    \nIf all went ok, a ``chat_id`` will be printed to stdout.\n\nUsing ``token`` and ``chat_id``, configure log handler in your desired way.\nFor example, using dictConfig:\n\n.. code-block:: python\n\n        {\n            'version': 1,\n            'handlers': {\n                'telegram': {\n                    'class': 'telegram_handler.TelegramHandler',\n                    'token': 'your token',\n                    'chat_id': 'chat id'\n                }\n            },\n            'loggers': {\n                'my_logger': {\n                    'handlers': ['telegram'],\n                    'level': 'DEBUG'\n                }\n            }\n        }\n\nFormatting\n==========\n\nCurrently the format of sent messages is very simple: ``\u003ccode\u003e%(asctime)s\u003c/code\u003e \u003cb\u003e%(levelname)s\u003c/b\u003e\\nFrom %(name)s:%(funcName)s\\n%(message)s``\nException traceback will be formatted as pre-formatted fixed-width code block. (https://core.telegram.org/bots/api#html-style)\n\nIf you want to tweak it, configure a ``telegram_handler.HtmlFormatter`` with your desired format string.\nUsing a dictConfig:\n\n.. code-block:: python\n        \n        ...\n        {\n            'formatters': {\n                'telegram': {\n                    'class': 'telegram_handler.HtmlFormatter',\n                    'fmt': '%(levelname)s %(message)s'\n                }\n            }\n            'handlers': {\n                'telegram': {\n                    'class': 'telegram_handler.TelegramHandler',\n                    'formatter': 'telegram',\n                    'token': 'your token',\n                    'chat_id': 'chat id'\n                }\n            }\n        }\n        ...\n\nIf you wish, you can enable emoji symbols in HtmlFormatter. Just specify `use_emoji=True` in HtmlFormatter settings.\nThis will add to levelname a :white_circle: for DEBUG, :large_blue_circle: for INFO, and :red_circle: for WARNING and ERROR levels. \n\nProxy\n===========\n\nIn case if you have to use this package inside the country where Telegram servers are blocked by gowrnment you can specify proxy urls in config.\nUsing a dictConfig:\n\n.. code-block:: python\n        \n        ...\n        {\n            'handlers': {\n                'telegram': {\n                    'class': 'telegram_handler.TelegramHandler',\n                    'formatter': 'telegram',\n                    'token': 'your token',\n                    'chat_id': 'chat id',\n                    'proxies': {\n                        'http': 'socks5://user:pass@host:port',\n                        'https': 'socks5://user:pass@host:port'\n                    }\n                }\n            }\n        }\n        ...\n\n**Important!** If you plan to use *socks* proxy make sure you have ``requests`` package with ``socks`` support installed:\n\n.. code-block:: shell\n\n    pip install requests[socks]\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsashgorokhov%2Fpython-telegram-handler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsashgorokhov%2Fpython-telegram-handler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsashgorokhov%2Fpython-telegram-handler/lists"}