You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
4.4 KiB
127 lines
4.4 KiB
import json
|
|
import traceback
|
|
from flask import Flask, request
|
|
from loguru import logger
|
|
from tg_module.initial_group import run, _replay, _detail
|
|
|
|
app = Flask(__name__)
|
|
|
|
"""
|
|
pip install 'flask[async]': flask 支持异步版本
|
|
启动:uwsgi -d --ini uwsgi.ini
|
|
停止:uwsgi --stop
|
|
重启:uwsgi --reload
|
|
"""
|
|
|
|
|
|
@app.route("/join")
|
|
async def index():
|
|
"""
|
|
加群接口
|
|
telegram_link: telegram链接 目前只支持public群组
|
|
:return:
|
|
成功:{"code": 200, "content": obj}
|
|
失败:{"code": 500, "content": error}/ {"code": 200, "content": ""}
|
|
"""
|
|
state_203 = "You have successfully requested to join this chat or channel"
|
|
try:
|
|
telegram_link = request.args.get('telegram_link')
|
|
if telegram_link and "https://t.me/" in telegram_link:
|
|
i = telegram_link.strip()
|
|
split_list = i.split("/")
|
|
is_private = False
|
|
if len(split_list) > 4 and split_list[-2] == "joinchat": # 切换为私密群添加方式
|
|
is_private = True
|
|
channel = split_list[-1]
|
|
logger.info(f"获得当前群链接:{i}, 获得当前channel:{channel}")
|
|
res = await run(telegram_link, channel, is_private=is_private)
|
|
if isinstance(res, dict):
|
|
ret = {"code": 200, "content": res}
|
|
elif state_203 in res:
|
|
ret = {"code": 203, "content": state_203}
|
|
else:
|
|
ret = {"code": 500, "content": res}
|
|
else:
|
|
ret = {"code": 500, "content": "telegram_link error"}
|
|
except Exception as e:
|
|
traceback.print_exc()
|
|
logger.error(f"server error: {e}")
|
|
ret = {"code": 500, "content": str(e)}
|
|
logger.info(ret)
|
|
return json.dumps(ret, ensure_ascii=False)
|
|
|
|
|
|
@app.route("/replay", methods=["POST"])
|
|
async def replay():
|
|
"""
|
|
回复消息
|
|
:return:
|
|
"""
|
|
try:
|
|
data = request.get_data()
|
|
json_data = json.loads(data)
|
|
telegram_link = json_data.get("telegram_link")
|
|
messages = json_data.get("messages")
|
|
|
|
# messages = {
|
|
# "message": "",
|
|
# "media_type": "", # photos/videos/plain/docs
|
|
# "media_link": ""
|
|
# }
|
|
# telegram_link = "https://t.me/xeqm3333"
|
|
if telegram_link and "https://t.me/" in telegram_link:
|
|
i = telegram_link.strip()
|
|
split_list = i.split("/")
|
|
if len(split_list) > 4 and split_list[-2] == "joinchat": # TODO: 私密群暂时不支持
|
|
ret = {"code": 500, "content": "私密群暂时不支持"}
|
|
else:
|
|
channel = split_list[-1]
|
|
logger.info(f"获得当前群链接:{i}, 获得当前channel:{channel}")
|
|
status = await _replay(telegram_link, channel, messages)
|
|
if status["send_message_status"] == "发送成功":
|
|
ret = {"code": 200, "content": status}
|
|
else:
|
|
ret = {"code": 400, "content": status}
|
|
else:
|
|
ret = {"code": 500, "content": "telegram_link error"}
|
|
|
|
except Exception as e:
|
|
traceback.print_exc()
|
|
logger.error(f"server error: {e}")
|
|
ret = {"code": 500, "content": str(e)}
|
|
logger.info(ret)
|
|
|
|
return json.dumps(ret, ensure_ascii=False)
|
|
|
|
|
|
@app.route("/detail")
|
|
async def get_detail():
|
|
"""
|
|
获取群组详情
|
|
|
|
:return:
|
|
"""
|
|
try:
|
|
telegram_link = request.args.get('telegram_link')
|
|
if telegram_link and "https://t.me/" in telegram_link:
|
|
i = telegram_link.strip()
|
|
split_list = i.split("/")
|
|
if len(split_list) > 4 and split_list[-2] == "joinchat": # TODO: 私密群暂时不支持
|
|
ret = {"code": 500, "content": "私密群暂时不支持"}
|
|
else:
|
|
channel = split_list[-1]
|
|
logger.info(f"获得当前群链接:{i}, 获得当前channel:{channel}")
|
|
group_detail = await _detail(channel, file="tg_module/profile")
|
|
ret = {"code": 200, "content": group_detail}
|
|
else:
|
|
ret = {"code": 500, "content": "telegram_link error"}
|
|
except Exception as e:
|
|
traceback.print_exc()
|
|
logger.error(f"server error: {e}")
|
|
ret = {"code": 500, "content": str(e)}
|
|
logger.info(ret)
|
|
return json.dumps(ret, ensure_ascii=False)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host='0.0.0.0', port=8080)
|