2023-01-21 15:31:27 +00:00
|
|
|
import requests
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
def read_env(key):
|
|
|
|
if key in os.environ:
|
|
|
|
return os.environ[key]
|
|
|
|
else:
|
2023-03-03 02:13:12 +00:00
|
|
|
print(f"failed to read env {key}")
|
2023-01-21 15:31:27 +00:00
|
|
|
return ""
|
|
|
|
|
|
|
|
APK_FOLDER = "apks"
|
2023-03-03 02:13:12 +00:00
|
|
|
API_PREFIX = "http://127.0.0.1:38118/bot" + os.environ["BOT_TOKEN"] + "/"
|
2023-01-21 15:31:27 +00:00
|
|
|
|
2023-03-03 02:13:12 +00:00
|
|
|
BOT_TARGET = read_env("BOT_TARGET")
|
|
|
|
ADMIN_USERID = BOT_TARGET.replace("-100","")
|
2023-01-21 15:31:27 +00:00
|
|
|
|
|
|
|
VERSION_NAME = read_env("VERSION_NAME")
|
|
|
|
VERSION_CODE = read_env("VERSION_CODE")
|
|
|
|
COMMIT_HASH = read_env("GITHUB_SHA")
|
|
|
|
COMMIT_MESSAGE = read_env("COMMIT_MESSAGE")
|
|
|
|
|
2023-03-03 22:49:31 +00:00
|
|
|
APK_CHANNEL_ID = "@NekoXApks"
|
|
|
|
UPDATE_CHANNEL_ID = "@NekogramX"
|
|
|
|
UPDATE_METADATA_CHANNEL_ID = "@nekox_update_metadata"
|
2023-01-21 15:31:27 +00:00
|
|
|
CI_CHANNEL_ID = "@NekoX_CI"
|
|
|
|
|
|
|
|
|
2023-03-03 22:49:31 +00:00
|
|
|
def addEntity(entities, origin_str, en_type, content, url = None) -> str:
|
|
|
|
origin_len = len(origin_str)
|
|
|
|
entity = {
|
|
|
|
"type": en_type,
|
|
|
|
"offset": origin_len,
|
|
|
|
"length": len(content)
|
|
|
|
}
|
|
|
|
if url:
|
|
|
|
entity["url"] = url
|
|
|
|
entities.append(entity)
|
|
|
|
return content
|
2023-01-21 15:31:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def waitReply(mid):
|
|
|
|
last_update = 0
|
|
|
|
while True:
|
|
|
|
print(f"Waiting reply for {mid} offset {last_update}...")
|
|
|
|
resp = requests.post(API_PREFIX + "getUpdates", json={"allowed_updates":["message"], "timeout": 20, "offset": last_update + 1})
|
|
|
|
resp = resp.json()
|
|
|
|
if not resp["ok"]:
|
|
|
|
continue
|
|
|
|
resp = resp["result"]
|
|
|
|
for update in resp:
|
|
|
|
if 'message' in update:
|
|
|
|
msg = update["message"]
|
|
|
|
if 'from' in msg and str(msg['from']["id"]) == ADMIN_USERID:
|
|
|
|
if 'reply_to_message' in msg and str(msg["reply_to_message"]["message_id"]) == str(mid):
|
|
|
|
return msg
|
|
|
|
for update in resp:
|
|
|
|
last_update = max(last_update, update["update_id"])
|
|
|
|
|
|
|
|
|
2023-03-03 22:49:31 +00:00
|
|
|
def sendMessage(message, user_id = BOT_TARGET, entities = None) -> int:
|
2023-01-21 15:31:27 +00:00
|
|
|
data = {
|
|
|
|
"chat_id" : user_id,
|
|
|
|
"text": message,
|
2023-03-03 22:49:31 +00:00
|
|
|
"entities": entities
|
2023-01-21 15:31:27 +00:00
|
|
|
}
|
2023-03-15 23:39:52 +00:00
|
|
|
print(message)
|
|
|
|
print(entities)
|
2023-01-21 15:31:27 +00:00
|
|
|
resp = requests.post(API_PREFIX + "sendMessage", json=data).json()
|
2023-03-03 02:13:12 +00:00
|
|
|
print(resp)
|
2023-01-21 15:31:27 +00:00
|
|
|
return int(resp["result"]["message_id"])
|
|
|
|
|
|
|
|
|
2023-03-06 18:02:59 +00:00
|
|
|
def sendDocument(user_id, path, message = "", entities = None):
|
2023-01-21 15:31:27 +00:00
|
|
|
files = {'document': open(path, 'rb')}
|
2023-03-06 18:02:59 +00:00
|
|
|
data = {'chat_id': user_id,
|
|
|
|
'caption': message,
|
|
|
|
'parse_mode': 'Markdown',
|
|
|
|
'caption_entities': entities}
|
2023-01-21 15:31:27 +00:00
|
|
|
response = requests.post(API_PREFIX + "sendDocument", files=files, data=data)
|
2023-03-03 02:13:12 +00:00
|
|
|
print(response.json())
|
2023-01-21 15:31:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def sendRelease():
|
|
|
|
apks = os.listdir(APK_FOLDER)
|
|
|
|
apks.sort()
|
2023-03-03 02:13:12 +00:00
|
|
|
print(apks)
|
2023-01-21 15:31:27 +00:00
|
|
|
|
|
|
|
# read message from admin
|
2023-03-03 02:13:12 +00:00
|
|
|
mid = sendMessage(f"Please reply the release message for the version {VERSION_NAME},{VERSION_CODE}:", user_id=BOT_TARGET)
|
2023-01-21 15:31:27 +00:00
|
|
|
admin_resp = waitReply(mid)
|
|
|
|
|
|
|
|
# send message and apks to APK channel
|
|
|
|
message = f"=== {VERSION_NAME} ==="
|
|
|
|
apk_channel_first_id = sendMessage(message, user_id=APK_CHANNEL_ID)
|
|
|
|
|
|
|
|
for apk in apks:
|
|
|
|
path = os.path.join(APK_FOLDER, apk)
|
|
|
|
sendDocument(user_id=APK_CHANNEL_ID, path=path)
|
|
|
|
|
|
|
|
# generate release message and send to update channel
|
2023-03-03 22:49:31 +00:00
|
|
|
entities = []
|
|
|
|
text = ""
|
|
|
|
text += addEntity(entities, text, "hashtag", f"#{'PRE_RELEASE' if 'preview' in VERSION_NAME else 'RELEASE'}")
|
|
|
|
text += " "
|
|
|
|
text += addEntity(entities, text, "text_link", " ", f'https://t.me/{APK_CHANNEL_ID.replace("@","")}/{apk_channel_first_id + 1}')
|
|
|
|
text += " "
|
|
|
|
text += addEntity(entities, text, "bold", VERSION_NAME)
|
|
|
|
text += "\n\n"
|
2023-03-15 23:39:52 +00:00
|
|
|
if "entities" not in admin_resp:
|
|
|
|
admin_resp["entities"] = list()
|
2023-03-03 22:49:31 +00:00
|
|
|
resp_entities = admin_resp["entities"]
|
|
|
|
for en in resp_entities:
|
|
|
|
copy = en.copy()
|
|
|
|
copy["offset"] += len(text)
|
|
|
|
entities.append(copy)
|
|
|
|
text += admin_resp["text"]
|
|
|
|
text += "\n\n"
|
|
|
|
text += addEntity(entities, text, "text_link", "GitHub Release", f"https://github.com/NekoX-Dev/NekoX/releases/{VERSION_NAME}")
|
|
|
|
text += " | "
|
|
|
|
text += addEntity(entities, text, "text_link", "Apks", f'https://t.me/{APK_CHANNEL_ID.replace("@","")}/{apk_channel_first_id}')
|
|
|
|
text += " | "
|
|
|
|
text += addEntity(entities, text, "text_link", "Check Update", "tg://update/")
|
|
|
|
|
|
|
|
sendMessage(text, user_id=UPDATE_CHANNEL_ID, entities=entities)
|
2023-01-21 15:31:27 +00:00
|
|
|
|
|
|
|
# send release message to metadata channel
|
2023-03-03 22:49:31 +00:00
|
|
|
mid = sendMessage(admin_resp["text"], user_id=UPDATE_METADATA_CHANNEL_ID, entities=admin_resp["entities"])
|
2023-01-21 15:31:27 +00:00
|
|
|
meta_msg = f"{VERSION_NAME},{VERSION_CODE},{apk_channel_first_id},{mid}"
|
|
|
|
sendMessage(meta_msg, user_id=UPDATE_METADATA_CHANNEL_ID)
|
|
|
|
|
|
|
|
|
|
|
|
def sendCIRelease():
|
|
|
|
apks = os.listdir(APK_FOLDER)
|
|
|
|
apks.sort()
|
|
|
|
apk = os.path.join(APK_FOLDER, apks[0])
|
2023-03-06 18:02:59 +00:00
|
|
|
entities = []
|
|
|
|
message = f"CI Build\n\n{COMMIT_MESSAGE}\n\n"
|
|
|
|
message += addEntity(entities, message, "text_link", COMMIT_HASH[0:8], f"https://github.com/NekoX-Dev/NekoX/commit/{COMMIT_HASH}")
|
|
|
|
sendDocument(user_id=CI_CHANNEL_ID, path = apk, message=message, )
|
2023-01-21 15:31:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-03-03 02:13:12 +00:00
|
|
|
print(sys.argv)
|
2023-01-21 15:31:27 +00:00
|
|
|
if len(sys.argv) != 2:
|
|
|
|
print("Run Type: release, ci, debug")
|
2023-03-15 23:39:52 +00:00
|
|
|
sys.stdout.flush()
|
|
|
|
sys.stderr.flush()
|
2023-03-03 02:13:12 +00:00
|
|
|
exit(1)
|
2023-01-21 15:31:27 +00:00
|
|
|
mode = sys.argv[1]
|
2023-03-15 23:39:52 +00:00
|
|
|
if mode == "release":
|
|
|
|
sendRelease()
|
|
|
|
elif mode == "ci":
|
|
|
|
if COMMIT_MESSAGE.startswith("ci"):
|
|
|
|
CI_CHANNEL_ID = BOT_TARGET
|
|
|
|
sendCIRelease()
|
|
|
|
elif mode == "debug":
|
|
|
|
APK_CHANNEL_ID = "@test_channel_nekox"
|
|
|
|
UPDATE_CHANNEL_ID = "@test_channel_nekox"
|
|
|
|
UPDATE_METADATA_CHANNEL_ID = "@test_channel_nekox"
|
|
|
|
sendRelease()
|
|
|
|
else:
|
|
|
|
print("unknown mode")
|
2023-01-21 15:31:27 +00:00
|
|
|
|