以下是一个实现DeepSeek自动回帖功能的Discuz插件基础代码框架,包含API密钥对接接口:
<?php
/*
Plugin Name: DeepSeek自动回帖
Plugin URL: https://yourdomain.com/
Description: 使用DeepSeek AI自动生成帖子回复
Version: 1.0
Author: YourName
Author URL: https://yourdomain.com/
*/
// 后台设置页面
if(!defined('IN_DISCUZ')) exit('Access Denied');
class plugin_deepseek_reply {
// 插件配置
public function common() {
global $_G;
$_G['setting']['deepseek_api'] = unserialize($_G['setting']['deepseek_setting']);
}
}
class plugin_deepseek_reply_admin extends plugin_deepseek_reply {
public function index() {
global $_G;
require_once libfile('function/plugin');
require_once libfile('function/admincp');
if(submitcheck('submit')) {
$settings = array(
'api_key' => trim($_GET['api_key']),
'model' => trim($_GET['model']),
'max_tokens' => intval($_GET['max_tokens']),
'temperature' => floatval($_GET['temperature']),
'enable' => intval($_GET['enable'])
);
C::t('common_setting')->update('deepseek_setting', $settings);
updatecache('setting');
cpmsg('设置已保存', 'action=plugins&operation=config&identifier=deepseek_reply', 'succeed');
}
showformheader('plugins&operation=config&identifier=deepseek_reply');
showtableheader('DeepSeek API设置');
// API密钥输入
showsetting('API密钥', 'api_key', $this->settings['api_key'], 'text');
showsetting('启用自动回帖', 'enable', $this->settings['enable'], 'radio');
showsetting('模型版本', 'model', $this->settings['model'], 'select',
array('deepseek-chat' => 'DeepSeek Chat', 'deepseek-coder' => 'DeepSeek Coder'));
showsetting('最大长度', 'max_tokens', $this->settings['max_tokens'], 'text');
showsetting('随机性', 'temperature', $this->settings['temperature'], 'text');
showtablefooter();
showsubmit();
showformfooter();
}
}
// 主功能实现
class deepseek_reply {
public static function auto_reply($param) {
global $_G;
$settings = $_G['setting']['deepseek_setting'];
if(!$settings['enable'] || empty($settings['api_key'])) return;
$post = C::t('forum_post')->fetch_post_by_tid_invisible($param['tid']);
$prompt = "请为以下论坛帖子生成一个友好回复:\n".$post['message'];
$response = self::call_deepseek_api(array(
'model' => $settings['model'],
'messages' => array(
array('role' => 'user', 'content' => $prompt)
),
'temperature' => $settings['temperature'],
'max_tokens' => $settings['max_tokens']
), $settings['api_key']);
if($response && $response['choices'][0]['message']['content']) {
$reply = array(
'tid' => $param['tid'],
'message' => $response['choices'][0]['message']['content'],
'authorid' => $_G['uid'],
'author' => $_G['username'],
'dateline' => TIMESTAMP
);
C::t('forum_post')->insert('tid:'.$param['tid'], $reply);
}
}
private static function call_deepseek_api($data, $api_key) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.deepseek.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer '.$api_key
));
$response = curl_exec($ch);
if(curl_errno($ch)) {
log_message('DeepSeek API Error: '.curl_error($ch));
return false;
}
curl_close($ch);
return json_decode($response, true);
}
}
// 安装脚本
if($_GET['action'] == 'install') {
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `pre_deepseek_log` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`tid` int(10) NOT NULL,
`response` text NOT NULL,
`dateline` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
SQL;
runquery($sql);
$finish = TRUE;
}
// 卸载脚本
if($_GET['action'] == 'uninstall') {
$sql = "DROP TABLE IF EXISTS `pre_deepseek_log`";
runquery($sql);
C::t('common_setting')->delete('deepseek_setting');
$finish = TRUE;
}
使用说明:
-
将代码保存为 /source/plugin/deepseek_reply/deepseek_reply.php
-
需要创建以下目录结构:
/source/plugin/deepseek_reply/
├── deepseek_reply.php
└── template/
└── admincp.htm
-
在Discuz后台安装插件后,需要:
- 在DeepSeek官网获取API密钥
- 在插件设置页面配置API密钥和其他参数
- 根据需求调整回复生成参数(temperature、max_tokens等)
注意事项:
- 需要根据实际API文档调整请求参数和响应处理
- 建议添加频率限制防止API滥用
- 建议添加内容审核机制保证回复合规性
- 需要处理异步请求避免影响发帖流程
- 建议添加日志记录功能用于调试
请根据实际需求调整代码中的API端点、参数名称和业务逻辑。
以下是admincp.htm
的典型实现代码,用于显示插件后台配置界面:
<!-- source/plugin/deepseek_reply/template/admincp.htm -->
<div class="bk">
<div class="tb tb-tb tb-form">
<h3 class="tb-title">DeepSeek自动回帖配置</h3>
<form method="post" autocomplete="off" action="admin.php?action=plugins&operation=config&identifier=deepseek_reply">
<input type="hidden" name="formhash" value="{FORMHASH}">
<!-- API密钥设置 -->
<div class="form-group">
<label class="label">API密钥</label>
<div class="input-group">
<input type="password" class="input px" name="api_key" value="{setting['api_key']}" size="50">
<p class="help-text">请前往<a href="https://platform.deepseek.com/api-keys" target="_blank">DeepSeek控制台</a>获取API密钥</p>
</div>
</div>
<!-- 功能开关 -->
<div class="form-group">
<label class="label">启用自动回帖</label>
<div class="input-group">
<label class="radio-inline">
<input type="radio" name="enable" value="1" {if setting['enable']}checked{/if}> 启用
</label>
<label class="radio-inline">
<input type="radio" name="enable" value="0" {if !setting['enable']}checked{/if}> 关闭
</label>
</div>
</div>
<!-- 模型选择 -->
<div class="form-group">
<label class="label">选择模型</label>
<div class="input-group">
<select name="model" class="ps">
<option value="deepseek-chat" {if setting['model'] == 'deepseek-chat'}selected{/if}>DeepSeek Chat(通用对话)</option>
<option value="deepseek-coder" {if setting['model'] == 'deepseek-coder'}selected{/if}>DeepSeek Coder(代码专用)</option>
</select>
</div>
</div>
<!-- 参数调节 -->
<div class="form-group">
<label class="label">生成参数</label>
<div class="input-group">
<div class="pt">
<label>最大长度(max_tokens)</label>
<input type="number" class="input px" name="max_tokens" value="{setting['max_tokens']}" min="50" max="2048">
</div>
<div class="pt">
<label>随机性(temperature 0-2)</label>
<input type="number" class="input px" name="temperature" value="{setting['temperature']}" step="0.1" min="0" max="2">
</div>
</div>
</div>
<!-- 提交按钮 -->
<div class="form-group">
<div class="input-group">
<button type="submit" name="submit" value="submit" class="btn btn-primary">提交保存</button>
</div>
</div>
</form>
</div>
</div>
<style>
.form-group {
margin-bottom: 15px;
padding: 10px;
border-bottom: 1px solid #eee;
}
.label {
display: block;
width: 200px;
float: left;
padding-top: 5px;
font-weight: bold;
}
.input-group {
margin-left: 220px;
}
.help-text {
color: #999;
margin-top: 5px;
}
.pt {
padding: 5px 0;
}
</style>
主要功能说明:
-
表单结构:
- 使用Discuz标准后台样式类(
bk
/tb
/form-group
等)
- 包含必要的formhash验证
- 与plugin_deepseek_reply_admin类中的设置处理逻辑对接
-
配置项说明:
- API密钥输入框:用于填写从DeepSeek获取的密钥
- 功能开关:启用/禁用自动回复功能
- 模型选择:下拉菜单选择不同AI模型
- 参数调节:控制生成内容的长度和随机性
-
数据绑定:
- 使用
{setting['key']}
模板语法显示已保存的配置值
- 通过
{if}
条件判断实现单选按钮和下拉菜单的状态保持
-
安全措施:
- 使用
password
类型输入API密钥
- 包含Discuz标准的formhash验证
- 输入字段限制数值范围(min/max属性)
-
交互提示:
- 添加帮助文字说明参数含义
- 包含官方API密钥获取链接
- 使用友好的中文标签说明
使用注意事项:
-
需要保持与插件PHP代码中字段名称一致:
name="api_key"
对应 $_GET['api_key']
name="model"
对应 $_GET['model']
- 其他参数同理
-
样式可以根据Discuz版本调整,建议:
- 参考当前Discuz版本的后台样式
- 使用官方推荐的CSS类名
- 保持界面风格统一
-
如果需要更复杂的交互,可以:
- 添加JavaScript验证
- 增加参数说明的tooltip
- 添加测试API连接的功能按钮
这个模板文件与之前的PHP代码共同构成完整的后台配置功能,管理员可以在Discuz后台插件设置界面进行参数配置。