Telegram Image Bot

Lite Telegram Image Bot 是一个基于 C++ 的 Telegram 机器人项目,可以处理用户发送给机器人的图片,并返回一个可公开访问的 URL。

功能特性

  1. 接收和处理用户发送的图片:用户可以直接将图片发送给机器人,机器人会返回一个可分享的 URL。
  1. 处理群聊中的图片:当机器人被 @ 并且是对图片的回复时,机器人会返回该图片的 URL。
  1. 动态线程池:自动调整线程池大小以优化性能。
  1. 持久化状态:自动保存并恢复 offset,以避免处理重复的消息。
  1. 错误处理:友好的错误提示和详细的日志记录,便于调试和维护。

项目结构

shell

/telegram_bot ├── include │ ├── bot.h # Telegram Bot 功能 │ ├── http_client.h # HTTP 请求处理 │ ├── thread_pool.h # 线程池管理 │ ├── utils.h # 辅助功能 ├── src │ ├── bot.cpp # Telegram Bot 实现 │ ├── http_client.cpp # HTTP 请求实现 │ ├── main.cpp # 主程序入口 │ ├── thread_pool.cpp # 线程池实现 │ ├── utils.cpp # 辅助功能实现 └── Makefile # 编译和清理脚本
Shell

环境要求

  1. C++11/14/17:支持 C++11 或以上版本的编译器。
  1. libcurl:用于处理 HTTP 请求。
  1. nlohmann/json:用于解析 JSON 数据。
  1. POSIX:线程池依赖于 POSIX 线程。

安装与使用

1. 克隆项目

shell

git clone https://github.com/cryptoli/lite-telegram-image-bot.git cd lite-telegram-image-bot
Shell

2. 安装依赖库

在 Ubuntu/Debian 上,你可以使用以下命令安装所需的库:

shell

sudo apt-get install g++ libcurl4-openssl-dev make nlohmann-json3-dev libssl-dev
Shell

3. 编译项目

在项目根目录下运行:

shell

make
Shell

4. 修改配置文件config.json

需要修改hostname,api_token

shell

{ "server": { "hostname": "yourdomain.com", "port": 443 }, "api_token": "your_telegram_api_token_here", "mime_types": { ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".png": "image/png", ".gif": "image/gif", ".bmp": "image/bmp", ".tiff": "image/tiff", ".webp": "image/webp", ".mp4": "video/mp4", ".mp3": "audio/mpeg", ".ogg": "audio/ogg", ".wav": "audio/wav", ".m4a": "audio/mp4", ".aac": "audio/aac", ".pdf": "application/pdf", ".doc": "application/msword", ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ".xls": "application/vnd.ms-excel", ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ".ppt": "application/vnd.ms-powerpoint", ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation", ".zip": "application/zip", ".rar": "application/x-rar-compressed", ".7z": "application/x-7z-compressed", ".txt": "text/plain", ".csv": "text/csv", ".json": "application/json", ".xml": "application/xml", ".html": "text/html", ".htm": "text/html", ".css": "text/css", ".js": "application/javascript", ".webm": "video/webm", ".mkv": "video/x-matroska", ".mov": "video/quicktime", ".avi": "video/x-msvideo", ".flv": "video/x-flv", ".apk": "application/vnd.android.package-archive", ".tar": "application/x-tar", ".gz": "application/gzip", ".bz2": "application/x-bzip2", ".stl": "application/vnd.ms-pkistl" }, "cache": { "max_size_mb": 100, "max_age_seconds": 3600 } }
Shell

5. 生成证书

证书放在项目根目录下,名称分别为server.key、server.crt

shell

sudo apt-get update sudo apt-get install certbot sudo certbot certonly --standalone -d yourdomain.com sudo cp /etc/letsencrypt/live/yourdomain.com/privkey.pem /path/to/your/project/server.key sudo cp /etc/letsencrypt/live/yourdomain.com/fullchain.pem /path/to/your/project/server.crt
Shell

6. 运行机器人

运行程序并传入 Telegram Bot API Token:

shell

./telegram_bot
Shell
你可以通过 @BotFather 在 Telegram 中创建并获取你的 Bot Token。

配置说明

在 bot.cpp 文件中,你可以根据需要调整机器人的配置,例如:
  1. 处理的文件类型:当前只处理图片(photo),你可以扩展到处理其他文件类型。
  1. API 请求频率:默认情况下,每秒请求一次更新,可以根据需要调整请求频率。
项目地址:
lite-telegram-image-bot
cryptoliUpdated Oct 8, 2024
成品bot:

效果展示

notion image
notion image
notion image
优秀演员经典画面:张世篇红色嫁衣,绿色嫁衣在北宋时期哪个更正式?我来告诉你!
Loading...
hexo