如何写一个discuz AIDeepSeek自动回帖的插件
以下是一个实现DeepSeek自动回帖功能的Discuz插件基础代码框架,包含API密钥对接接口:```php
<?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']['message']['content']) {
$reply = array(
'tid' => $param['tid'],
'message' => $response['choices']['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;
}
```
使用说明:
1. 将代码保存为 `/source/plugin/deepseek_reply/deepseek_reply.php`
2. 需要创建以下目录结构:
```
/source/plugin/deepseek_reply/
├── deepseek_reply.php
└── template/
└── admincp.htm
```
3. 在Discuz后台安装插件后,需要:
- 在DeepSeek官网获取API密钥
- 在插件设置页面配置API密钥和其他参数
- 根据需求调整回复生成参数(temperature、max_tokens等)
注意事项:
1. 需要根据实际API文档调整请求参数和响应处理
2. 建议添加频率限制防止API滥用
3. 建议添加内容审核机制保证回复合规性
4. 需要处理异步请求避免影响发帖流程
5. 建议添加日志记录功能用于调试
请根据实际需求调整代码中的API端点、参数名称和业务逻辑。
以下是`admincp.htm`的典型实现代码,用于显示插件后台配置界面:
```html
<!-- 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>
```
主要功能说明:
1. **表单结构**:
- 使用Discuz标准后台样式类(`bk`/`tb`/`form-group`等)
- 包含必要的formhash验证
- 与plugin_deepseek_reply_admin类中的设置处理逻辑对接
2. **配置项说明**:
- API密钥输入框:用于填写从DeepSeek获取的密钥
- 功能开关:启用/禁用自动回复功能
- 模型选择:下拉菜单选择不同AI模型
- 参数调节:控制生成内容的长度和随机性
3. **数据绑定**:
- 使用`{setting['key']}`模板语法显示已保存的配置值
- 通过`{if}`条件判断实现单选按钮和下拉菜单的状态保持
4. **安全措施**:
- 使用`password`类型输入API密钥
- 包含Discuz标准的formhash验证
- 输入字段限制数值范围(min/max属性)
5. **交互提示**:
- 添加帮助文字说明参数含义
- 包含官方API密钥获取链接
- 使用友好的中文标签说明
使用注意事项:
1. 需要保持与插件PHP代码中字段名称一致:
- `name="api_key"` 对应 $_GET['api_key']
- `name="model"` 对应 $_GET['model']
- 其他参数同理
2. 样式可以根据Discuz版本调整,建议:
- 参考当前Discuz版本的后台样式
- 使用官方推荐的CSS类名
- 保持界面风格统一
3. 如果需要更复杂的交互,可以:
- 添加JavaScript验证
- 增加参数说明的tooltip
- 添加测试API连接的功能按钮
这个模板文件与之前的PHP代码共同构成完整的后台配置功能,管理员可以在Discuz后台插件设置界面进行参数配置。 编写Discuz AIDeepSeek自动回帖插件,首先需掌握Discuz插件开发基础。以下是基于您提供的基础代码框架,创意性的实现思路:
php
// 引入API接口文件
include 'AIDeepSeekAPI.php';
// Discuz插件安装钩子
function plugin_install() {
// 初始化API密钥
$api_key = 'YOUR_API_KEY';
// 存储API密钥至数据库
C::t('common_setting')->update('aidEEPSEEK_api_key', $api_key);
}
// 自动回帖触发函数
function forum_post回复($params) {
// 从数据库获取API密钥
$api_key = C::t('common_setting')->fetch('aidEEPSEEK_api_key')['aidEEPSEEK_api_key'];
// 使用API生成回帖内容
$reply_content = AIDeepSeekAPI::generateReply($api_key, $params['post']['message']);
// 发送回帖
$reply_data = array(
'fid' => $params['fid'],
'tid' => $params['tid'],
'pid' => $params['pid'],
'message' => $reply_content,
'authorid' => 0, // 设为系统回复
);
C::t('forum_post')->insert($reply_data);
}
此代码框架通过钩子机制,在帖子发布后自动调用`forum_post回复`函数,利用AIDeepSeekAPI生成回复内容,并插入数据库。记得替换`YOUR_API_KEY`为您的实际API密钥。创意之处在于,让机器智能化地参与到社区互动中,提升论坛活跃度。
[本文内容由人工智能清华智谱 - ChatGLM辅助生成,仅供参考] 编写Discuz AIDeepSeek自动回帖插件,首先要准备完整的插件基础代码框架,并接入API密钥。以下简述关键步骤:
1. **初始化API接口**:在插件中创建一个类或函数,用于初始化与AIDeepSeek的API连接,配置API密钥等认证信息。
2. **捕捉回帖事件**:利用Discuz的钩子(hook)机制,捕捉到用户发帖的事件。
3. **调用AI接口**:在捕捉到发帖事件后,调用AIDeepSeek的API,将帖子内容发送给AI进行智能回复。
4. **生成回复内容**:接收AI返回的回复内容,并将其格式化为适合论坛的格式。
5. **自动回帖**:将生成的回复内容作为新帖子发表到论坛。
示例代码框架如下:
php
class AIDeepSeekAutoReply {
private $apiKey;
public function __construct($apiKey) {
$this->apiKey = $apiKey;
}
public function replyToPost($postId, $postContent) {
$response = $this->callAI($postContent);
$this->createReplyPost($postId, $response);
}
private function callAI($content) {
// 发送请求到AIDeepSeek API并获取回复
// ...
return $aiReply;
}
private function createReplyPost($postId, $replyContent) {
// 在Discuz创建新帖子
// ...
}
}
记得在插件管理中注册你的插件,并确保钩子正确设置。创意在于让AI与论坛用户自然交流,提升论坛互动性!
[本文内容由人工智能清华智谱 - ChatGLM辅助生成,仅供参考]
页:
[1]