添加主动触发任务命令

设置任务队列机制
支持设置每个群组每小时主动触发次数限制
This commit is contained in:
机器人总动员 2021-05-08 11:29:10 +08:00
parent 9c38377809
commit 7609d39916
4 changed files with 19 additions and 19 deletions

View File

@ -5,7 +5,7 @@ RUN rm -rf /etc/localtime
RUN ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime RUN ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN cd /root \ RUN cd /root \
&& git clone https://github.com/devourbots/word_cloud_bot.git && git clone https://github.com/devourbots/word_cloud_bot.git
RUN echo 'TOKEN = "这里输入机器人token"' > /root/word_cloud_bot/config.py RUN echo sed '1c TOKEN = "这里输入机器人token"' /root/word_cloud_bot/config.py
COPY entrypoint.sh /root/entrypoint.sh COPY entrypoint.sh /root/entrypoint.sh
RUN chmod +x /root/entrypoint.sh \ RUN chmod +x /root/entrypoint.sh \
&& pip3 install -r /root/word_cloud_bot/requirements.txt && pip3 install -r /root/word_cloud_bot/requirements.txt

View File

@ -57,7 +57,7 @@ def rank(update, context):
update.message.reply_text("该群组在这个小时内的生成配额已经用完,请稍后再试~") update.message.reply_text("该群组在这个小时内的生成配额已经用完,请稍后再试~")
return return
add_task(chat_id) add_task(chat_id)
print("群组: {},用户: {}|{} 发起了主动触发请求".format(username, user_id, chat_id)) print("群组: {},用户: {}|{} 发起了主动触发请求".format(chat_id, username, user_id, ))
update.message.reply_text("统计数据将在分析完毕后发送到当前群组,请稍等~") update.message.reply_text("统计数据将在分析完毕后发送到当前群组,请稍等~")
except Exception as e: except Exception as e:
print("主动触发任务失败,请检查") print("主动触发任务失败,请检查")

View File

@ -11,7 +11,7 @@ schedule.every().day.at('23:30').do(schedule_task)
schedule.every().day.at('23:59').do(flush_redis) schedule.every().day.at('23:59').do(flush_redis)
# 测试代码,每分钟推送数据,非测试目的不要取消注释下一行 # 测试代码,每分钟推送数据,非测试目的不要取消注释下一行
# schedule.every(1).minutes.do(do_task) # schedule.every(1).minutes.do(schedule_task)
# 开启分析线程,当队列中由任务时,会取出任务分析生成数据 # 开启分析线程,当队列中由任务时,会取出任务分析生成数据
threading.Thread(target=do_task).start() threading.Thread(target=do_task).start()

32
task.py
View File

@ -24,6 +24,7 @@ def schedule_task():
if "chat_content" in i: if "chat_content" in i:
group_list.append(i[:i.find("_")]) group_list.append(i[:i.find("_")])
# print(group_list) # print(group_list)
print("运行定时任务,让任务队列中添加任务,任务数量:{}".format(len(group_list)))
for group in group_list: for group in group_list:
try: try:
# 网任务队列中添加任务 # 网任务队列中添加任务
@ -53,6 +54,10 @@ def do_task():
except Exception as e: except Exception as e:
print("群组: {} | 处理失败,请检查报错!".format(group)) print("群组: {} | 处理失败,请检查报错!".format(group))
print(e) print(e)
bot.send_message(
chat_id=group,
text="当前聊天数据量过小,嗨起来吧~"
)
time.sleep(1) time.sleep(1)
@ -79,29 +84,23 @@ def generate(group):
if chat_content is None: if chat_content is None:
print("数据库中不存在此群组数据") print("数据库中不存在此群组数据")
try: try:
time.sleep(1)
bot.send_message( bot.send_message(
chat_id=group, chat_id=group,
text="数据库中不存在群组数据,请检查是否授予机器人管理员权限\n" text="数据库中不存在群组数据,请检查是否授予机器人管理员权限,并通过聊天添加数据量,嗨起来吧~\n"
) )
except Exception as e: except Exception as e:
print("群组: {} | 机器人发送信息失败".format(group)) print("群组: {} | 机器人发送信息失败".format(group))
return return
word_list = [] word_list = []
try: words = pseg.cut(chat_content, use_paddle=True) # paddle模式
words = pseg.cut(chat_content, use_paddle=True) # paddle模式 for word, flag in words:
for word, flag in words: # print(word + "\t" + flag)
# print(word + "\t" + flag) if flag in ["n", "nr", "nz", "PER", "f", "ns", "LOC", "s", "nt", "ORG", "nw"]:
if flag in ["n", "nr", "nz", "PER", "f", "ns", "LOC", "s", "nt", "ORG", "nw"]: # 判断该词是否有效,不为空格
# 判断该词是否有效,不为空格 if re.match(r"^\s+?$", word) is None:
if re.match(r"^\s+?$", word) is None: word_list.append(word)
word_list.append(word) # print(word_list)
except Exception as e:
print(e)
bot.send_message(
chat_id=group,
text="当前聊天数据量过小,无法生成词云,嗨起来吧~\n"
)
# print(word_list)
# 获取消息总数 # 获取消息总数
total_message_amount = r.get("{}_total_message_amount".format(group)) total_message_amount = r.get("{}_total_message_amount".format(group))
@ -205,6 +204,7 @@ def generate(group):
chat_id=group, chat_id=group,
text="当前聊天数据量过小,嗨起来吧~" text="当前聊天数据量过小,嗨起来吧~"
) )
return
def flush_redis(): def flush_redis():