Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSJ pruned_transducer_stateless7_streaming #892

Merged
merged 27 commits into from
Feb 13, 2023
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add core codes
  • Loading branch information
teowenshen committed Jan 25, 2023

Verified

This commit was signed with the committer’s verified signature.
kidroca Peter Velkov
commit 9f1a62513d999e1f649e2867082d74a322898bff
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import logging
from configparser import ConfigParser

import requests


def escape_html(text: str):
"""
Escapes all html characters in text
:param str text:
:rtype: str
"""
return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")


class TelegramStreamIO(logging.Handler):

API_ENDPOINT = "https://api.telegram.org"
MAX_MESSAGE_LEN = 4096
formatter = logging.Formatter(
"%(asctime)s - %(levelname)s at %(funcName)s "
"(line %(lineno)s):\n\n%(message)s"
)

def __init__(self, tg_configfile: str):
super(TelegramStreamIO, self).__init__()
config = ConfigParser()
if not config.read(tg_configfile):
raise FileNotFoundError(
f"{tg_configfile} not found. "
"Retry without --telegram-cred flag."
)
config = config["TELEGRAM"]
token = config["token"]
self.chat_id = config["chat_id"]
self.url = f"{self.API_ENDPOINT}/bot{token}/sendMessage"

def emit(self, record: logging.LogRecord):
"""
Emit a record.
Send the record to the Web server as a percent-encoded dictionary
"""
data = {
"chat_id": self.chat_id,
"text": self.format(self.mapLogRecord(record)),
"parse_mode": "HTML",
}
try:
requests.get(self.url, json=data)
# return response.json()
except Exception as e:
logging.error(f"Failed to send telegram message: {repr(e)}")
pass

def mapLogRecord(self, record):
"""
Default implementation of mapping the log record into a dict
that is sent as the CGI data. Overwrite in your class.
Contributed by Franz Glasner.
"""

for k, v in record.__dict__.items():
if isinstance(v, str):
setattr(record, k, escape_html(v))
return record
Loading