-
Notifications
You must be signed in to change notification settings - Fork 0
/
Telebot.py
46 lines (35 loc) · 1.26 KB
/
Telebot.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
from telegram import Bot
import asyncio
import threading
import sys
class Telebot:
def __init__(self):
self._bot_id = ""
self._chat_id = ""
self._loop = asyncio.new_event_loop()
with open("../bot_info.info", "r") as f:
file_lines = f.readlines()
self._bot_id = file_lines[0].strip()
self._chat_id = file_lines[1].strip()
self._bot = Bot(token=self._bot_id)
async def _send_async(self, message):
try:
await self._bot.send_message(chat_id=self._chat_id, text=message)
except Exception as e:
print(f"Error sending message: {e}")
def _send(self, message):
def run_coroutine():
asyncio.set_event_loop(self._loop)
self._loop.run_until_complete(self._send_async(message))
thread = threading.Thread(target=run_coroutine)
thread.start()
def send_message(self, name, error=False):
message = ""
if error:
message = "Error occurred while taking screenshot for {}".format(name)
else:
message = "{}".format(name)
self._send(message)
def send_error(self, contents):
contents = "Error occurred: {}".format(contents)
self._send(contents)