使用 Telethon 实现 Telegram 签到功能
玩 Telegram(TG)的人不少,某些频道群组会有签到积分等活动,天天打卡真的蛮累的。这个Python 脚本可以实现自动签到。
问题
Telethon 在青龙面板上的痛点在于青龙面板无交互,无法输入获取账号信息,但我们可以曲线救国。
方法
方法1. SSH 进入青龙容器
- 在Shell工具使用命令:`docker exec -it 容器名称 bash` 进入青龙容器(自行替换容器名称)。
- 然后
- 执行完以上操作,生成了session文件,此后青龙面板运行脚本不再提示输入账号与验证码。
方法2. 预先生成会话文件
- 在有交互的 Python 环境中(例如:VsCode)先运行一次脚本,按提示输入账号与验证码,脚本同目录下将会生成青龙容器
- 在Shell工具文件。
- 将thon 实现 Telegra文件移动到青龙面板脚本同目录下,青龙面板运行脚本不再提示输入账号与验证码。
示例代码
注意
- 输入账号的格式为:`+86xxxxxxxxxxxx`。
- Telegram 的 API ID 和 API Hash 获取方式可以通过搜索引擎找到。
- 如何在青龙面板安装依赖 Telethon 的方法也可以通过搜索了解。
转自:yaohuo.me 昵称:AquaLyn
至此,解放双手。
#python脚本
玩 Telegram(TG)的人不少,某些频道群组会有签到积分等活动,天天打卡真的蛮累的。这个Python 脚本可以实现自动签到。
问题
Telethon 在青龙面板上的痛点在于青龙面板无交互,无法输入获取账号信息,但我们可以曲线救国。
方法
方法1. SSH 进入青龙容器
- 在Shell工具使用命令:`docker exec -it 容器名称 bash` 进入青龙容器(自行替换容器名称)。
- 然后
cd
到脚本所在目录,运行 `python XXXX.py`,此时可以交互输入账号信息与验证码。- 执行完以上操作,生成了session文件,此后青龙面板运行脚本不再提示输入账号与验证码。
方法2. 预先生成会话文件
- 在有交互的 Python 环境中(例如:VsCode)先运行一次脚本,按提示输入账号与验证码,脚本同目录下将会生成青龙容器
- 在Shell工具文件。
- 将thon 实现 Telegra文件移动到青龙面板脚本同目录下,青龙面板运行脚本不再提示输入账号与验证码。
示例代码
from telethon import TelegramClient
# 替换为你的 API ID 和 API Hash
api_id = 'xxxxxxxx'
api_hash = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# 定义 chat_id 和消息文本
chat_messages = [
(-10011234567890, "/签到"), # 修改为你需要的聊天 ID 和消息
# (-1001234567890, "Hello World!"), # 可以继续添加更多的 chat_id 和消息
]
# 创建一个 TelegramClient 实例
client = TelegramClient('session', api_id, api_hash)
async def get_chat_ids():
await client.start()
chat_info = {}
async for dialog in client.iter_dialogs():
chat_info[dialog.id] = dialog.name
print(f'Chat ID: {dialog.id}, Name: {dialog.name}')
print("-" * 50+"\n")
return chat_info
async def main():
# 登录
await client.start()
# 获取聊天 ID 和名称
chat_info = await get_chat_ids()
# 遍历每个 chat_messages 并发送对应的文本到相应的 chat_id
for chat_id, message_text in chat_messages:
chat_name = chat_info.get(chat_id, "未知聊天") # 获取聊天名称,如果不存在则为"未知聊天"
await send_text(chat_name, chat_id, message_text)
async def send_text(chat_name, chat_id, text):
# 向指定 chat_id 发送文本消息
await client.send_message(chat_id, text)
print(f"Sent '{text}' to chat: {chat_name}")
# 运行主程序
with client:
client.loop.run_until_complete(main())
注意
- 输入账号的格式为:`+86xxxxxxxxxxxx`。
- Telegram 的 API ID 和 API Hash 获取方式可以通过搜索引擎找到。
- 如何在青龙面板安装依赖 Telethon 的方法也可以通过搜索了解。
转自:yaohuo.me 昵称:AquaLyn
至此,解放双手。
#python脚本
Please open Telegram to view this post
VIEW IN TELEGRAM