• 欢迎访问情深深几许的博客网站,这里有免费网络资源信息,WordPress教程,Python、MySQL教程
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏本网站吧

使用tg机器人自动下载并上传频道内文件

技术日志 luckzk 3年前 (2022-05-24) 2510次浏览 0个评论

因为需要经常转存tg群里的文件,一旦多起来就得经常手动转存,于是在GitHub找到了一个自动下载并上传到OD、GD的tg机器人源码。搭建过程中,博主使用的是Debian9系统,脚本作者使用的是Ubuntu 18.04.5。其他系统自测。同时,搭建过程需要 python3.6.9 环境,经过实测,通过apt-get自动安装的是 python3.5,运行该脚本会报错。

如果运行运行脚本时出现164行报错,那可能就是你的系统或是环境问题,这一点要注意。

GitHub地址:telegram_channel_downloader

一. Telegram API 申请

进入 https://my.telegram.org/apps ,登录后选择 Apps ,填写相关信息后自动生成 apiId 和 apiHash 等信息。如果之前申请过,进入 Apps 后即可看到。

这个很简单,这里就不啰嗦了。

二. 安装 python3.6.9

1. 安装 aptitude 及 依赖包

<span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install aptitude
sudo aptitude </span><span class="pun">-</span><span class="pln">y install gcc make zlib1g</span><span class="pun">-</span><span class="pln">dev libffi</span><span class="pun">-</span><span class="pln">dev libssl</span><span class="pun">-</span><span class="pln">dev</span>

2. 下载 Python 3.6.9,解压并授予权限

<span class="pln">wget https</span><span class="pun">:</span><span class="com">//www.python.org/ftp/python/3.6.9/Python-3.6.9.tgz</span><span class="pln">
tar </span><span class="pun">-</span><span class="pln">xzvf </span><span class="typ">Python</span><span class="pun">-</span><span class="lit">3.6</span><span class="pun">.</span><span class="lit">9.tgz</span><span class="pln">
chmod </span><span class="pun">-</span><span class="pln">R </span><span class="pun">+</span><span class="pln">x </span><span class="typ">Python</span><span class="pun">-</span><span class="lit">3.6</span><span class="pun">.</span><span class="lit">9</span>

3. 进入文件夹,执行安装配置,开始编译安装

<span class="pln">cd </span><span class="typ">Python</span><span class="pun">-</span><span class="lit">3.6</span><span class="pun">.</span><span class="lit">9</span><span class="pun">/</span>
<span class="pun">./</span><span class="pln">configure </span><span class="pun">--</span><span class="pln">prefix</span><span class="pun">=</span><span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">python36
make </span><span class="pun">&&</span><span class="pln"> make install</span>

4. 对 python 运营文件夹赋予权限

<span class="pln">sudo chmod </span><span class="lit">777</span> <span class="pun">-</span><span class="pln">R </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">python36</span>

5. 添加PATH环境变量

<span class="pln">vim </span><span class="pun">~</span><span class="str">/.bashrc

在文件末尾添加:export PATH=/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">python36</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">:</span><span class="pln">$PATH

source </span><span class="pun">~/.</span><span class="pln">bashrc</span>

6. 添加快捷方式

<span class="pln">ln </span><span class="pun">-</span><span class="pln">s </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">python36</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">python3</span><span class="pun">.</span><span class="lit">6</span> <span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">python3
ln </span><span class="pun">-</span><span class="pln">s </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">python36</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">pip3</span><span class="pun">.</span><span class="lit">6</span> <span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">pip3</span><span class="pun">.</span><span class="lit">6</span><span class="pln">
ln </span><span class="pun">-</span><span class="pln">s </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">python36</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">pip3 </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">pip3
ln </span><span class="pun">-</span><span class="pln">s </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">python36</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">pyvenv</span><span class="pun">-</span><span class="lit">3.6</span> <span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">pyvenv</span>

7. 检查安装是否成功:返回版本信息即代表安装成功

<span class="pln">python3 <span class="pun">-</span>V
pip </span><span class="pun">-</span><span class="pln">V</span>

8. 升级pip3、查询python库需要升级的文件,升级库文件(这一步可做可不做)

<span class="pln">python3 </span><span class="pun">-</span><span class="pln">m pip install </span><span class="pun">--</span><span class="pln">upgrade pip</span>
<span class="pln">pip3 list </span><span class="pun">--</span><span class="pln">outdated</span>
<span class="pln">pip3 install </span><span class="pun">--</span><span class="pln">upgrade <span class="pun">需升级的</span></span><span class="pun">库名</span>

三. 开始正式搭建tg机器人

1. 下载脚本

<code><span class="pln">git clone https</span><span class="pun">:</span><span class="com">//github.com/snow922841/telegram_channel_downloader.git</span></code>

2. 安装 rclone / gclone 或是 fclone:这三个都可以用于上传GD,rclone、gclone仅能用于上传至OD。如果只需要下载到本地,则无需安装这一步

<span class="typ">Rclone</span><span class="pun">安装及检查命令:</span>
<span class="pln">curl https</span><span class="pun">:</span><span class="com">//rclone.org/install.sh | sudo bash</span><span class="pln">
rclone </span><span class="pun">--</span><span class="pln">version

</span><span class="typ">Gclone</span><span class="pun">安装及检查命令:</span>
<span class="pln">bash </span><span class="pun"><(</span><span class="pln">wget </span><span class="pun">-</span><span class="pln">qO</span><span class="pun">-</span><span class="pln"> https</span><span class="pun">:</span><span class="com">//git.io/gclone.sh) </span>
<span class="pln">gclone </span><span class="pun">--</span><span class="pln">version</span>

<span class="typ">Fclone</span><span class="pun">安装办法:</span>
<span class="lit">1</span><span class="pun">、进入项目页:</span><span class="pln">https</span><span class="pun">:</span><span class="com">//github.com/mawaya/rclone/releases,下载对应系统版本的zip文件</span>
<span class="lit">2</span><span class="pun">、在本地解压获得</span><span class="pln">fclone</span><span class="pun">文件</span>
<span class="lit">3</span><span class="pun">、上传至/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">目录里</span>
<span class="lit">4</span><span class="pun">、</span><span class="pln">chmod </span><span class="pun">+</span><span class="pln">x </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">fclone</span><span class="pun">来赋予权限</span>


<strong><span class="pun">***</span> <span class="pun">安装完之后一定要记得配置好需要上传的网盘。</span></strong>

3. 进入脚本目录

<code><span class="pln">cd telegram_channel_downloader</span></code>

4. 安装依赖

<code><span class="pln">pip3 install </span><span class="pun">-</span><span class="pln">r requirements</span><span class="pun">.</span><span class="pln">txt</span></code>

5. 修改 telegram_channel_downloader.py 文件内的相关信息:

api_id、api_hash:第一步获取到的对应信息,分别填入对应位置;

bot_token:@BotFather 申请一个新的bot机器人(必须是未使用的),将获得一串字符。填入其中;

admin_id:可以在电报中给 @get_id_bot 发送随意消息获取;

如果需要上传到OD:应修改脚本中以下参数:drive_id=”,drive_name=’onedrive’,onedrive 就是你配置 rclone 的时候填的网盘名称,还有第102行的“fclone”替换为“rclone”;

所有配置都有相应备注,根据需要修改。

6. 运行脚本:脚本只要在运行过程中才能使用,可以使用screen保持不断线

<code><span class="pln">python3 tg_channel_downloader</span><span class="pun">.</span><span class="pln">py
</span></code> <span class="com"># 按照提示输入你的 telegram 所绑定的手机号,获取验证码并输入后即可</span>

screen的一些相关命令:

<span class="com"># 创建一个screen会话:</span><span class="pln">
screen </span><span class="pun">-</span><span class="pln">S xx      </span><span class="com">#xx为创建会话的名称 </span>

<span class="com"># 隐藏并保留当前会话窗口: </span>
<span class="pun">按</span><span class="typ">Ctrl</span><span class="pun">+</span><span class="pln">A</span><span class="pun">,再按</span><span class="str">"D"</span><span class="pun">键</span> 

<span class="com"># 恢复会话窗口: </span><span class="pln">
screen </span><span class="pun">-</span><span class="pln">r xx      </span><span class="com">#恢复名字为xx的会话 </span>

<span class="com"># 如果忘记或者没有设定会话名称: </span><span class="pln">
screen </span><span class="pun">-</span><span class="pln">ls      </span><span class="com">#列出所有会话,找到并使用恢复命令即可 </span>

<span class="com"># 关闭会话窗口 </span>
<span class="kwd">exit</span>

7. 配置完成后,给你的bot机器人发送: /start 频道的链接 0 ,就可以正式开始运行脚本,下载文件了。

频道的链接:点击你的频道名就能看到频道链接;

0:这是消息ID,可以自己定义,0代表的是最早的信息。如果要从特定某条信息往后下载,鼠标右键点击该条消息,选择复制该消息直链,就能从链接末尾看到消息ID数字。

 

好了,到此就开始愉快的下载吧!


情深几许 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用tg机器人自动下载并上传频道内文件
喜欢 (0)
关于作者:
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址