2021-05-05 08:13:28 +00:00
|
|
|
|
import time
|
2021-05-05 03:41:27 +00:00
|
|
|
|
import connector
|
2021-05-05 03:10:01 +00:00
|
|
|
|
import telegram
|
2021-05-05 10:28:26 +00:00
|
|
|
|
from telegram.ext import CommandHandler, MessageHandler, Filters
|
2021-05-05 03:10:01 +00:00
|
|
|
|
from config import TOKEN
|
2021-05-05 08:13:28 +00:00
|
|
|
|
import schedule
|
2021-05-05 03:10:01 +00:00
|
|
|
|
|
|
|
|
|
bot = telegram.Bot(token=TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def start(update, context):
|
2021-05-05 10:28:26 +00:00
|
|
|
|
try:
|
2021-05-05 11:30:11 +00:00
|
|
|
|
connector.get_connection().keys()
|
2021-05-05 10:28:26 +00:00
|
|
|
|
print('进入start函数')
|
|
|
|
|
update.message.reply_text(
|
|
|
|
|
'在呢!系统运行正常~',
|
|
|
|
|
)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(e)
|
|
|
|
|
print('进入start函数')
|
|
|
|
|
update.message.reply_text("系统故障,Redis连接失败,请检查!")
|
|
|
|
|
update.message.reply_text("错误信息:" + str(e))
|
2021-05-05 03:10:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def chat_content_exec(update, context):
|
2021-05-05 07:35:04 +00:00
|
|
|
|
try:
|
2021-05-06 23:45:54 +00:00
|
|
|
|
r = connector.get_connection()
|
|
|
|
|
text = update.message.text
|
|
|
|
|
chat_type = update.effective_chat.type
|
|
|
|
|
user_id = update.effective_user.id
|
|
|
|
|
chat_id = update.effective_message.chat_id
|
|
|
|
|
# 限制为群组
|
|
|
|
|
if chat_type != "supergroup":
|
|
|
|
|
return
|
|
|
|
|
# 限制文字长度不能超过80字
|
|
|
|
|
if len(text) > 80:
|
|
|
|
|
return
|
|
|
|
|
# 取消注释开启独享模式(仅授权群组可用)
|
|
|
|
|
# if chat_id not in ["1231242141"]:
|
|
|
|
|
# return
|
|
|
|
|
try:
|
|
|
|
|
username = update.effective_user.username
|
|
|
|
|
except Exception as e:
|
|
|
|
|
username = update.effective_user.id
|
|
|
|
|
user = update.message.from_user
|
2021-05-06 23:31:07 +00:00
|
|
|
|
firstname = ""
|
2021-05-06 23:45:54 +00:00
|
|
|
|
lastname = ""
|
|
|
|
|
try:
|
2021-05-06 23:55:32 +00:00
|
|
|
|
firstname = str(user["first_name"])
|
2021-05-06 23:45:54 +00:00
|
|
|
|
except Exception as e:
|
|
|
|
|
print(e)
|
|
|
|
|
print("用户没有设置 firstname")
|
|
|
|
|
try:
|
2021-05-06 23:55:32 +00:00
|
|
|
|
lastname = str(user["last_name"])
|
2021-05-06 23:45:54 +00:00
|
|
|
|
except Exception as e:
|
|
|
|
|
print(e)
|
|
|
|
|
print("用户没有设置 last_name")
|
2021-05-07 03:18:04 +00:00
|
|
|
|
if firstname == "None" and lastname == "None":
|
2021-05-06 23:45:54 +00:00
|
|
|
|
name = username
|
2021-05-07 03:18:04 +00:00
|
|
|
|
elif firstname == "None" and lastname != "None":
|
2021-05-06 23:45:54 +00:00
|
|
|
|
name = lastname
|
2021-05-07 03:18:04 +00:00
|
|
|
|
elif firstname != "None" and lastname == "None":
|
2021-05-06 23:45:54 +00:00
|
|
|
|
name = firstname
|
2021-05-07 03:18:04 +00:00
|
|
|
|
elif firstname != "None" and lastname != "None":
|
2021-05-06 23:45:54 +00:00
|
|
|
|
name = firstname + " " + lastname
|
|
|
|
|
print("\n---------------------------")
|
|
|
|
|
print("内容: " + text[:10])
|
|
|
|
|
print("群组类型: " + str(chat_type))
|
|
|
|
|
print("用户ID: " + str(user_id))
|
|
|
|
|
print("chat_id: " + str(chat_id))
|
|
|
|
|
if "/" in text:
|
|
|
|
|
print("这是一条指令信息,跳过")
|
|
|
|
|
return
|
2021-05-05 06:50:25 +00:00
|
|
|
|
else:
|
2021-05-06 23:45:54 +00:00
|
|
|
|
if text[-1] not in [",", "。", "!", ":", "?", "!", "?", ",", ":", "."]:
|
|
|
|
|
r.append("{}_chat_content".format(chat_id), text + "。")
|
|
|
|
|
else:
|
|
|
|
|
r.append("{}_chat_content".format(chat_id), text)
|
|
|
|
|
r.incrby("{}_total_message_amount".format(chat_id))
|
|
|
|
|
r.hincrby("{}_user_message_amount".format(chat_id), name)
|
|
|
|
|
print("---------------------------")
|
|
|
|
|
except Exception as e:
|
2021-05-06 23:55:32 +00:00
|
|
|
|
print(e)
|
2021-05-06 23:45:54 +00:00
|
|
|
|
print("用户数据提取、入库错误")
|
2021-05-05 03:41:27 +00:00
|
|
|
|
|
2021-05-05 03:10:01 +00:00
|
|
|
|
|
2021-05-05 08:13:28 +00:00
|
|
|
|
def check_schedule():
|
|
|
|
|
while True:
|
|
|
|
|
schedule.run_pending()
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
|
2021-05-05 03:10:01 +00:00
|
|
|
|
start_handler = CommandHandler('start', start)
|
2021-05-05 03:12:17 +00:00
|
|
|
|
chat_content_handler = MessageHandler(Filters.text, chat_content_exec)
|