tangled
alpha
login
or
join now
me.vylion.com
/
velascobot2
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
Added dynamic calculation of processed messages
me.vylion.com
1 month ago
5ddae470
2d143ebc
+48
-34
5 changed files
expand all
collapse all
unified
split
.gitignore
velascobot2
bot.py
database.py
messenger.py
parrot.py
+4
-1
.gitignore
···
10
10
11
11
# databases
12
12
*.db
13
13
-
*.sqlite
13
13
+
*.sql*
14
14
+
15
15
+
# personal scripts
16
16
+
.personal_*
+21
-33
velascobot2/bot.py
···
191
191
assert update.message
192
192
193
193
chat_id = str(update.message.chat.id)
194
194
-
chat_data = self.messenger.get_chat_data(chat_id)
195
195
-
if chat_data is None:
196
196
-
try:
197
197
-
await update.message.reply_text(
198
198
-
"I've processed no messages so far."
199
199
-
)
200
200
-
except (
201
201
-
telegram.error.Forbidden,
202
202
-
telegram.error.BadRequest,
203
203
-
telegram.error.NetworkError,
204
204
-
) as e:
205
205
-
logger.error(str(e))
206
206
-
return
207
207
-
208
208
-
num = (
209
209
-
str(chat_data.processed_messages)
210
210
-
if chat_data.processed_messages > 0
211
211
-
else "no"
212
212
-
)
194
194
+
count = self.messenger.get_processed_messages(chat_id)
213
195
try:
214
196
await update.message.reply_text(
215
215
-
"I've processed {} messages so far.".format(num)
197
197
+
f"I remember {count} messages."
216
198
)
217
199
except (
218
200
telegram.error.Forbidden,
···
379
361
"let only admins" if not chat_data.restricted else "let everyone"
380
362
)
381
363
try:
382
382
-
await update.message.reply_text(f"I will {allowed} configure me now.")
364
364
+
await update.message.reply_text(
365
365
+
f"I will {allowed} configure me now."
366
366
+
)
383
367
except (
384
368
telegram.error.Forbidden,
385
369
telegram.error.BadRequest,
···
672
656
self, update: Update, context: ContextTypes.DEFAULT_TYPE
673
657
):
674
658
"""Handles the /speak command."""
659
659
+
assert update.message
675
660
chat = update.message.chat
676
661
chat_id = str(chat.id)
662
662
+
if self.messenger.get_processed_messages(chat_id) == 0:
663
663
+
empty_gen_warning = "I haven't learned a single word yet."
664
664
+
await self.send(
665
665
+
chat_id,
666
666
+
Response.TEXT,
667
667
+
content=empty_gen_warning,
668
668
+
reply_to_message_id=update.message.message_id,
669
669
+
)
670
670
+
return
671
671
+
677
672
chat_data = self.messenger.get_chat_data(chat_id)
678
673
679
674
if chat_data is None:
680
675
return
681
676
682
677
if not self.testing and chat_data.restricted:
683
683
-
assert update.message
678
678
+
assert update.message.from_user
684
679
user = await update.message.chat.get_member(
685
680
update.message.from_user.id
686
681
)
···
696
691
# words = update.message.text.split()
697
692
# if len(words) > 1:
698
693
# self.messenger.process_text(chat_id, ' '.join(words[1:]))
699
699
-
if chat_data.processed_messages > 0:
700
700
-
await self.speak(chat_id, reply_to_message_id=rid)
701
701
-
else:
702
702
-
empty_gen_warning = "I haven't learned a single word yet."
703
703
-
await self.send(
704
704
-
chat_id,
705
705
-
Response.TEXT,
706
706
-
content=empty_gen_warning,
707
707
-
reply_to_message_id=rid,
708
708
-
)
694
694
+
await self.speak(chat_id, reply_to_message_id=rid)
709
695
710
696
async def show_config(
711
697
self, update: Update, context: ContextTypes.DEFAULT_TYPE
···
726
712
else:
727
713
permissions = "neither restricted nor silenced"
728
714
715
715
+
processed_messages = self.messenger.get_processed_messages(chat_id)
716
716
+
729
717
answer = (
730
718
f"You're messaging in the chat of __{chat_data.name}__, with id "
731
719
f"`{chat_data.id}`, message count "
732
732
-
f"{chat_data.processed_messages}, period "
720
720
+
f"{processed_messages}, period "
733
721
f"{chat_data.message_period}, and answer probability "
734
722
f"{chat_data.answer_chance}.\n\nThis chat is {permissions}."
735
723
)
+14
velascobot2/database.py
···
154
154
155
155
return rows
156
156
157
157
+
def get_chat_vocab_count(self, chat_id: str) -> int:
158
158
+
with _SQLiteContextMgr(database=self.db_name) as db:
159
159
+
result = db.execute(
160
160
+
"SELECT SUM(count) FROM vocab WHERE chat_id = ?", (chat_id,)
161
161
+
).fetchone()[0]
162
162
+
return result or 0
163
163
+
164
164
+
def get_chat_graph_count(self, chat_id: str) -> int:
165
165
+
with _SQLiteContextMgr(database=self.db_name) as db:
166
166
+
result = db.execute(
167
167
+
"SELECT SUM(count) FROM graphics WHERE chat_id = ?", (chat_id,)
168
168
+
).fetchone()[0]
169
169
+
return result or 0
170
170
+
157
171
def get_graphics(self, chat_id: str):
158
172
with _SQLiteContextMgr(database=self.db_name) as db:
159
173
rows = db.execute(
+3
velascobot2/messenger.py
···
138
138
139
139
def update_set_silenced(self, chat_id: str, silenced: bool):
140
140
self._parrot.update_chat_set_silenced(chat_id, silenced)
141
141
+
142
142
+
def get_processed_messages(self, chat_id: str) -> int:
143
143
+
return self._parrot.get_processed_messages(chat_id)
+6
velascobot2/parrot.py
···
133
133
134
134
def update_chat_set_silenced(self, chat_id: str, silenced: bool):
135
135
self.db.update_chat_set_silenced(chat_id, silenced)
136
136
+
137
137
+
def get_processed_messages(self, chat_id: str) -> int:
138
138
+
graph_count = self.db.get_chat_graph_count(chat_id)
139
139
+
start_words = self.db.get_next_words(chat_id, " ")
140
140
+
start_word_count = sum(weight for _, weight in start_words)
141
141
+
return graph_count + start_word_count