允许发送 TG 中包含的文件和图片 (#121)

发送 TG 中的文件 : tgfile::filename 消息链接
发送 TG 中的图片 : tgphoto::filename.png 消息链接 ( filename.png 文件名必须以图片的后缀结尾,否则效果与 tgfile 相同 )
This commit is contained in:
c3b2a 2021-02-08 08:12:40 +08:00 committed by GitHub
parent 3070c5cf0d
commit ae7b3c3aab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 22 deletions

View File

@ -183,16 +183,17 @@ async def send_reply(chat_id, reply_msg, context):
for s in type_parse: for s in type_parse:
if len(s) >= 5 and "ext_" == s[0:4] and is_num(s[4:]): if len(s) >= 5 and "ext_" == s[0:4] and is_num(s[4:]):
chat_id = int(s[4:]) chat_id = int(s[4:])
type_parse.remove(s)
break break
if "plain" in type_parse: if "plain" == re_type:
if could_send_msg: if could_send_msg:
update_last_time = True update_last_time = True
await bot.send_message(chat_id, re_msg, reply_to=None) await bot.send_message(chat_id, re_msg, reply_to=None)
elif "reply" in type_parse and chat_id == real_chat_id: elif "reply" == re_type and chat_id == real_chat_id:
if could_send_msg: if could_send_msg:
update_last_time = True update_last_time = True
await bot.send_message(chat_id, re_msg, reply_to=context.id) await bot.send_message(chat_id, re_msg, reply_to=context.id)
elif "file" in type_parse and len(re_msg.split()) >= 2: elif ("file" in type_parse or "photo" in type_parse) and len(re_msg.split()) >= 2:
if could_send_msg: if could_send_msg:
update_last_time = True update_last_time = True
if not path.exists("/tmp"): if not path.exists("/tmp"):
@ -206,20 +207,46 @@ async def send_reply(chat_id, reply_msg, context):
with open(file_name, "wb") as f: with open(file_name, "wb") as f:
f.write(file_get.content) f.write(file_get.content)
reply_to = None reply_to = None
if "reply" in re_type.split(","): if "reply" in type_parse:
reply_to = context.id reply_to = context.id
await bot.send_file(chat_id, file_name, reply_to=reply_to, force_document=True) await bot.send_file(chat_id, file_name,
reply_to=reply_to, force_document=("file" in type_parse))
remove(file_name) remove(file_name)
elif ("tgfile" in type_parse or "tgphoto" in type_parse) and len(re_msg.split()) >= 2:
if could_send_msg:
update_last_time = True
if not path.exists("/tmp"):
mkdir("/tmp")
re_data = re_msg.split()
file_name = "/tmp/" + re_data[0]
_data = BytesIO()
re_data[1] = re_data[1].split("/")[-2:]
msg_chat_id = int(re_data[1][0])
msg_id_inchat = int(re_data[1][1])
media_msg = (await bot.get_messages(msg_chat_id, msg_id_inchat))[0]
_data = BytesIO()
if media_msg and media_msg.media:
if "tgfile" in type_parse:
await bot.download_file(media_msg.media.document, _data)
else:
await bot.download_file(media_msg.photo, _data)
with open(file_name, "wb") as f:
f.write(_data.getvalue())
reply_to = None
if "reply" in type_parse:
reply_to = context.id
await bot.send_file(chat_id, file_name, reply_to=reply_to,
force_document=("tgfile" in type_parse))
remove(file_name)
elif "op" in type_parse: elif "op" in type_parse:
if re_msg == "delete": if re_msg == "delete":
await context.delete() await context.delete()
elif re_msg.split()[0] == "sleep" and len(re_msg.split()) == 2: elif re_msg.split()[0] == "sleep" and len(re_msg.split()) == 2:
sleep_time = re_msg.split()[1] sleep_time = re_msg.split()[1]
if is_num(sleep_time): await asyncio.sleep(float(sleep_time))
await asyncio.sleep(int(sleep_time))
chat_id = real_chat_id chat_id = real_chat_id
except: except:
pass chat_id = real_chat_id
if update_last_time: if update_last_time:
global group_last_time global group_last_time
group_last_time[int(chat_id)] = time.time() group_last_time[int(chat_id)] = time.time()
@ -229,8 +256,8 @@ async def send_reply(chat_id, reply_msg, context):
@listener(is_plugin=True, outgoing=True, command="keyword", @listener(is_plugin=True, outgoing=True, command="keyword",
description="关键词自动回复", description="关键词自动回复",
parameters="``new <plain|regex> '<规则>' '<回复信息>'` 或者 `del <plain|regex> '<规则>'` 或者 `list` 或者 `clear " parameters="``new <plain|regex> '<规则>' '<回复信息>'` 或者 `del <plain|regex> '<规则>'` 或者 `list` 或者 "
"<plain|regex>") "`clear <plain|regex>")
async def reply(context): async def reply(context):
if not redis_status(): if not redis_status():
await context.edit("出错了呜呜呜 ~ Redis 离线,无法运行") await context.edit("出错了呜呜呜 ~ Redis 离线,无法运行")
@ -252,8 +279,8 @@ async def reply(context):
len(parse[0].split()) == 1 and parse[0].split()[0] in ("new", "del", "delid", "clear")) or len( len(parse[0].split()) == 1 and parse[0].split()[0] in ("new", "del", "delid", "clear")) or len(
parse[0].split()) > 2: parse[0].split()) > 2:
await context.edit( await context.edit(
"[Code: -1] 格式错误,格式为 `-keyword` 加上 `new <plain|regex> '<规则>' '<回复信息>'` 或者 `del <plain|regex> '<规则>'` 或者 " "[Code: -1] 格式错误,格式为 `-keyword` 加上 `new <plain|regex> '<规则>' '<回复信息>'` 或者 "
"`list` 或者 `clear <plain|regex>`") "`del <plain|regex> '<规则>'` 或者 `list` 或者 `clear <plain|regex>`")
await del_msg(context, 10) await del_msg(context, 10)
return return
else: else:
@ -267,8 +294,8 @@ async def reply(context):
redis.set(f"keyword.{chat_id}.regex", save_rules(regex_dict, placeholder)) redis.set(f"keyword.{chat_id}.regex", save_rules(regex_dict, placeholder))
else: else:
await context.edit( await context.edit(
"格式错误,格式为 `-keyword` 加上 `new <plain|regex> '<规则>' '<回复信息>'` 或者 `del <plain|regex> '<规则>'` 或者 `list` " "[Code: -1] 格式错误,格式为 `-keyword` 加上 `new <plain|regex> '<规则>' '<回复信息>'` 或者 "
"或者 `clear <plain|regex>`") "`del <plain|regex> '<规则>'` 或者 `list` 或者 `clear <plain|regex>`")
await del_msg(context, 10) await del_msg(context, 10)
return return
await context.edit("设置成功") await context.edit("设置成功")
@ -298,8 +325,8 @@ async def reply(context):
return return
else: else:
await context.edit( await context.edit(
"格式错误,格式为 -keyword 加上 new <plain|regex> '<规则>' '<回复信息>' 或者 del <plain|regex> '<规则>' 或者 list 或者 clear " "[Code: -1] 格式错误,格式为 `-keyword` 加上 `new <plain|regex> '<规则>' '<回复信息>'` 或者 "
"<plain|regex>") "`del <plain|regex> '<规则>'` 或者 `list` 或者 `clear <plain|regex>`")
await del_msg(context, 10) await del_msg(context, 10)
return return
await context.edit("删除成功") await context.edit("删除成功")
@ -333,8 +360,8 @@ async def reply(context):
await del_msg(context, 5) await del_msg(context, 5)
else: else:
await context.edit( await context.edit(
"[Code -2] 格式错误,格式为 `-keyword` 加上 `new <plain|regex> '<规则>' '<回复信息>'` 或者 `del <plain|regex> '<规则>'` 或者 " "[Code: -1] 格式错误,格式为 `-keyword` 加上 `new <plain|regex> '<规则>' '<回复信息>'` 或者 "
"`list` 或者 `clear <plain|regex>`") "`del <plain|regex> '<规则>'` 或者 `list` 或者 `clear <plain|regex>`")
await del_msg(context, 10) await del_msg(context, 10)
return return
@ -616,9 +643,9 @@ async def funcset(context):
async def auto_reply(context): async def auto_reply(context):
if not redis_status(): if not redis_status():
return return
chat_id = context.chat_id
sender_id = context.sender_id
try: try:
chat_id = context.chat_id
sender_id = context.sender_id
if context.id not in read_context: if context.id not in read_context:
plain_dict = get_redis(f"keyword.{chat_id}.plain") plain_dict = get_redis(f"keyword.{chat_id}.plain")
regex_dict = get_redis(f"keyword.{chat_id}.regex") regex_dict = get_redis(f"keyword.{chat_id}.regex")

View File

@ -362,10 +362,10 @@
}, },
{ {
"name": "keyword", "name": "keyword",
"version": "2.31", "version": "2.4",
"section": "chat", "section": "chat",
"maintainer": "c3b2a, xtaodada", "maintainer": "c3b2a, xtaodada",
"size": "26.7 kb", "size": "28.6 kb",
"supported": true, "supported": true,
"des-short": "群组关键词自动回复插件", "des-short": "群组关键词自动回复插件",
"des": "命令: keyword, replyset, funcset。" "des": "命令: keyword, replyset, funcset。"