Yandex Dialogs SDK 项目教程
1. 项目目录结构及介绍
yandex-dialogs-sdk/
├── examples/
│ ├── src/
│ └── tests/
├── src/
│ ├── index.js
│ └── ...
├── tests/
│ └── ...
├── .gitignore
├── .prettierrc
├── CHANGELOG.md
├── LICENSE
├── README.md
├── jest.config.js
├── package.json
├── tsconfig.json
└── tslint.json
目录结构介绍
- examples/: 包含项目的示例代码,帮助开发者快速上手。
- src/: 示例代码的源文件。
- tests/: 示例代码的测试文件。
- src/: 项目的主要源代码文件。
- index.js: 项目的入口文件。
- tests/: 项目的测试文件。
- .gitignore: Git 忽略文件配置。
- .prettierrc: Prettier 代码格式化配置文件。
- CHANGELOG.md: 项目更新日志。
- LICENSE: 项目许可证文件。
- README.md: 项目说明文档。
- jest.config.js: Jest 测试框架配置文件。
- package.json: 项目的依赖管理文件。
- tsconfig.json: TypeScript 配置文件。
- tslint.json: TSLint 代码风格检查配置文件。
2. 项目启动文件介绍
入口文件:src/index.js
src/index.js
是项目的入口文件,负责初始化 SDK 并处理对话请求。以下是一个简单的示例:
const { Alice, Reply, Markup } = require('yandex-dialogs-sdk');
const alice = new Alice();
const M = Markup;
alice.command('', async ctx => {
return Reply.text('Look, what I can!');
});
alice.command('Give a piece of advice', async ctx => {
return Reply.text('Make const not var');
});
alice.command(['What is trending now', 'Watch films', 'Whats in the theatre'], ctx => {
return Reply.text(`What's trending now...`);
});
alice.on('response', ctx => {
console.log(ctx.response);
});
启动命令
在 package.json
中,通常会有一个启动脚本:
{
"scripts": {
"start": "node src/index.js"
}
}
通过运行 npm start
命令,可以启动项目。
3. 项目配置文件介绍
package.json
package.json
是 Node.js 项目的核心配置文件,包含项目的元数据和依赖项。以下是一些关键字段:
{
"name": "yandex-dialogs-sdk",
"version": "1.0.0",
"description": "SDK for building skills for Yandex.Alice",
"main": "src/index.js",
"scripts": {
"start": "node src/index.js",
"test": "jest"
},
"dependencies": {
"yandex-dialogs-sdk": "^1.0.0"
},
"devDependencies": {
"jest": "^26.0.0"
}
}
tsconfig.json
tsconfig.json
是 TypeScript 项目的配置文件,定义了 TypeScript 编译器的选项。以下是一个简单的示例:
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outDir": "./dist",
"strict": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
jest.config.js
jest.config.js
是 Jest 测试框架的配置文件,定义了测试运行时的选项。以下是一个简单的示例:
module.exports = {
testEnvironment: 'node',
testMatch: ['**/tests/**/*.test.js'],
verbose: true
};
通过这些配置文件,开发者可以自定义项目的编译、测试和运行行为。