新手向 Ubuntu linux服务器搭建部署QQ机器人方法
<h1> </h1><h1>本人博客</h1>
<h1><br>https://blog.seclusion.work</h1>
<h1> </h1>
<h1> </h1>
<h1><span style="font-family: "Microsoft YaHei"">序言</span></h1>
<h4><span style="font-family: "Microsoft YaHei"">本篇文章记录我搭建QQ机器人的步骤及踩过的坑</span></h4>
<h4><span style="font-family: "Microsoft YaHei"">使用的是 go-cqhttp 项目,基于 mirai 以及 MiraiGo <span class="sr-only">(opens new window)</span>开发的 cqhttp golang 原生实现, 并在 cqhttp 原版 <span class="sr-only">(opens new window)</span>的基础上做了部分修改和拓展。</span></h4>
<h4><span style="font-family: "Microsoft YaHei"">我使用的python语言编写功能模块</span></h4>
<h1><span style="font-family: "Microsoft YaHei"">大致流程</span></h1>
<h4><span style="font-family: "Microsoft YaHei"">一 : 下载解压go-cqhttp</span></h4>
<h4><span style="font-family: "Microsoft YaHei"">二 : 开放端口</span></h4>
<h4><span style="font-family: "Microsoft YaHei"">三 : 运行配置go-cqhttp</span></h4>
<h4><span style="font-family: "Microsoft YaHei"">四 : 编写机器人功能模块</span></h4>
<p> </p>
<h1><span style="font-family: "Microsoft YaHei"">正文</span></h1>
<h3><span style="font-family: "Microsoft YaHei"">一 : 下载解压go-cqhttp</span></h3>
<p><span style="font-family: "Microsoft YaHei""> 因为服务器不便于下载解压与编写模块,所以我建议在window下完成</span></p>
<p><span style="font-family: "Microsoft YaHei""> 下载go-cqhttp最新版本到本地并解压 https://github.com/Mrs4s/go-cqhttp/releases 注意下载对应的 Linux版</span></p>
<p><span style="font-family: "Microsoft YaHei""> 使用ssh工具上传到服务器的指定目录,我使用的是 FinalShell http://www.hostbuf.com/downloads/finalshell_install.exe</span></p>
<p><span style="font-family: "Microsoft YaHei""> <img src="https://img2020.cnblogs.com/blog/2179308/202105/2179308-20210527142040642-15755714.png"></span></p>
<p><span style="font-family: "Microsoft YaHei""> <img src="https://img2020.cnblogs.com/blog/2179308/202105/2179308-20210527142058169-267938916.png"></span></p>
<p><span style="font-family: "Microsoft YaHei""> 连接完在软件下方出现的文件夹中,创建你能记住的机器人目录,并上传刚刚下载解压好的go-cqhttp文件夹</span></p>
<p><span style="font-family: "Microsoft YaHei""> <img src="https://img2020.cnblogs.com/blog/2179308/202105/2179308-20210527142604851-257203472.png"></span></p>
<p><span style="font-family: "Microsoft YaHei""> </span></p>
<h3><span style="font-family: "Microsoft YaHei"">二 : 开放端口</span></h3>
<p><span style="font-family: "Microsoft YaHei""> 这个端口是机器人处理事务用的端口</span></p>
<p><span style="font-family: "Microsoft YaHei""> 开放的端口号随意,你记住就行 但不能是80,8080,8181,3306,443等等常见端口,会占用</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: "Microsoft YaHei""><span style="color: rgba(0, 128, 128, 1)">1</span> ufw allow <span style="color: rgba(128, 0, 128, 1)">1314</span>
<span style="color: rgba(0, 128, 128, 1)">2</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> iptables -I INPUT -p tcp --dport <span style="color: rgba(128, 0, 128, 1)">1314</span> -<span style="color: rgba(0, 0, 0, 1)">j ACCEPT
</span><span style="color: rgba(0, 128, 128, 1)">4</span>
<span style="color: rgba(0, 128, 128, 1)">5</span> iptables-save > /etc/<span style="color: rgba(0, 0, 0, 1)">iptables.up.rules
</span><span style="color: rgba(0, 128, 128, 1)">6</span>
<span style="color: rgba(0, 128, 128, 1)">7</span> iptables-restore < /etc/iptables.up.rules</span></pre>
</div>
<p><span style="font-family: "Microsoft YaHei""> 显然我这里开放的是1314端口</span></p>
<p> </p>
<h3><span style="font-family: "Microsoft YaHei"">三 : 运行配置</span></h3>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"><span style="font-family: "Microsoft YaHei"">cd /NaiveBot/goqq/ //进入到你的go-cq'http主程序目录
./go-cqhttp //现在是第一次运行,会报错退出并生成一个config.hjson文件
</span></pre>
</div>
<p><span style="font-family: "Microsoft YaHei""> 如果你使用的是我前文推荐的 FinalShell ,可以在软件下方文件夹中右键config文件,选择我们window上的软件打开并编译</span></p>
<p><span style="font-family: "Microsoft YaHei""> 所以我是在window的Sublime编辑的</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: "Microsoft YaHei""><span style="color: rgba(0, 128, 128, 1)">1</span> <span style="color: rgba(0, 0, 0, 1)">/*
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 0, 0, 1)"> go-cqhttp 默认配置文件
</span><span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 0, 1)">*/
</span><span style="color: rgba(0, 128, 128, 1)">4</span>
<span style="color: rgba(0, 128, 128, 1)">5</span> <span style="color: rgba(0, 0, 0, 1)">{
</span><span style="color: rgba(0, 128, 128, 1)">6</span> <span style="color: rgba(0, 0, 0, 1)"> // QQ号
</span><span style="color: rgba(0, 128, 128, 1)">7</span> <span style="color: rgba(0, 0, 0, 1)"> uin: 6666666
</span><span style="color: rgba(0, 128, 128, 1)">8</span> <span style="color: rgba(0, 0, 0, 1)"> // QQ密码
</span><span style="color: rgba(0, 128, 128, 1)">9</span> <span style="color: rgba(0, 0, 0, 1)"> password: "6666666666"
</span><span style="color: rgba(0, 128, 128, 1)"> 10</span> <span style="color: rgba(0, 0, 0, 1)"> // 是否启用密码加密
</span><span style="color: rgba(0, 128, 128, 1)"> 11</span> <span style="color: rgba(0, 0, 0, 1)"> encrypt_password: false
</span><span style="color: rgba(0, 128, 128, 1)"> 12</span> <span style="color: rgba(0, 0, 0, 1)"> // 加密后的密码, 如未启用密码加密将为空, 请勿随意修改.
</span><span style="color: rgba(0, 128, 128, 1)"> 13</span> <span style="color: rgba(0, 0, 0, 1)"> password_encrypted: ""
</span><span style="color: rgba(0, 128, 128, 1)"> 14</span> <span style="color: rgba(0, 0, 0, 1)"> status: 4
</span><span style="color: rgba(0, 128, 128, 1)"> 15</span> <span style="color: rgba(0, 0, 0, 1)"> // 是否启用内置数据库
</span><span style="color: rgba(0, 128, 128, 1)"> 16</span> <span style="color: rgba(0, 0, 0, 1)"> // 启用将会增加10-20MB的内存占用和一定的磁盘空间
</span><span style="color: rgba(0, 128, 128, 1)"> 17</span> <span style="color: rgba(0, 0, 0, 1)"> // 关闭将无法使用 撤回 回复 get_msg 等上下文相关功能
</span><span style="color: rgba(0, 128, 128, 1)"> 18</span> <span style="color: rgba(0, 0, 0, 1)"> enable_db: true
</span><span style="color: rgba(0, 128, 128, 1)"> 19</span> <span style="color: rgba(0, 0, 0, 1)"> // 访问密钥, 强烈推荐在公网的服务器设置
</span><span style="color: rgba(0, 128, 128, 1)"> 20</span> <span style="color: rgba(0, 0, 0, 1)"> access_token: ""
</span><span style="color: rgba(0, 128, 128, 1)"> 21</span> <span style="color: rgba(0, 0, 0, 1)"> // 重连设置
</span><span style="color: rgba(0, 128, 128, 1)"> 22</span> <span style="color: rgba(0, 0, 0, 1)"> relogin: {
</span><span style="color: rgba(0, 128, 128, 1)"> 23</span> <span style="color: rgba(0, 0, 0, 1)"> // 是否启用自动重连
</span><span style="color: rgba(0, 128, 128, 1)"> 24</span> <span style="color: rgba(0, 0, 0, 1)"> // 如不启用掉线后将不会自动重连
</span><span style="color: rgba(0, 128, 128, 1)"> 25</span> <span style="color: rgba(0, 0, 0, 1)"> enabled: true
</span><span style="color: rgba(0, 128, 128, 1)"> 26</span> <span style="color: rgba(0, 0, 0, 1)"> // 重连延迟, 单位秒
</span><span style="color: rgba(0, 128, 128, 1)"> 27</span> <span style="color: rgba(0, 0, 0, 1)"> relogin_delay: 3
</span><span style="color: rgba(0, 128, 128, 1)"> 28</span> <span style="color: rgba(0, 0, 0, 1)"> // 最大重连次数, 0为无限制
</span><span style="color: rgba(0, 128, 128, 1)"> 29</span> <span style="color: rgba(0, 0, 0, 1)"> max_relogin_times: 0
</span><span style="color: rgba(0, 128, 128, 1)"> 30</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 31</span> <span style="color: rgba(0, 0, 0, 1)"> // API限速设置
</span><span style="color: rgba(0, 128, 128, 1)"> 32</span> <span style="color: rgba(0, 0, 0, 1)"> // 该设置为全局生效
</span><span style="color: rgba(0, 128, 128, 1)"> 33</span> <span style="color: rgba(0, 0, 0, 1)"> // 原 cqhttp 虽然启用了 rate_limit 后缀, 但是基本没插件适配
</span><span style="color: rgba(0, 128, 128, 1)"> 34</span> <span style="color: rgba(0, 0, 0, 1)"> // 目前该限速设置为令牌桶算法, 请参考:
</span><span style="color: rgba(0, 128, 128, 1)"> 35</span> <span style="color: rgba(0, 0, 0, 1)"> // https://baike.baidu.com/item/%E4%BB%A4%E7%89%8C%E6%A1%B6%E7%AE%97%E6%B3%95/6597000?fr=aladdin
</span><span style="color: rgba(0, 128, 128, 1)"> 36</span> <span style="color: rgba(0, 0, 0, 1)"> _rate_limit: {
</span><span style="color: rgba(0, 128, 128, 1)"> 37</span> <span style="color: rgba(0, 0, 0, 1)"> // 是否启用限速
</span><span style="color: rgba(0, 128, 128, 1)"> 38</span> <span style="color: rgba(0, 0, 0, 1)"> enabled: false
</span><span style="color: rgba(0, 128, 128, 1)"> 39</span> <span style="color: rgba(0, 0, 0, 1)"> // 令牌回复频率, 单位秒
</span><span style="color: rgba(0, 128, 128, 1)"> 40</span> <span style="color: rgba(0, 0, 0, 1)"> frequency: 1
</span><span style="color: rgba(0, 128, 128, 1)"> 41</span> <span style="color: rgba(0, 0, 0, 1)"> // 令牌桶大小
</span><span style="color: rgba(0, 128, 128, 1)"> 42</span> <span style="color: rgba(0, 0, 0, 1)"> bucket_size: 1
</span><span style="color: rgba(0, 128, 128, 1)"> 43</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 44</span> <span style="color: rgba(0, 0, 0, 1)"> // 是否忽略无效的CQ码
</span><span style="color: rgba(0, 128, 128, 1)"> 45</span> <span style="color: rgba(0, 0, 0, 1)"> // 如果为假将原样发送
</span><span style="color: rgba(0, 128, 128, 1)"> 46</span> <span style="color: rgba(0, 0, 0, 1)"> ignore_invalid_cqcode: false
</span><span style="color: rgba(0, 128, 128, 1)"> 47</span> <span style="color: rgba(0, 0, 0, 1)"> // 是否强制分片发送消息
</span><span style="color: rgba(0, 128, 128, 1)"> 48</span> <span style="color: rgba(0, 0, 0, 1)"> // 分片发送将会带来更快的速度
</span><span style="color: rgba(0, 128, 128, 1)"> 49</span> <span style="color: rgba(0, 0, 0, 1)"> // 但是兼容性会有些问题
</span><span style="color: rgba(0, 128, 128, 1)"> 50</span> <span style="color: rgba(0, 0, 0, 1)"> force_fragmented: false
</span><span style="color: rgba(0, 128, 128, 1)"> 51</span> <span style="color: rgba(0, 0, 0, 1)"> // 心跳频率, 单位秒
</span><span style="color: rgba(0, 128, 128, 1)"> 52</span> <span style="color: rgba(0, 0, 0, 1)"> // -1 为关闭心跳
</span><span style="color: rgba(0, 128, 128, 1)"> 53</span> <span style="color: rgba(0, 0, 0, 1)"> heartbeat_interval: -1
</span><span style="color: rgba(0, 128, 128, 1)"> 54</span> <span style="color: rgba(0, 0, 0, 1)"> // HTTP设置
</span><span style="color: rgba(0, 128, 128, 1)"> 55</span> <span style="color: rgba(0, 0, 0, 1)"> http_config: {
</span><span style="color: rgba(0, 128, 128, 1)"> 56</span> <span style="color: rgba(0, 0, 0, 1)"> // 是否启用正向HTTP服务器
</span><span style="color: rgba(0, 128, 128, 1)"> 57</span> <span style="color: rgba(0, 0, 0, 1)"> enabled: true
</span><span style="color: rgba(0, 128, 128, 1)"> 58</span> <span style="color: rgba(0, 0, 0, 1)"> // 服务端监听地址
</span><span style="color: rgba(0, 128, 128, 1)"> 59</span> <span style="color: rgba(0, 0, 0, 1)"> //host:
</span><span style="color: rgba(0, 128, 128, 1)"> 60</span> <span style="color: rgba(0, 0, 0, 1)"> // 服务端监听端口
</span><span style="color: rgba(0, 128, 128, 1)"> 61</span> <span style="color: rgba(0, 0, 0, 1)"> //port:
</span><span style="color: rgba(0, 128, 128, 1)"> 62</span> <span style="color: rgba(0, 0, 0, 1)"> // 反向HTTP超时时间, 单位秒
</span><span style="color: rgba(0, 128, 128, 1)"> 63</span> <span style="color: rgba(0, 0, 0, 1)"> // 最小值为5,小于5将会忽略本项设置
</span><span style="color: rgba(0, 128, 128, 1)"> 64</span> <span style="color: rgba(0, 0, 0, 1)"> timeout: 0
</span><span style="color: rgba(0, 128, 128, 1)"> 65</span> <span style="color: rgba(0, 0, 0, 1)"> // 反向HTTP POST地址列表
</span><span style="color: rgba(0, 128, 128, 1)"> 66</span> <span style="color: rgba(0, 0, 0, 1)"> // 格式:
</span><span style="color: rgba(0, 128, 128, 1)"> 67</span> <span style="color: rgba(0, 0, 0, 1)"> // {
</span><span style="color: rgba(0, 128, 128, 1)"> 68</span> <span style="color: rgba(0, 0, 0, 1)"> // 地址: secret
</span><span style="color: rgba(0, 128, 128, 1)"> 69</span> <span style="color: rgba(0, 0, 0, 1)"> // }
</span><span style="color: rgba(0, 128, 128, 1)"> 70</span> <span style="color: rgba(0, 0, 0, 1)"> post_urls: {}
</span><span style="color: rgba(0, 128, 128, 1)"> 71</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 72</span> <span style="color: rgba(0, 0, 0, 1)"> // 正向WS设置
</span><span style="color: rgba(0, 128, 128, 1)"> 73</span> <span style="color: rgba(0, 0, 0, 1)"> ws_config: {
</span><span style="color: rgba(0, 128, 128, 1)"> 74</span> <span style="color: rgba(0, 0, 0, 1)"> // 是否启用正向WS服务器
</span><span style="color: rgba(0, 128, 128, 1)"> 75</span> <span style="color: rgba(0, 0, 0, 1)"> enabled: false
</span><span style="color: rgba(0, 128, 128, 1)"> 76</span> <span style="color: rgba(0, 0, 0, 1)"> // 正向WS服务器监听地址
</span><span style="color: rgba(0, 128, 128, 1)"> 77</span> <span style="color: rgba(0, 0, 0, 1)"> host: 0.0.0.0
</span><span style="color: rgba(0, 128, 128, 1)"> 78</span> <span style="color: rgba(0, 0, 0, 1)"> // 正向WS服务器监听端口
</span><span style="color: rgba(0, 128, 128, 1)"> 79</span> <span style="color: rgba(0, 0, 0, 1)"> port: 6700
</span><span style="color: rgba(0, 128, 128, 1)"> 80</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 81</span> <span style="color: rgba(0, 0, 0, 1)"> // 反向WS设置
</span><span style="color: rgba(0, 128, 128, 1)"> 82</span> <span style="color: rgba(0, 0, 0, 1)"> ws_reverse_servers: [
</span><span style="color: rgba(0, 128, 128, 1)"> 83</span> <span style="color: rgba(0, 0, 0, 1)"> // 可以添加多个反向WS推送
</span><span style="color: rgba(0, 128, 128, 1)"> 84</span> <span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 128, 1)"> 85</span> <span style="color: rgba(0, 0, 0, 1)"> // 是否启用该推送
</span><span style="color: rgba(0, 128, 128, 1)"> 86</span> <span style="color: rgba(0, 0, 0, 1)"> enabled: true
</span><span style="color: rgba(0, 128, 128, 1)"> 87</span> <span style="color: rgba(0, 0, 0, 1)"> // 反向WS Universal 地址
</span><span style="color: rgba(0, 128, 128, 1)"> 88</span> <span style="color: rgba(0, 0, 0, 1)"> // 注意 设置了此项地址后下面两项将会被忽略
</span><span style="color: rgba(0, 128, 128, 1)"> 89</span> <span style="color: rgba(0, 0, 0, 1)"> // 留空请使用 ""
</span><span style="color: rgba(0, 128, 128, 1)"> 90</span> <span style="color: rgba(0, 0, 0, 1)"> reverse_url: ws://127.0.0.4:1314/ws
</span><span style="color: rgba(0, 128, 128, 1)"> 91</span> <span style="color: rgba(0, 0, 0, 1)"> // 反向WS API 地址
</span><span style="color: rgba(0, 128, 128, 1)"> 92</span> <span style="color: rgba(0, 0, 0, 1)"> reverse_api_url: ws://you_websocket_api.server
</span><span style="color: rgba(0, 128, 128, 1)"> 93</span> <span style="color: rgba(0, 0, 0, 1)"> // 反向WS Event 地址
</span><span style="color: rgba(0, 128, 128, 1)"> 94</span> <span style="color: rgba(0, 0, 0, 1)"> reverse_event_url: ws://you_websocket_event.server
</span><span style="color: rgba(0, 128, 128, 1)"> 95</span> <span style="color: rgba(0, 0, 0, 1)"> // 重连间隔 单位毫秒
</span><span style="color: rgba(0, 128, 128, 1)"> 96</span> <span style="color: rgba(0, 0, 0, 1)"> reverse_reconnect_interval: 3000
</span><span style="color: rgba(0, 128, 128, 1)"> 97</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)"> 98</span> <span style="color: rgba(0, 0, 0, 1)"> ]
</span><span style="color: rgba(0, 128, 128, 1)"> 99</span> <span style="color: rgba(0, 0, 0, 1)"> // 上报数据类型
</span><span style="color: rgba(0, 128, 128, 1)">100</span> <span style="color: rgba(0, 0, 0, 1)"> // 可选: string array
</span><span style="color: rgba(0, 128, 128, 1)">101</span> <span style="color: rgba(0, 0, 0, 1)"> post_message_format: string
</span><span style="color: rgba(0, 128, 128, 1)">102</span> <span style="color: rgba(0, 0, 0, 1)"> // 是否使用服务器下发的新地址进行重连
</span><span style="color: rgba(0, 128, 128, 1)">103</span> <span style="color: rgba(0, 0, 0, 1)"> // 注意, 此设置可能导致在海外服务器上连接情况更差
</span><span style="color: rgba(0, 128, 128, 1)">104</span> <span style="color: rgba(0, 0, 0, 1)"> use_sso_address: false
</span><span style="color: rgba(0, 128, 128, 1)">105</span> <span style="color: rgba(0, 0, 0, 1)"> // 是否启用 DEBUG
</span><span style="color: rgba(0, 128, 128, 1)">106</span> <span style="color: rgba(0, 0, 0, 1)"> debug: false
</span><span style="color: rgba(0, 128, 128, 1)">107</span> <span style="color: rgba(0, 0, 0, 1)"> // 日志等级 trace,debug,info,warn,error
</span><span style="color: rgba(0, 128, 128, 1)">108</span> <span style="color: rgba(0, 0, 0, 1)"> log_level: "info"
</span><span style="color: rgba(0, 128, 128, 1)">109</span> <span style="color: rgba(0, 0, 0, 1)"> // WebUi 设置
</span><span style="color: rgba(0, 128, 128, 1)">110</span> <span style="color: rgba(0, 0, 0, 1)"> web_ui: {
</span><span style="color: rgba(0, 128, 128, 1)">111</span> <span style="color: rgba(0, 0, 0, 1)"> // 是否启用 WebUi
</span><span style="color: rgba(0, 128, 128, 1)">112</span> <span style="color: rgba(0, 0, 0, 1)"> enabled: false
</span><span style="color: rgba(0, 128, 128, 1)">113</span> <span style="color: rgba(0, 0, 0, 1)"> // 监听地址
</span><span style="color: rgba(0, 128, 128, 1)">114</span> <span style="color: rgba(0, 0, 0, 1)"> host : 127.0.0.6
</span><span style="color: rgba(0, 128, 128, 1)">115</span> <span style="color: rgba(0, 0, 0, 1)"> // 监听端口
</span><span style="color: rgba(0, 128, 128, 1)">116</span> <span style="color: rgba(0, 0, 0, 1)"> web_ui_port: 1316
</span><span style="color: rgba(0, 128, 128, 1)">117</span> <span style="color: rgba(0, 0, 0, 1)"> // 是否接收来自web的输入
</span><span style="color: rgba(0, 128, 128, 1)">118</span> <span style="color: rgba(0, 0, 0, 1)"> web_input: false
</span><span style="color: rgba(0, 128, 128, 1)">119</span> <span style="color: rgba(0, 0, 0, 1)"> }
</span><span style="color: rgba(0, 128, 128, 1)">120</span> }</span></pre>
</div>
<p><span style="font-family: "Microsoft YaHei""> 配置如上面代码,都有注释</span></p>
<p><span style="font-family: "Microsoft YaHei""> 其中uin为机器人QQ号 password为密码 status为登入协议 </span></p>
<p><span style="font-family: "Microsoft YaHei""> 我采用了<em id="__mceDel"><em id="__mceDel"><em id="__mceDel"><em id="__mceDel">Websocket反向接口,所以只开启了86行的代码,并在90行位置填写你监听的IP地址和第二步开放的端口号</em></em></em></em></span></p>
<p><span style="font-family: "Microsoft YaHei""><em><em><em><em> </em></em></em></em><em id="__mceDel" style="font-family: "Courier New"; font-size: 12px">reverse_url: ws://127.0.0.4:1314/ws //其中的127.0.0.4随意写,1314改成你<em id="__mceDel"><em id="__mceDel"><em id="__mceDel"><em id="__mceDel">第二步开放的端口号</em></em></em></em></em></span></p>
<p><span style="font-family: "Microsoft YaHei""><em style="font-family: "Courier New"; font-size: 12px"> 至此,再次运行go-cqhttp</em></span></p>
<p><span style="font-family: "Microsoft YaHei""><em style="font-family: "Courier New"; font-size: 12px"> </em></span></p>
<div class="cnblogs_code">
<pre><span style="font-family: "Microsoft YaHei"">./go-cqhttp</span></pre>
</div>
<p><span style="font-family: "Microsoft YaHei""> 不出意外能登入,但要验证,聪明的你自己解决验证问题</span></p>
<p><span style="font-family: "Microsoft YaHei""> 我在这里给出滑块验证的官方参考 https://docs.go-cqhttp.org/faq/slider.html</span></p>
<p><span style="font-family: "Microsoft YaHei""> 解决完验证后, 向机器人发送消息,你的命令符已经能看见了</span></p>
<p><span style="font-family: "Microsoft YaHei""> <img src="https://img2020.cnblogs.com/blog/2179308/202105/2179308-20210527145743153-1784364978.png"></span></p>
<p><span style="font-family: "Microsoft YaHei""> </span></p>
<p><span style="font-family: "Microsoft YaHei""> 如果呀,在登入时出现端口占用的提示,执行如下步骤</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1); font-family: "Microsoft YaHei"">root@yzg-server:/NaiveBot/goqq# netstat -lnp | grep 1314
tcp 0 0 127.0.0.4:1314 0.0.0.0:* LISTEN 3140/python3</span></pre>
</div>
<p><span style="font-family: "Microsoft YaHei""> 命令是 <em id="__mceDel" style="font-family: "Courier New"; font-size: 12px">netstat -lnp | grep 端口号</em></span></p>
<p><span style="font-family: "Microsoft YaHei""><em style="font-family: "Courier New"; font-size: 12px"> 可以看到是python3占用,他的进程号是 3140 ,我们接下来杀这个进程</em></span></p>
<p><span style="font-family: "Microsoft YaHei""><em style="font-family: "Courier New"; font-size: 12px"> </em></span></p>
<div class="cnblogs_code">
<pre><span style="font-family: "Microsoft YaHei"">kill -9 3410</span></pre>
</div>
<p><span style="font-family: "Microsoft YaHei""> 现在可以重新运行go,妙手回春</span></p>
<p> </p>
<h3><span style="font-family: "Microsoft YaHei""><em style="font-family: "Courier New"; font-size: 12px">四 : 编写功能模块</em></span></h3>
<p><span style="font-family: "Microsoft YaHei""><em style="font-family: "Courier New"; font-size: 12px"> 给了我写的服务,按下文参考内容 https://nepenthicstore.lanzoui.com/iqx5upitn5a</em></span></p>
<p><span style="font-family: "Microsoft YaHei""><em style="font-family: "Courier New"; font-size: 12px"> 唉,涉及了python环境问题,Ubuntu20.4以上是自带了python3,淘汰了2,查询自己是否有环境如下</em></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1); font-family: "Microsoft YaHei"">root@yzg-server:/NaiveBot/goqq# python3
Python 3.8.5 (default, Jan 27 2021, 15:41:15)
on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()</span></pre>
</div>
<p><span style="font-family: "Microsoft YaHei""> 我这里自带了3.8.5,如果你没有的话,参考一下网上的方法我就不多言 https://www.jianshu.com/p/fe0b278b8916</span></p>
<p><span style="font-family: "Microsoft YaHei""> 接下来我就默认你配置好了python呦</span></p>
<p> </p>
<p><span style="font-family: "Microsoft YaHei""> 不管你用什么程序写python,新建一个项目,项目名随意,我使用了我机器人的名字Naive</span></p>
<p><span style="font-family: "Microsoft YaHei""> 项目里新建 bot.py bot_config.py apps目录,并在apps目录下再新建plugins目录,这是官方建议</span></p>
<p><span style="font-family: "Microsoft YaHei""><img src="https://img2020.cnblogs.com/blog/2179308/202105/2179308-20210527152301508-1624742469.png"></span></p>
<p><span style="font-family: "Microsoft YaHei""> </span></p>
<p><span style="font-family: "Microsoft YaHei""> bot的文件编写 : </span></p>
<div class="cnblogs_code">
<pre><span style="font-family: "Microsoft YaHei""><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> -*- codeing = utf-8 -*-</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> @TIME : 2021/5/18 5:49</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> @Auther : 幼稚鬼(Naive)</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> @what are you to do? : 机器人入口</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 255, 1)">from</span> os <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> path
</span><span style="color: rgba(0, 128, 128, 1)"> 7</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> nonebot
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span> <span style="color: rgba(0, 0, 255, 1)">from</span> nonebot <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> session
</span><span style="color: rgba(0, 128, 128, 1)"> 9</span>
<span style="color: rgba(0, 128, 128, 1)">10</span> <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> bot_config
</span><span style="color: rgba(0, 128, 128, 1)">11</span>
<span style="color: rgba(0, 128, 128, 1)">12</span> nonebot.init(bot_config)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 引入config配置</span>
<span style="color: rgba(0, 128, 128, 1)">13</span>
<span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 0, 1)">nonebot.load_plugins(
</span><span style="color: rgba(0, 128, 128, 1)">15</span> path.join(path.dirname(<span style="color: rgba(128, 0, 128, 1)">__file__</span>), <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">apps</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">plugins</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">),
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">apps.plugins</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(0, 128, 128, 1)">17</span> )<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 引入插件库 第一个参数是插件目录的路径 第二个参数是导入插件模块时使用的模块名前缀</span>
<span style="color: rgba(0, 128, 128, 1)">18</span>
<span style="color: rgba(0, 128, 128, 1)">19</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 如果使用 asgi</span>
<span style="color: rgba(0, 128, 128, 1)">20</span> bot =<span style="color: rgba(0, 0, 0, 1)"> nonebot.get_bot()
</span><span style="color: rgba(0, 128, 128, 1)">21</span> app =<span style="color: rgba(0, 0, 0, 1)"> bot.asgi
</span><span style="color: rgba(0, 128, 128, 1)">22</span>
<span style="color: rgba(0, 128, 128, 1)">23</span>
<span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span> == <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 128, 128, 1)">25</span> nonebot.run()</span></pre>
</div>
<p> </p>
<p><span style="font-family: "Microsoft YaHei""> bot_config.py文件编写 : 第八行写你管理机器人的QQ非机器人QQ 16,17行写你第三步config文件监听的地址和端口</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: "Microsoft YaHei""><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> -*- codeing = utf-8 -*-</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> @TIME : 2021/5/18 5:51</span>
<span style="color: rgba(0, 128, 128, 1)"> 3</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> @Auther : 幼稚鬼(Naive)</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span> <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> @what are you to do? :机器人的配置</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span> <span style="color: rgba(0, 0, 255, 1)">from</span> datetime <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> timedelta
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span> <span style="color: rgba(0, 0, 255, 1)">from</span> nonebot.default_config <span style="color: rgba(0, 0, 255, 1)">import</span> *
<span style="color: rgba(0, 128, 128, 1)"> 7</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span> SUPERUSERS = {2297813468}<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 可以是 set、list、tuple 等类型,元素类型为 int;超级用户的 QQ</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span>
<span style="color: rgba(0, 128, 128, 1)">10</span> COMMAND_START = {<span style="color: rgba(128, 0, 0, 1)">''</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">!</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">!</span><span style="color: rgba(128, 0, 0, 1)">'</span>}<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 命令头 可以是 list、tuple、set 等任意容器类型,元素类型可以是 str 或正则表达式</span>
<span style="color: rgba(0, 128, 128, 1)">11</span>
<span style="color: rgba(0, 128, 128, 1)">12</span> NICKNAME = {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Naive</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">naive</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">幼稚鬼</span><span style="color: rgba(128, 0, 0, 1)">'</span>}<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 机器人昵称,设定后 "@机器人 天气" 和 "lucia 天气" 效果相同。</span>
<span style="color: rgba(0, 128, 128, 1)">13</span>
<span style="color: rgba(0, 128, 128, 1)">14</span> SESSION_EXPIRE_TIMEOUT = timedelta(minutes=2)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 表示一条命令的超时(没有用户输入)时间。</span>
<span style="color: rgba(0, 128, 128, 1)">15</span>
<span style="color: rgba(0, 128, 128, 1)">16</span> HOST = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">127.0.0.4</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 服务器和端口</span>
<span style="color: rgba(0, 128, 128, 1)">17</span> PORT = 1314
<span style="color: rgba(0, 128, 128, 1)">18</span>
<span style="color: rgba(0, 128, 128, 1)">19</span> DEBUG = False<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 关闭调试输出,提升性能。</span></span></pre>
</div>
<p> </p>
<p><span style="font-family: "Microsoft YaHei""> 现在基本需要已经解决,机器人有了基本处理能力,现在我们实战加一个翻译功能</span></p>
<p><span style="font-family: "Microsoft YaHei""> 在plugins目录下新建一个 translation目录,并在此目录下新建 一个__init__.py</span></p>
<p><span style="font-family: "Microsoft YaHei""><img src="https://img2020.cnblogs.com/blog/2179308/202105/2179308-20210527153242704-760444551.png"></span></p>
<p><span style="font-family: "Microsoft YaHei""> 在 __init__.py 里编写 : </span></p>
<div class="cnblogs_code">
<pre><span style="font-family: "Microsoft YaHei""><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> -*- codeing = utf-8 -*-</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> @TIME : 2021/5/18 20:24</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> @Auther : 幼稚鬼(Naive)</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> @what are you to do? : 实现翻译功能</span>
<span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> requests
</span><span style="color: rgba(0, 0, 255, 1)">from</span> nonebot.command <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> CommandSession
</span><span style="color: rgba(0, 0, 255, 1)">from</span> nonebot.experimental.plugin <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> on_command
</span><span style="color: rgba(128, 0, 128, 1)">__plugin_name__</span> = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">翻译</span><span style="color: rgba(128, 0, 0, 1)">'</span>
<span style="color: rgba(128, 0, 128, 1)">__plugin_usage__</span> = <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">用法 : 翻译 xxxx</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
@on_command(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">翻译</span><span style="color: rgba(128, 0, 0, 1)">'</span>, permission=<span style="color: rgba(0, 0, 255, 1)">lambda</span> sender: sender.is_privatechat <span style="color: rgba(0, 0, 255, 1)">or</span><span style="color: rgba(0, 0, 0, 1)"> sender.is_superuser)
async </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> trans(session: CommandSession):
need </span>= session.get(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">need</span><span style="color: rgba(128, 0, 0, 1)">'</span>, prompt=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">你想翻译的内容呢?</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
answer </span>=<span style="color: rgba(0, 0, 0, 1)"> await transMain(need)
await session.send(answer)
@trans.args_parser
async </span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> _(session: CommandSession):
stripped_arg </span>=<span style="color: rgba(0, 0, 0, 1)"> session.current_arg_text.strip()
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> session.is_first_run:
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> stripped_arg:
session.state[</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">need</span><span style="color: rgba(128, 0, 0, 1)">'</span>] =<span style="color: rgba(0, 0, 0, 1)"> stripped_arg
</span><span style="color: rgba(0, 0, 255, 1)">return</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(0, 0, 255, 1)">not</span><span style="color: rgba(0, 0, 0, 1)"> stripped_arg:
session.pause(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">翻译内容为空</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
session.state </span>=<span style="color: rgba(0, 0, 0, 1)"> stripped_arg
async </span><span style="color: rgba(0, 0, 255, 1)">def</span> transMain(need: str) -><span style="color: rgba(0, 0, 0, 1)"> str:
formdata </span>= {<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 传入翻译内容</span>
<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">q</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">: need,
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">from</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Auto</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">to</span><span style="color: rgba(128, 0, 0, 1)">"</span>: <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Auto</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
}
url </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://aidemo.youdao.com/trans</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
response </span>= requests.post(url, params=<span style="color: rgba(0, 0, 0, 1)">formdata)
answer </span>= str(response.json()[<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">translation</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">])
</span><span style="color: rgba(0, 0, 255, 1)">return</span> answer</span></pre>
</div>
<p> </p>
<p><span style="font-family: "Microsoft YaHei""> 现在翻译功能已经有了,我们把整个Naive项目文件夹通过FinalShell上传到服务器上,前文已教</span></p>
<p><span style="font-family: "Microsoft YaHei""> 学过python的朋友应该知道我们新建的几个py文件使用了一些库,显然服务器的python是没有的,使用我们在服务器上也要安装对应库</span></p>
<p><span style="font-family: "Microsoft YaHei""> 命令是</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: "Microsoft YaHei""><span style="color: rgba(0, 0, 0, 1)">pip install 库名
</span>///<span style="color: rgba(0, 0, 0, 1)">或者是
pip3 install 库名</span></span></pre>
</div>
<p><span style="font-family: "Microsoft YaHei""> </span></p>
<p><span style="font-family: "Microsoft YaHei""> 解决完库问题</span></p>
<p><span style="font-family: "Microsoft YaHei""> cd到机器人目录启动机器人</span></p>
<p><span style="font-family: "Microsoft YaHei""> 然后新建一个命令窗口</span></p>
<p><span style="font-family: "Microsoft YaHei""> cd到bot.py的目录 python3 bot.py 启动服务</span></p>
<p><span style="font-family: "Microsoft YaHei""> 给机器人发 翻译 xxxx,应该能回复</span></p>
<p> </p>
<h3><span style="font-family: "Microsoft YaHei"">以后你要开发的功能也在plugins新建即可,再见!!!</span></h3>
<p><span style="font-family: "Microsoft YaHei""> </span></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/aidoll/p/14818188.html
頁:
[1]