Telegram Bot API senddice 使用教程
## 1. senddice 方法用途介绍


senddice 是 Telegram Bot API 提供的一个方法,用于向指定聊天发送交互式骰子/游戏动画,增加聊天的趣味性和互动性。
**主要用途:**
- 发送各种骰子动画(骰子、飞镖、保龄球等)
- 增加聊天的互动性和娱乐性
- 创建简单的游戏和抽奖活动
- 作为对用户操作的反馈机制
**核心特点:**
- 支持多种骰子表情类型(dice、dart、basketball、football、slot_machine、bowling)
- 自动生成随机结果
- 所有用户看到相同的随机结果
- 支持所有常规消息发送选项(如禁用通知、回复消息等)
- 可将动画发送到 Direct Messages 聊天主题
- 支持设置建议的帖子参数
## 2. 原生 PHP 调用实例
### 2.1 准备工作
确保你的 PHP 环境满足以下要求:
- PHP 5.6 或更高版本
- 已安装 cURL 扩展
- 已创建 Telegram Bot 并获取 Bot Token
### 2.2 发送基本骰子动画
<?php
/**
* 使用 Telegram Bot API 发送基本骰子动画
* @param string $botToken Bot Token
* @param int|string $chatId 聊天 ID
* @return array 响应结果
*/
function sendBasicDice($botToken, $chatId) {
$url = "https://api.telegram.org/bot{$botToken}/senddice";
$data = [
'chat_id' => $chatId
];
$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";
$chatId = 123456789; // 用户或群组的聊天 ID
$result = sendBasicDice($botToken, $chatId);
if ($result['ok']) {
echo "骰子动画发送成功!消息 ID: " . $result['result']['message_id'];
echo "\n骰子点数: " . $result['result']['dice']['value'];
echo "\n骰子类型: " . $result['result']['dice']['emoji'];
} else {
echo "发送失败:" . $result['description'];
}
?>
```
### 2.3 发送特定类型的骰子动画
```php
<?php
/**
* 使用 Telegram Bot API 发送特定类型的骰子动画
* @param string $botToken Bot Token
* @param int|string $chatId 聊天 ID
* @param string $emoji 骰子类型表情(dice、dart、basketball、football、slot_machine、bowling)
* @return array 响应结果
*/
function sendSpecificDice($botToken, $chatId, $emoji) {
$url = "https://api.telegram.org/bot{$botToken}/senddice";
// 验证表情类型
$allowedEmojis = ['🎲', '🎯', '🏀', '⚽', '🎰', '🎳', 'dice', 'dart', 'basketball', 'football', 'slot_machine', 'bowling'];
if (!in_array($emoji, $allowedEmojis)) {
return [
'ok' => false,
'error_code' => 400,
'description' => '不支持的骰子类型'
];
}
$data = [
'chat_id' => $chatId,
'emoji' => $emoji
];
$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";
$chatId = 123456789;
// 发送飞镖
echo "发送飞镖...\n";
$result1 = sendSpecificDice($botToken, $chatId, '🎯');
if ($result1['ok']) {
echo "飞镖发送成功!环数: " . $result1['result']['dice']['value'] . "\n";
} else {
echo "发送失败:" . $result1['description'] . "\n";
}
// 发送篮球
echo "\n发送篮球...\n";
$result2 = sendSpecificDice($botToken, $chatId, 'basketball');
if ($result2['ok']) {
echo "篮球发送成功!投篮结果: " . $result2['result']['dice']['value'] . "\n";
} else {
echo "发送失败:" . $result2['description'] . "\n";
}
// 发送老虎机
echo "\n发送老虎机...\n";
$result3 = sendSpecificDice($botToken, $chatId, 'slot_machine');
if ($result3['ok']) {
echo "老虎机发送成功!结果: " . $result3['result']['dice']['value'] . "\n";
} else {
echo "发送失败:" . $result3['description'] . "\n";
}
?>
```
### 2.4 发送带高级选项的骰子动画
```php
<?php
/**
* 使用 Telegram Bot API 发送带高级选项的骰子动画
* @param string $botToken Bot Token
* @param int|string $chatId 聊天 ID
* @param string $emoji 骰子类型表情
* @param array $options 高级选项
* @return array 响应结果
*/
function sendAdvancedDice($botToken, $chatId, $emoji = null, $options = []) {
$url = "https://api.telegram.org/bot{$botToken}/senddice";
$data = ['chat_id' => $chatId];
// 添加骰子类型(如果提供)
if ($emoji !== null) {
$data['emoji'] = $emoji;
}
// 合并高级选项
$allowedOptions = [
'business_connection_id', 'message_thread_id', '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";
$chatId = 123456789;
// 作为对特定消息的回复发送保龄球动画
$replyMessageId = 12345; // 要回复的消息 ID
$result = sendAdvancedDice(
$botToken,
$chatId,
'bowling', // 保龄球
[
'disable_notification' => false, // 不禁用通知
'protect_content' => true, // 保护内容不被转发
'reply_to_message_id' => $replyMessageId, // 回复特定消息
'allow_sending_without_reply' => true // 即使原消息被删除也能发送
]
);
if ($result['ok']) {
echo "保龄球动画发送成功!消息 ID: " . $result['result']['message_id'];
echo "\n得分: " . $result['result']['dice']['value'];
} else {
echo "发送失败:" . $result['description'];
}
?>
```
### 2.5 完整的 Telegram Bot 骰子发送类
```php
<?php
class TelegramDiceBot {
private $botToken;
/**
* 构造函数
* @param string $botToken Bot Token
*/
public function __construct($botToken) {
$this->botToken = $botToken;
}
/**
* 发送基本骰子动画
* @param int|string $chatId 聊天 ID
* @return array 响应结果
*/
public function sendDice($chatId) {
return $this->sendAdvancedDice($chatId, null, []);
}
/**
* 发送飞镖动画
* @param int|string $chatId 聊天 ID
* @param array $options 高级选项
* @return array 响应结果
*/
public function sendDart($chatId, $options = []) {
return $this->sendAdvancedDice($chatId, '🎯', $options);
}
/**
* 发送老虎机动画
* @param int|string $chatId 聊天 ID
* @param array $options 高级选项
* @return array 响应结果
*/
public function sendSlotMachine($chatId, $options = []) {
return $this->sendAdvancedDice($chatId, '🎰', $options);
}
/**
* 发送保龄球动画
* @param int|string $chatId 聊天 ID
* @param array $options 高级选项
* @return array 响应结果
*/
public function sendBowling($chatId, $options = []) {
return $this->sendAdvancedDice($chatId, '🎳', $options);
}
/**
* 发送篮球动画
* @param int|string $chatId 聊天 ID
* @param array $options 高级选项
* @return array 响应结果
*/
public function sendBasketball($chatId, $options = []) {
return $this->sendAdvancedDice($chatId, '🏀', $options);
}
/**
* 发送足球动画
* @param int|string $chatId 聊天 ID
* @param array $options 高级选项
* @return array 响应结果
*/
public function sendFootball($chatId, $options = []) {
return $this->sendAdvancedDice($chatId, '⚽', $options);
}
/**
* 发送带高级选项的骰子动画
* @param int|string $chatId 聊天 ID
* @param string $emoji 骰子类型表情
* @param array $options 高级选项
* @return array 响应结果
*/
public function sendAdvancedDice($chatId, $emoji = null, $options = []) {
$url = "https://api.telegram.org/bot{$this->botToken}/senddice";
$data = ['chat_id' => $chatId];
// 添加骰子类型(如果提供)
if ($emoji !== null) {
// 验证表情类型
$allowedEmojis = ['🎲', '🎯', '🏀', '⚽', '🎰', '🎳', 'dice', 'dart', 'basketball', 'football', 'slot_machine', 'bowling'];
if (!in_array($emoji, $allowedEmojis)) {
return [
'ok' => false,
'error_code' => 400,
'description' => '不支持的骰子类型'
];
}
$data['emoji'] = $emoji;
}
// 合并高级选项
$allowedOptions = [
'business_connection_id', 'message_thread_id', '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];
}
}
return $this->sendRequest($url, $data);
}
/**
* 创建简单的抽奖活动
* @param int|string $chatId 聊天 ID
* @param string $prize 奖品名称
* @return array 响应结果
*/
public function createRaffle($chatId, $prize) {
// 发送抽奖通知
$messageText = "🎉 抽奖活动开始!🎁 奖品:{$prize}\n\n点击下方的骰子开始抽奖!";
$messageResult = $this->sendMessage($chatId, $messageText);
if (!$messageResult['ok']) {
return $messageResult;
}
// 发送骰子作为抽奖结果
return $this->sendDice($chatId, ['reply_to_message_id' => $messageResult['result']['message_id']]);
}
/**
* 发送文本消息
* @param int|string $chatId 聊天 ID
* @param string $text 消息文本
* @return array 响应结果
*/
public function sendMessage($chatId, $text) {
$url = "https://api.telegram.org/bot{$this->botToken}/sendMessage";
$data = [
'chat_id' => $chatId,
'text' => $text
];
return $this->sendRequest($url, $data);
}
/**
* 发送 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);
}
}
// 使用示例
$botToken = "YOUR_BOT_TOKEN";
$bot = new TelegramDiceBot($botToken);
$chatId = 123456789;
// 发送基本骰子
echo "发送基本骰子...\n";
$result1 = $bot->sendDice($chatId);
if ($result1['ok']) {
echo "骰子发送成功!点数: " . $result1['result']['dice']['value'] . "\n";
} else {
echo "发送失败:" . $result1['description'] . "\n";
}
// 发送飞镖
echo "\n发送飞镖...\n";
$result2 = $bot->sendDart($chatId);
if ($result2['ok']) {
echo "飞镖发送成功!环数: " . $result2['result']['dice']['value'] . "\n";
} else {
echo "发送失败:" . $result2['description'] . "\n";
}
// 发送老虎机
echo "\n发送老虎机...\n";
$result3 = $bot->sendSlotMachine($chatId);
if ($result3['ok']) {
echo "老虎机发送成功!结果: " . $result3['result']['dice']['value'] . "\n";
} else {
echo "发送失败:" . $result3['description'] . "\n";
}
// 创建简单抽奖活动
echo "\n创建抽奖活动...\n";
$result4 = $bot->createRaffle($chatId, "iPhone 15");
if ($result4['ok']) {
echo "抽奖活动创建成功!抽奖结果: " . $result4['result']['dice']['value'] . "\n";
} else {
echo "创建失败:" . $result4['description'] . "\n";
}
?>
2.6 注意事项
1. **骰子类型:**
- 支持的骰子类型:`🎲` (dice)、`🎯` (dart)、`🏀` (basketball)、`⚽` (football)、`🎰` (slot_machine)、`🎳` (bowling)
- 可以使用表情符号或英文名称
2. **随机结果:**
- 结果是随机生成的,但所有用户看到相同的结果
- 每种骰子类型有不同的可能结果范围:
- 骰子:1-6
- 飞镖:1-6(环数)
- 篮球:1-5(1=未命中,5=完美投篮)
- 足球:1-5(1=未命中,5=完美射门)
- 老虎机:1-64(不同结果代表不同图案组合)
- 保龄球:1-6(1=未击中,6=全中)
3. **高级选项:**
- 支持所有常规消息发送选项
- 可以与 direct_messages_topic_id 一起使用,发送到 Direct Messages 聊天主题
- 可以设置 suggested_post_parameters 参数,发送建议的帖子
4. **聊天 ID 格式:**
- 私人聊天 ID 是整数
- 群组和频道 ID 通常以 `-100` 开头的负数
## 3. 应用场景
- **聊天互动:** 增加群聊的趣味性和活跃度
- **简单游戏:** 创建猜数字、比大小等简单游戏
- **抽奖活动:** 作为抽奖结果的可视化展示
- **反馈机制:** 作为对用户操作的随机反馈
- **破冰活动:** 在新群组中促进成员互动
## 4. 运行指南
1. 将以上代码保存为 `.php` 文件
2. 替换 `YOUR_BOT_TOKEN` 为你的实际 Bot Token
3. 替换聊天 ID 为实际值
4. 在命令行或浏览器中运行 PHP 文件
```bash
php telegram-send-dice.php

发表评论 取消回复