├── .gitignore ├── .glitch-assets ├── requirements.txt ├── start.sh ├── README.MD └── run.py /.gitignore: -------------------------------------------------------------------------------- 1 | .venv -------------------------------------------------------------------------------- /.glitch-assets: -------------------------------------------------------------------------------- 1 | {} -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests -------------------------------------------------------------------------------- /start.sh: -------------------------------------------------------------------------------- 1 | pip3 install -r requirements.txt 2 | python3 run.py -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | # glitch-gocq 2 | 3 | 在glitch平台上运行go-cqhttp,附带keeplive支持 4 | 5 | Glitch平台注册地址 https://glitch.com/ 6 | 7 | # 使用步骤 8 | 9 | 1. 按照go-cqhttp的文档在你的电脑上运行go-cqhttp,并完成登录 10 | 11 | 参考文档https://docs.go-cqhttp.org/guide/#go-cqhttp 12 | 2. 打开`config.yml`进行修改 13 | 14 | 请注意,必须要将某一个服务的端口改为`3000`,否则glitch无法识别,只能同时使用一个服务(所有的API调用必须经过API网关); 15 | 例如开启正向ws之后就不能使用http。如果你使用的是反向ws,请再添加一个其他的正向服务来让glitch识别。 16 | 17 | 我们强烈建议你设置一个api_token防止恶意访问 18 | 19 | 3. 导入项目到glitch 20 | 在Glitch中创建一个新的项目,项目类型必须要选择`glitch-hello-node`,在左下角的tools里面找到导入,选择使用GitHub导入并输入本项目的名字`mzdluo123 21 | /glitch_gocq`,即可导入 22 | 23 | 4. 传递配置文件 24 | 25 | 打开 https://www.sojson.com/base64.html 将你的`config.yml`进行BASE64编码 ,在Glitch的左边栏找到`.env`, 26 | 在里面新增一个叫做`CONF`的环境变量,将编码后的内容填进去。对`device.json`文件也需要使用相同的操作,对应的环境变量名称为`DEVICE` 27 | 28 | 5. 配置保活 29 | 30 | 在你的项目主页面左上角点击`show`,并点击`In a new Window`,复制此时的URL,添加一个叫做`URL`的环境变量并将这个url填进去。 31 | 32 | 同时这个URL也是访问go-cqhttpAPI所需要的url 33 | 34 | 5. 开始使用 35 | 36 | 在左下角的tools内打开终端,敲`refresh`并回车即可使用 37 | 38 | 39 | 40 | # FAQ 41 | 42 | ## 遇到验证码怎么办 43 | 44 | 在终端输入 45 | ```shell 46 | curl --upload-file ./.data/qrcode.png https://transfer.sh/qrcode.png 47 | ``` 48 | 你会得到一个链接,把链接复制到浏览器里打开,用你要登陆那个账户扫描下面的二维码并授权即可 49 | 50 | ## 手动上传项目到Glitch 51 | 52 | 打开终端,输入下面的命令 53 | 54 | ```shell 55 | git config receive.denyCurrentBranch updateInstead 56 | ``` 57 | 即可使用导入菜单内的Git仓库地址上传你的代码 58 | 59 | 更多操作请看我的博客 https://rainchan.win/2021/01/26/%E5%9C%A8Glitch%E4%B8%8A%E9%83%A8%E7%BD%B2%E4%BD%A0%E7%9A%84Web%E5%BA%94%E7%94%A8/ -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | import base64 2 | import logging 3 | import os 4 | import subprocess 5 | import sys 6 | import threading 7 | import time 8 | from pathlib import Path 9 | 10 | import requests 11 | 12 | HEADERS = { 13 | "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67"} 14 | 15 | 16 | def get_down_url(): 17 | rsp = requests.get("https://api.github.com/repos/Mrs4s/go-cqhttp/releases/latest") 18 | rsp = rsp.json() 19 | print(f"Latest go-cqhttp version:{rsp['tag_name']}") 20 | for i in rsp["assets"]: 21 | if "linux_amd64.tar.gz" in i["name"]: 22 | return i["browser_download_url"] 23 | 24 | 25 | def check_runnable(): 26 | if not Path("./.data", "go-cqhttp").exists(): 27 | down_url = get_down_url() 28 | content = requests.get(down_url, headers=HEADERS) 29 | with open(Path(".data", "download.tar.gz"), "wb")as file: 30 | file.write(content.content) 31 | os.system("cd ./.data/ && tar -zxvf ./download.tar.gz") 32 | 33 | os.chmod("./.data/go-cqhttp", 755) 34 | 35 | 36 | def run_gocq(): 37 | while True: 38 | process = subprocess.Popen("cd ./.data && ./go-cqhttp faststart", shell=True, stdin=sys.stdin, 39 | stdout=sys.stdout, 40 | stderr=sys.stderr) 41 | process.wait() 42 | time.sleep(10) 43 | 44 | 45 | def keep_live(url: str): 46 | while True: 47 | try: 48 | requests.get(url, headers=HEADERS) 49 | except Exception as e: 50 | logging.exception(e) 51 | time.sleep(60) 52 | 53 | 54 | def mv_config(env, file_name): 55 | if os.environ.get(env, None) is not None: 56 | file = Path(f"./.data/{file_name}") 57 | if file.exists(): 58 | os.remove(file) 59 | with open(file, "w") as file: 60 | content = base64.b64decode(os.environ[env]).decode() 61 | file.write(content) 62 | 63 | 64 | if __name__ == '__main__': 65 | if not Path(".data").exists(): 66 | os.mkdir(".data") 67 | check_runnable() 68 | mv_config("CONF", "config.yml") 69 | mv_config("DEVICE", "devices.json") 70 | # os.system("refresh") # 刷新glitch 71 | # os.system("git reset HEAD .") 72 | gocq_thread = threading.Thread(target=run_gocq) 73 | gocq_thread.isDaemon() 74 | gocq_thread.start() 75 | keep_live(os.environ["URL"]) 76 | --------------------------------------------------------------------------------