-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram.py
More file actions
33 lines (27 loc) · 974 Bytes
/
Copy pathtelegram.py
File metadata and controls
33 lines (27 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from configparser import ConfigParser
from enum import Enum
from typing import Optional, Any
from service import Service
class ParseMode(str, Enum):
HTML = "HTML"
MARKDOWN = "Markdown"
MARKDOWN_V2 = "MarkdownV2"
class TelegramBot(Service):
def process_data(self, data: dict[str, Any]) -> dict[str, Any]:
return data
def send_text(
self, chat_id: int, text: str, parse_mode: Optional[ParseMode] = None
) -> dict[str, Any]:
return self.post(
path="sendMessage",
data={"chat_id": chat_id, "text": text, "parse_mode": parse_mode},
)
@classmethod
def from_config(cls, path: str = "config.ini") -> "TelegramBot":
config = ConfigParser()
config.read(path)
BOT_SECTION = config["BOT"]
bot_id = BOT_SECTION["id"]
bot_secret = BOT_SECTION["secret"]
token = f"{bot_id}:{bot_secret}"
return cls(f"https://api.telegram.org/bot{token}/")