Telegram Bot API copyMessage 使用教程
## 1. copyMessage 方法用途介绍
copyMessage 是 Telegram Bot API 提供的一个方法,用于将一条消息从一个聊天(chat)复制到另一个聊天。与 forwardMessage 不同,copyMessage 会创建一条新消息,而不是简单转发原始消息。
**主要用途:**
- 在不同聊天之间复制消息内容,以机器人的名义重新发送
- 保留原始消息的内容和格式,但不显示原始发送者信息
- 实现内容迁移、备份和重新分发功能
- 作为机器人自动化工作流中的信息处理环节
**核心特点:**
- 复制的消息显示为机器人发送,不会显示原始发送者信息
- 支持复制各种类型的消息:文本、照片、视频、音频、文档、位置、联系方式等
- 可选择修改复制后的消息标题(仅适用于文档、照片、视频等)
- 支持复制到 Direct Messages 聊天主题
- 与 Bot API 的其他方法无缝集成
## 2. 原生 PHP 调用实例
### 2.1 准备工作
确保你的 PHP 环境满足以下要求:
- PHP 5.6 或更高版本
- 已安装 cURL 扩展
- 已创建 Telegram Bot 并获取 Bot Token
### 2.2 简单的 copyMessage 调用
<?php
/**
* 使用 Telegram Bot API 复制消息
* @param string $botToken Bot Token
* @param int|string $chatId 目标聊天 ID
* @param int|string $fromChatId 源聊天 ID
* @param int $messageId 要复制的消息 ID
* @return array 响应结果
*/
function copyMessage($botToken, $chatId, $fromChatId, $messageId) {
$url = "https://api.telegram.org/bot{$botToken}/copyMessage";
$data = [
'chat_id' => $chatId,
'from_chat_id' => $fromChatId,
'message_id' => $messageId
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// 使用示例
$botToken = "YOUR_BOT_TOKEN";
$targetChatId = -1001234567890; // 目标聊天 ID(群组/频道)
$sourceChatId = 123456789; // 源聊天 ID(用户/群组)
$messageId = 123; // 要复制的消息 ID
$result = copyMessage($botToken, $targetChatId, $sourceChatId, $messageId);
if ($result['ok']) {
echo "消息复制成功!";
echo "复制后消息 ID: " . $result['result']['message_id'];
} else {
echo "复制失败:" . $result['description'];
}
?>
```
### 2.3 带可选参数的 copyMessage 调用
```php
<?php
/**
* 使用 Telegram Bot API 复制消息(带可选参数)
* @param string $botToken Bot Token
* @param int|string $chatId 目标聊天 ID
* @param int|string $fromChatId 源聊天 ID
* @param int $messageId 要复制的消息 ID
* @param array $options 可选参数数组
* @return array 响应结果
*/
function copyMessageWithOptions($botToken, $chatId, $fromChatId, $messageId, $options = []) {
$url = "https://api.telegram.org/bot{$botToken}/copyMessage";
$data = [
'chat_id' => $chatId,
'from_chat_id' => $fromChatId,
'message_id' => $messageId
];
// 合并可选参数
$allowedOptions = [
'message_thread_id', 'caption', 'parse_mode', 'caption_entities',
'disable_notification', 'protect_content', 'reply_to_message_id',
'allow_sending_without_reply', 'reply_markup', 'direct_messages_topic_id',
'suggested_post_parameters'
];
foreach ($allowedOptions as $option) {
if (isset($options[$option])) {
$data[$option] = $options[$option];
}
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
// 使用示例 - 复制文档并修改标题
$botToken = "YOUR_BOT_TOKEN";
$targetChatId = -1001234567890;
$sourceChatId = 123456789;
$messageId = 123;
$result = copyMessageWithOptions(
$botToken,
$targetChatId,
$sourceChatId,
$messageId,
[
'caption' => '这是修改后的文档标题',
'parse_mode' => 'MarkdownV2',
'protect_content' => true,
'direct_messages_topic_id' => 456
]
);
if ($result['ok']) {
echo "带修改标题的文档复制成功!";
} else {
echo "复制失败:" . $result['description'];
}
?>
```
### 2.4 完整的 Telegram Bot 消息复制类
```php
<?php
class TelegramCopyBot {
private $botToken;
/**
* 构造函数
* @param string $botToken Bot Token
*/
public function __construct($botToken) {
$this->botToken = $botToken;
}
/**
* 复制单条消息
* @param int|string $chatId 目标聊天 ID
* @param int|string $fromChatId 源聊天 ID
* @param int $messageId 要复制的消息 ID
* @param array $options 可选参数
* @return array 响应结果
*/
public function copyMessage($chatId, $fromChatId, $messageId, $options = []) {
$url = "https://api.telegram.org/bot{$this->botToken}/copyMessage";
$data = [
'chat_id' => $chatId,
'from_chat_id' => $fromChatId,
'message_id' => $messageId
];
// 合并可选参数
$this->mergeOptions($data, $options);
return $this->sendRequest($url, $data);
}
/**
* 批量复制消息
* @param int|string $chatId 目标聊天 ID
* @param int|string $fromChatId 源聊天 ID
* @param array $messageIds 要复制的消息 ID 数组
* @param array $options 可选参数(适用于所有复制)
* @return array 所有复制结果
*/
public function copyMessages($chatId, $fromChatId, $messageIds, $options = []) {
$url = "https://api.telegram.org/bot{$this->botToken}/copyMessages";
$data = [
'chat_id' => $chatId,
'from_chat_id' => $fromChatId,
'message_ids' => json_encode($messageIds)
];
// 合并可选参数
$this->mergeOptions($data, $options);
return $this->sendRequest($url, $data);
}
/**
* 合并可选参数
* @param array $data 基础数据
* @param array $options 可选参数
*/
private function mergeOptions(&$data, $options) {
$allowedOptions = [
'message_thread_id', 'caption', 'parse_mode', 'caption_entities',
'disable_notification', 'protect_content', 'reply_to_message_id',
'allow_sending_without_reply', 'reply_markup', 'direct_messages_topic_id',
'suggested_post_parameters'
];
foreach ($allowedOptions as $option) {
if (isset($options[$option])) {
$data[$option] = $options[$option];
}
}
}
/**
* 发送 HTTP 请求
* @param string $url 请求 URL
* @param array $data 请求数据
* @return array 响应结果
*/
private function sendRequest($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
$error = curl_error($ch);
curl_close($ch);
return [
'ok' => false,
'error_code' => $httpCode,
'description' => 'cURL 错误: ' . $error
];
}
curl_close($ch);
return json_decode($response, true);
}
/**
* 获取更新(用于获取消息 ID)
* @param int|null $offset 更新偏移量
* @param int|null $limit 更新数量限制
* @return array 响应结果
*/
public function getUpdates($offset = null, $limit = null) {
$url = "https://api.telegram.org/bot{$this->botToken}/getUpdates";
$data = [];
if ($offset !== null) $data['offset'] = $offset;
if ($limit !== null) $data['limit'] = $limit;
return $this->sendRequest($url, $data);
}
}
// 使用示例
$botToken = "YOUR_BOT_TOKEN";
$bot = new TelegramCopyBot($botToken);
// 基本复制
$result1 = $bot->copyMessage(
-1001234567890, // 目标聊天 ID
123456789, // 源聊天 ID
123 // 消息 ID
);
if ($result1['ok']) {
echo "基本复制成功!消息 ID: " . $result1['result']['message_id'] . "\n";
} else {
echo "基本复制失败:" . $result1['description'] . "\n";
}
// 带高级选项的复制
$result2 = $bot->copyMessage(
-1001234567890,
123456789,
456,
[
'caption' => '*修改后的标题*',
'parse_mode' => 'MarkdownV2',
'protect_content' => true,
'direct_messages_topic_id' => 789
]
);
if ($result2['ok']) {
echo "高级复制成功!\n";
} else {
echo "高级复制失败:" . $result2['description'] . "\n";
}
// 批量复制
$messageIds = [123, 456, 789];
$result3 = $bot->copyMessages(-1001234567890, 123456789, $messageIds);
if ($result3['ok']) {
echo "批量复制成功!复制了 " . count($result3['result']['message_ids']) . " 条消息\n";
foreach ($result3['result']['message_ids'] as $originalId => $newId) {
echo "消息 {$originalId} 复制后的新 ID: {$newId}\n";
}
} else {
echo "批量复制失败:" . $result3['description'] . "\n";
}
?>
```
2.5 注意事项
1. **与 forwardMessage 的区别**:
- copyMessage 以机器人名义发送新消息,不显示原始发送者信息
- forwardMessage 保留原始发送者信息,显示为转发消息
2. **聊天 ID 格式**:
- 私人聊天 ID 是整数
- 群组和频道 ID 通常以 `-100` 开头的负数
3. **权限要求**:
- 机器人必须在源聊天和目标聊天中都具有访问权限
- 对于私有频道,机器人需要是管理员
4. **复制限制**:
- 遵守 Telegram 的速率限制,避免频繁复制
- 某些聊天可能限制了消息发送功能
5. **特殊参数**:
- `caption` 参数可用于修改复制的媒体消息标题
- `direct_messages_topic_id` 用于将消息复制到 Direct Messages 聊天主题
- `suggested_post_parameters` 用于将复制的消息作为建议帖子发送
## 3. 应用场景
- **内容重新分发**:将内容从一个聊天重新分发到多个其他聊天
- **匿名发布**:在不暴露原始发送者的情况下分享内容
- **内容备份**:将重要消息复制到备份聊天
- **格式转换**:复制消息时修改格式或标题
- **自动化工作流**:在机器人工作流中处理和转发信息
## 4. 运行指南
1. 将以上代码保存为 `.php` 文件
2. 替换 `YOUR_BOT_TOKEN` 为你的实际 Bot Token
3. 替换聊天 ID 和消息 ID 为实际值
4. 在命令行或浏览器中运行 PHP 文件
```bash
php telegram-copy-message.php
```

发表评论 取消回复