当前位置: 首页 > news >正文

Vue项目使用Coze的聊天窗(一)

背景:

vue项目中,调用扣子的agent发起聊天

实现:

快速实现:

  1. 在vue项目的index.html文件中引入扣子Chat SDK
<!doctype html>
<html lang="en"><head><meta charset="UTF-8" /><link rel="icon" type="image/svg+xml" href="/favicon.ico" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>使用coze chat</title><script src="https://lf-cdn.coze.cn/obj/unpkg/flow-platform/chat-app-sdk/1.2.0-beta.17/libs/cn/index.js"></script></head><body><div id="app"></div><script type="module" src="/src/main.ts"></script><script>new CozeWebSDK.WebChatClient({/*** Agent or app settings* for agent* @param config.bot_id - Agent ID.* for app* @param config.type - To integrate a Coze app, you must set the value to app.* @param config.isIframe - Whether to use the iframe method to open the chat box* @param config.appInfo.appId - AI app ID.* @param config.appInfo.workflowId - Workflow or chatflow ID.*/config: {type: 'bot',bot_id: '你的agent id',isIframe: false,},/*** The auth property is used to configure the authentication method.* @param type - Authentication method, default type is 'unauth', which means no authentication is required; it is recommended to set it to 'token', which means authentication is done through PAT (Personal Access Token) or OAuth.* @param token - When the type is set to 'token', you need to configure the PAT (Personal Access Token) or OAuth access key.* @param onRefreshToken - When the access key expires, a new key can be set as needed.*/auth: {type: 'token',token: '你的token',onRefreshToken: async () => 'token'},/*** The userInfo parameter is used to set the display of agent user information in the chat box.* @param userInfo.id - ID of the agent user.* @param userInfo.url - URL address of the user's avatar.* @param userInfo.nickname - Nickname of the agent user.*/userInfo: {id: 'user',url: 'https://lf-coze-web-cdn.coze.cn/obj/eden-cn/lm-lgvj/ljhwZthlaukjlkulzlp/coze/coze-logo.png',nickname: 'User',},ui: {/*** The ui.base parameter is used to add the overall UI effect of the chat window.* @param base.icon - Application icon URL.* @param base.layout - Layout style of the agent chat box window, which can be set as 'pc' or'mobile'.* @param base.lang - System language of the agent, which can be set as 'en' or 'zh-CN'.* @param base.zIndex - The z-index of the chat box.*/base: {icon: 'https://lf-coze-web-cdn.coze.cn/obj/eden-cn/lm-lgvj/ljhwZthlaukjlkulzlp/coze/chatsdk-logo.png',layout: 'pc',lang: 'zh-CN',zIndex: 1000},/*** Controls whether to display the top title bar and the close button* @param header.isShow - Whether to display the top title bar.* @param header.isNeedClose - Whether to display the close button.*/header: {isShow: true,isNeedClose: true,},conversations: {isNeed: true},/*** Controls whether to display the floating ball at the bottom right corner of the page.*/asstBtn: {isNeed: true},/*** The ui.footer parameter is used to add the footer of the chat window.* @param footer.isShow - Whether to display the bottom copy module.* @param footer.expressionText - The text information displayed at the bottom.* @param footer.linkvars - The link copy and link address in the footer.*/footer: {isShow: true,expressionText: 'Powered by ...',},/*** Control the UI and basic capabilities of the chat box.* @param chatBot.title - The title of the chatbox* @param chatBot.uploadable - Whether file uploading is supported.* @param chatBot.width - The width of the agent window on PC is measured in px, default is 460.* @param chatBot.el - Container for setting the placement of the chat box (Element).*/chatBot: {title: 'Coze Bot',uploadable: true,width: 390,},},});</script></body>
</html>

将聊天窗作为一个vue组件

  1. 在vue项目的index.html文件中引入扣子Chat SDK
<script src="https://lf-cdn.coze.cn/obj/unpkg/flow-platform/chat-app-sdk/1.2.0-beta.17/libs/cn/index.js"></script>
  1. 新建一个文件将聊天窗作为一个组件,CozeChat.vue
<template><div ref="chatContainer" class="coze-chat"  style="width: 800px; height: 600px"></div>
</template><script setup>
import "@coze/chat-sdk/webCss";
import { onMounted, ref } from "vue";const chatContainer = ref<HTMLDivElement | null>(null);chatContainer.value = new CozeWebSDK.WebChatClient({/*** Agent or app settings* for agent* @param config.bot_id - Agent ID.* for app* @param config.type - To integrate a Coze app, you must set the value to app.* @param config.isIframe - Whether to use the iframe method to open the chat box* @param config.appInfo.appId - AI app ID.* @param config.appInfo.workflowId - Workflow or chatflow ID.*/config: {type: 'bot',bot_id: '你的agent id',isIframe: false,},/*** The auth property is used to configure the authentication method.* @param type - Authentication method, default type is 'unauth', which means no authentication is required; it is recommended to set it to 'token', which means authentication is done through PAT (Personal Access Token) or OAuth.* @param token - When the type is set to 'token', you need to configure the PAT (Personal Access Token) or OAuth access key.* @param onRefreshToken - When the access key expires, a new key can be set as needed.*/auth: {type: 'token',token: '你的token',onRefreshToken: async () => 'token',},/*** The userInfo parameter is used to set the display of agent user information in the chat box.* @param userInfo.id - ID of the agent user.* @param userInfo.url - URL address of the user's avatar.* @param userInfo.nickname - Nickname of the agent user.*/userInfo: {id: 'user',url: 'https://lf-coze-web-cdn.coze.cn/obj/eden-cn/lm-lgvj/ljhwZthlaukjlkulzlp/coze/coze-logo.png',nickname: 'User',},ui: {/*** The ui.base parameter is used to add the overall UI effect of the chat window.* @param base.icon - Application icon URL.* @param base.layout - Layout style of the agent chat box window, which can be set as 'pc' or'mobile'.* @param base.lang - System language of the agent, which can be set as 'en' or 'zh-CN'.* @param base.zIndex - The z-index of the chat box.*/base: {icon: 'https://lf-coze-web-cdn.coze.cn/obj/eden-cn/lm-lgvj/ljhwZthlaukjlkulzlp/coze/chatsdk-logo.png',layout: 'pc',lang: 'zh-CN',zIndex: 1000,},/*** Controls whether to display the top title bar and the close button* @param header.isShow - Whether to display the top title bar.* @param header.isNeedClose - Whether to display the close button.*/header: {isShow: true,isNeedClose: true,},conversations: {isNeed: true,},/*** Controls whether to display the floating ball at the bottom right corner of the page.*/asstBtn: {isNeed: true,},/*** The ui.footer parameter is used to add the footer of the chat window.* @param footer.isShow - Whether to display the bottom copy module.* @param footer.expressionText - The text information displayed at the bottom.* @param footer.linkvars - The link copy and link address in the footer.*/footer: {isShow: true,expressionText: 'Powered by ...',},/*** Control the UI and basic capabilities of the chat box.* @param chatBot.title - The title of the chatbox* @param chatBot.uploadable - Whether file uploading is supported.* @param chatBot.width - The width of the agent window on PC is measured in px, default is 460.* @param chatBot.el - Container for setting the placement of the chat box (Element).*/chatBot: {title: 'Coze Bot',uploadable: true,width: 390,},},
})
</script><style scoped>
/* 可以根据需要自定义样式 */
</style>
  1. 使用组件,实现聊天窗的唤起,App.vue
<!--扣子聊天窗 --><div style="height:100vh;"><CozeChat /></div>

效果展示:

在这里插入图片描述

http://www.dtcms.com/a/391904.html

相关文章:

  • 关于将tomcat、nginx 实现 注册window服务并实现自启动
  • 【精品资料鉴赏】358页 数字政府大数据中心资源平台治理建设方案
  • 关于Spring Bean之间的循环依赖
  • pake将前端web项目打包成windows可安装文件
  • 低轨卫星应用:MCU、CANFD与DCDC芯片的集成解决方案
  • AI 编程Claude Code使用详细教程
  • vue3 下载文件方式(包括通过url下载文件并修改文件名称,和文件流下载方式)
  • 如何高效筛选海量文献,避免浪费时间?
  • heyday
  • Go语言结构体初始化全面指南与最佳实践
  • 神经网络学习笔记15——高效卷积神经网络架构GhostNet
  • Mysql的Exists条件子查询
  • 电脑系统windows10怎么合盘
  • 一文详解Stata回归分析
  • GPS 定位:连接时空的数字导航革命
  • Rust 特有关键字及用法
  • 关于C++游戏开发入门:如何从零开始实现一个完整的游戏项目!
  • OpenRank结合游戏及算法技术原理
  • 协方差矩阵、皮尔逊相关系数
  • Redis 三大架构模式详解:主从复制、哨兵、Cluster 搭建全指南
  • [x-cmd] 如何安全卸载 x-cmd
  • 整体设计 语言拼凑/逻辑拆解/词典缝合 之 3 词典缝合(“他”):显露词典编纂行列式项的 “自然”三“然”:自然本然/ 自然而然/自然实然
  • linux配置ssh,亲测简单可用
  • SNMP 模块化设计解析
  • 2025的Xmind自定义安装(实测版)
  • AI“闻香识酒”:电子鼻+机器学习开启气味数字化新纪元
  • Coze工作流拆解:成语故事类小红书图文批量创作全流程
  • PyQt6之进度条
  • 【AI编程】Trae配置rules与配置和使用一些目前比较好用的MCP
  • 音乐家不会被束缚,MusicGPT+cpolar让创作更自由