Telegram Autopilot
Source: https://github.com/Shor73/telegram-autopilot
Author: @Shor73
License: MIT
AI-powered autopilot for personal Telegram accounts. Responds to private messages as the user when they're unavailable.
Prerequisites
- - Python 3.10+
- Telethon: INLINECODE0
- Telegram API credentials (apiid + apihash from https://my.telegram.org)
- An Anthropic API key (for AI responses)
Setup
1. Get Telegram API Credentials
Direct the user to https://my.telegram.org → API Development Tools → create app.
They need: API ID (number) and API Hash (string).
2. Login Flow
Telegram login requires: phone → OTP code → optional 2FA password.
Critical: OTP codes expire in ~60 seconds. Minimize latency:
- - Use a file-based code exchange (script polls a file every 200ms)
- Or serve a simple web form on a local port for instant code entry
- Never rely on chat round-trip for code delivery — too slow
Run scripts/setup.py with the user's credentials. It handles:
- 1. Requesting the OTP code
- Waiting for code via file (
enter_code.txt) - 2FA password if needed
- Saving the session file
CODEBLOCK0
The session file (.session) persists auth — login is one-time only.
3. Configure Contacts
Edit config.json to define allowed contacts:
CODEBLOCK1
4. Start Autopilot
CODEBLOCK2
Features
Auto-Reply
- - Responds only to allowed contacts in private chats
- Marks messages as read before replying (natural behavior)
- Simulates typing delay proportional to response length
- Maintains conversation history for context
AI Persona
- - System prompt configurable per-contact (tone, language)
- Never invents facts — says "I'll check and get back to you" for unknowns
- Never reveals it's AI
Notifications
- - Forwards received messages to owner via Telegram bot
- Forwards sent replies for monitoring
Paid Media (Channels Only)
Telegram paid media (
inputMediaPaidMedia) only works in channels, not private chats.
To send paid media:
- 1. Create a private channel
- Post media with INLINECODE6
- Generate invite link and send to recipient
CODEBLOCK3
Management Commands
Stop autopilot: kill the process or send SIGTERM.
Add/remove contacts: edit config.json and restart.
Architecture
CODEBLOCK4
Required Credentials
| Secret | Purpose | Where used |
|---|
| Telegram API ID + Hash | MTProto client auth | INLINECODE8 , INLINECODE9 |
| Phone number + 2FA |
Account login (one-time) |
setup.py |
| AI API key | Response generation |
autopilot.py |
| Bot token (optional) | Owner notifications |
autopilot.py |
All secrets are stored in config.json — never commit this file.
Security & Ethics
- - Session security: The
.session file grants full access to the account. Protect it like a password. - Transparency: The AI is instructed to be honest if directly asked whether it's AI.
- OTP server:
code_server.py binds to 127.0.0.1 (localhost only). Never expose it to the network. - Notifications: The skill can forward messages to the owner via bot. Ensure you control the bot token and chat_id.
- Rate limits: Telegram may restrict accounts with aggressive automation. The autopilot uses natural delays.
- One session at a time: Only one process can use a session file. Stop autopilot before running other scripts.
- Platform policies: Using a userbot to auto-reply may violate Telegram ToS. Use at your own risk.
Telegram 自动导航系统
来源: https://github.com/Shor73/telegram-autopilot
作者: @Shor73
许可协议: MIT
基于人工智能的个人Telegram账户自动导航系统。当用户不在时,可自动回复私人消息。
前置条件
- - Python 3.10+
- Telethon:pip3 install telethon
- Telegram API凭证(来自 https://my.telegram.org 的 apiid + apihash)
- Anthropic API密钥(用于AI回复)
设置
1. 获取Telegram API凭证
引导用户访问 https://my.telegram.org → API开发工具 → 创建应用。
他们需要:API ID(数字)和 API Hash(字符串)。
2. 登录流程
Telegram登录需要:手机号 → OTP验证码 → 可选的双因素认证密码。
关键点: OTP验证码约60秒后过期。请最小化延迟:
- - 使用基于文件的验证码交换(脚本每200毫秒轮询文件)
- 或在本地端口提供简单的网页表单用于即时输入验证码
- 切勿依赖聊天往返传递验证码——速度太慢
使用用户的凭证运行 scripts/setup.py。该脚本处理:
- 1. 请求OTP验证码
- 通过文件(enter_code.txt)等待验证码
- 如果需要,处理双因素认证密码
- 保存会话文件
bash
python3 scripts/setup.py --api-id 12345 --api-hash abc123 --phone +1234567890
会话文件(.session)持久保存认证信息——只需登录一次。
3. 配置联系人
编辑 config.json 以定义允许的联系人:
json
{
contacts: {
username: {
name: 显示名称,
id: 123456789,
tone: 友好,
language: zh
}
},
ai: {
provider: anthropic,
model: claude-sonnet-4-6,
api_key: sk-ant-...,
max_tokens: 300
},
owner: {
name: 所有者名称,
bio: AI角色简要描述,
telegram_id: 123456789
},
notifications: {
bot_token: 可选的通知机器人令牌,
chat_id: 可选的聊天ID
}
}
4. 启动自动导航系统
bash
python3 scripts/autopilot.py --config config.json --session session_name
功能特性
自动回复
- - 仅回复私人聊天中允许的联系人
- 回复前将消息标记为已读(自然行为)
- 模拟与回复长度成正比的输入延迟
- 维护对话历史以保持上下文
AI角色
- - 每个联系人可配置系统提示(语气、语言)
- 从不编造事实——对未知信息回复我会核实后回复您
- 从不透露自己是AI
通知功能
- - 通过Telegram机器人将接收到的消息转发给所有者
- 转发已发送的回复以供监控
付费媒体(仅限频道)
Telegram付费媒体(inputMediaPaidMedia)仅适用于频道,不适用于私人聊天。
发送付费媒体:
- 1. 创建一个私密频道
- 使用 InputMediaPaidMedia(starsamount=N, extendedmedia=[...]) 发布媒体
- 生成邀请链接并发送给接收者
bash
python3 scripts/sendpaidmedia.py --session session_name --target username --photo /path/to/photo.jpg --stars 1
管理命令
停止自动导航系统:终止进程或发送SIGTERM信号。
添加/删除联系人:编辑 config.json 并重启。
架构
telegram-autopilot/
├── SKILL.md
├── scripts/
│ ├── setup.py — 登录流程(OTP + 双因素认证)
│ ├── autopilot.py — 主事件循环
│ ├── sendpaidmedia.py — 通过频道发送付费媒体
│ └── code_server.py — 快速输入OTP的网页表单
└── references/
└── telegram-auth.md — Telegram认证流程文档
所需凭证
| 密钥 | 用途 | 使用位置 |
|---|
| Telegram API ID + Hash | MTProto客户端认证 | setup.py, autopilot.py |
| 手机号 + 双因素认证 |
账户登录(一次性) | setup.py |
| AI API密钥 | 回复生成 | autopilot.py |
| 机器人令牌(可选) | 所有者通知 | autopilot.py |
所有密钥存储在 config.json 中——切勿提交此文件。
安全与伦理
- - 会话安全: .session 文件提供对账户的完全访问权限。请像保护密码一样保护它。
- 透明度: 如果被直接询问是否为AI,AI被指示如实回答。
- OTP服务器: code_server.py 绑定到 127.0.0.1(仅限本地主机)。切勿将其暴露到网络中。
- 通知: 该技能可通过机器人将消息转发给所有者。请确保您控制机器人令牌和聊天ID。
- 速率限制: Telegram可能会限制具有激进自动化行为的账户。自动导航系统使用自然的延迟。
- 一次一个会话: 只有一个进程可以使用会话文件。在运行其他脚本前停止自动导航系统。
- 平台政策: 使用用户机器人自动回复可能违反Telegram服务条款。使用风险自负。