-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcess_handling.py
128 lines (114 loc) · 4.11 KB
/
Process_handling.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# import for GUI signals and threads
from PyQt5.QtCore import QRunnable, QThreadPool,QThread,pyqtSignal,QObject,pyqtSlot
import threading
# Import for telegram bot and async code usage
import asyncio
from aiogram import Bot, Dispatcher, executor, types
from aiogram.utils import executor
import re
# SQLite database imports
import sqlite3
# Making bot code
async def bot_main(token,status):
bot = Bot(token)
dp = Dispatcher(bot)
try:
# Use the 'get_me' method to check if the token is valid
me = await bot.get_me()
print("Bot information:", me)
print("Token is valid!")
mydict = me.__dict__["_values"]
mydict['status'] = True
print(mydict)
status.emit(mydict)
except Exception as e:
print("Token is invalid:", e)
mydict = {"status":False,"message":str(e)}
status.emit(mydict)
finally:
# Close the bot session gracefully
await dp.storage.close()
await dp.storage.wait_closed()
session = await bot.get_session()
await session.close()
return True
class Test_Bot(QThread):
status = pyqtSignal(dict)
def __init__(self,token):
super().__init__()
self.token = token
def run(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(bot_main(self.token,self.status))
return
class AIogramBot(QThread):
stopped = pyqtSignal(bool)
started = pyqtSignal()
error = pyqtSignal(str)
def __init__(self,token,bot_started_signal: pyqtSignal):
super().__init__()
try:
self.bot = Bot(token=token)
self.dp = Dispatcher(self.bot)
self.bot_started_signal = bot_started_signal
except Exception as error:
print(error)
self.error.emit(str(error))
raise error
@self.dp.message_handler()
async def get_messages(message: types.Message):
try:
db = sqlite3.connect("information.db")
cursor = db.cursor()
cursor.execute("SELECT * FROM messages")
rows = cursor.fetchall()
for row in rows:
if row[2] != "" and row[2] != None:
if row[4] == 0:
print("checking in lowercase : ",message.text.lower()," | ",row[2].lower())
if message.text.lower() == row[2].lower():
await message.reply(row[1])
break
else:
if message.text == row[2]:
await message.reply(row[1])
break
elif row[3] != "" and row[3] != None:
if row[4] == 0:
matches = re.findall(row[3],message.text,re.IGNORECASE)
else:
matches = re.findall(row[3],message.text)
if len(matches)>0:
await message.reply(row[1])
break
cursor.close()
db.close()
except sqlite3.Error as e:
print(f"Error occurred: {e}")
async def started_signal(self,dp):
self.started.emit()
self.bot_started_signal.emit(True)
def start_bot(self):
try:
print("bot starting...")
executor.start_polling(dispatcher=self.dp,skip_updates=True,on_startup=self.started_signal)
except Exception as error:
print(error)
self.error.emit(str(error))
return
def run(self):
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
self.start_bot()
except Exception as error:
print(error)
self.error.emit(str(error))
return
def stop_bot(self):
self.terminate()
self.wait()
print("bot is stopped")
self.stopped.emit(True)
self.bot_started_signal.emit(False)