-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.py
65 lines (52 loc) · 2.25 KB
/
Bot.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import logging
import requests
import asyncio
from typing import Tuple
from telegram.ext import Application
# Decorator to track errors. if more than 3 errors occur, raise an exception
def error_handler(func):
async def wrapper(*args, **kwargs):
error_count = 0
while error_count < 3:
try:
return await func(*args, **kwargs)
except Exception as e:
error_count += 1
await asyncio.sleep(5)
logging.error(f"Error in {func.__name__}: {e}")
raise Exception(f"Failed to execute {func.__name__}")
return wrapper
class Bot:
def __init__(self, token, chat_id) -> None:
# Creating an Application instance for the bot using the provided token
self.application = Application.builder().token(token).build()
self.chat_id = chat_id
@error_handler
async def send_file(self, file_path: str) -> Tuple[int, str]:
# Sending the document to the specified chat ID and getting the response
data = await self.application.bot.send_document(
self.chat_id,
open(file_path, "rb"), # Opens the file in binary read mode
)
# Extracting the message ID and file ID from the response
message_id = data["message_id"]
file_id = data["document"]["file_id"]
logging.info(f"File sent: {file_path}, file_id: {file_id}, msg_id: {message_id}")
# Returning the message ID and file ID
return message_id, file_id
@error_handler
async def get_file(self, file_id: str) -> str:
# Get file details from Telegram
file_data = await self.application.bot.get_file(file_id)
file_path = file_data.file_path
# Download the file content
response = requests.get(file_path)
response.raise_for_status() # Ensure that a valid response is received
# Log the action
logging.info(f"File received: {file_id}")
return response.content
async def delete_file(self, message_id: int) -> None:
# Deleting the message with the specified message ID from the chat
await self.application.bot.delete_message(self.chat_id, message_id)
logging.info(f"File deleted: {message_id}")