Commit 004ed37c authored by nanahira's avatar nanahira

snake case for elements

parent be53352e
...@@ -22,6 +22,9 @@ app.plugin(WeComBot, { ...@@ -22,6 +22,9 @@ app.plugin(WeComBot, {
corpId: 'corpId', // 企业 ID,在企业信息 https://work.weixin.qq.com/wework_admin/frame#profile 中查看 corpId: 'corpId', // 企业 ID,在企业信息 https://work.weixin.qq.com/wework_admin/frame#profile 中查看
agentId: 'agentId', // 应用 ID,在应用管理中查看 agentId: 'agentId', // 应用 ID,在应用管理中查看
secret: 'secret', // 应用密钥,在应用管理中查看 secret: 'secret', // 应用密钥,在应用管理中查看
path: '/wecom', // 回调 API 路径
token: 'token', // 回调 API token,在应用消息接收设置中设置并填入
encodingAESKey: 'encodingAESKey', // 加密密钥,在应用消息接收设置中设置并填入
menus: [ menus: [
{ {
type: 'view', // 打开网页 type: 'view', // 打开网页
...@@ -45,9 +48,6 @@ app.plugin(WeComBot, { ...@@ -45,9 +48,6 @@ app.plugin(WeComBot, {
], ],
}, },
], ],
path: '/wecom', // 回调 API 路径
token: 'token', // 回调 API token,在应用消息接收设置中设置并填入
encodingAESKey: 'encodingAESKey', // 加密密钥,在应用消息接收设置中设置并填入
}); });
app.on('wecom/LOCATION', (session) => { app.on('wecom/LOCATION', (session) => {
...@@ -107,15 +107,15 @@ app.command('dish').action(async (argv) => { ...@@ -107,15 +107,15 @@ app.command('dish').action(async (argv) => {
card_type: 'multiple_interaction', card_type: 'multiple_interaction',
}, },
[ [
segment('main_title', { segment('mainTitle', {
title: '晚上想吃什么?', title: '晚上想吃什么?',
desc: '有很多好吃的呢!', desc: '有很多好吃的呢!',
}), }),
segment('select_list', {}, [ segment('selectList', {}, [
segment( segment(
'', '',
{ {
question_key: 'main', questionKey: 'main',
title: '主食', title: '主食',
}, },
[ [
...@@ -126,7 +126,7 @@ app.command('dish').action(async (argv) => { ...@@ -126,7 +126,7 @@ app.command('dish').action(async (argv) => {
segment( segment(
'', '',
{ {
question_key: 'meat', questionKey: 'meat',
title: '肉类', title: '肉类',
}, },
[ [
...@@ -138,11 +138,11 @@ app.command('dish').action(async (argv) => { ...@@ -138,11 +138,11 @@ app.command('dish').action(async (argv) => {
segment( segment(
'', '',
{ {
question_key: 'soup', questionKey: 'soup',
title: '', title: '',
}, },
[ [
segment('option_list', {}, [ segment('optionList', {}, [
segment('', { id: 'tomato', text: '番茄汤' }), segment('', { id: 'tomato', text: '番茄汤' }),
segment('', { id: 'mushroom', text: '蘑菇汤' }), segment('', { id: 'mushroom', text: '蘑菇汤' }),
segment('', { id: 'fish', text: '鱼汤' }), segment('', { id: 'fish', text: '鱼汤' }),
...@@ -150,7 +150,7 @@ app.command('dish').action(async (argv) => { ...@@ -150,7 +150,7 @@ app.command('dish').action(async (argv) => {
], ],
), ),
]), ]),
segment('submit_button', { segment('submitButton', {
text: '点菜!', text: '点菜!',
key: 'submit', key: 'submit',
}), }),
......
import { Dict, Element, Messenger, segment } from 'koishi'; import { Dict, Element, Messenger, segment, snakeCase } from 'koishi';
import type WeComBot from './index'; import type WeComBot from './index';
import { import {
CommonOutMessage, CommonOutMessage,
...@@ -14,6 +14,7 @@ import fs from 'fs'; ...@@ -14,6 +14,7 @@ import fs from 'fs';
import path from 'path'; import path from 'path';
import FileType from 'file-type'; import FileType from 'file-type';
import cryptoRandomString from 'crypto-random-string'; import cryptoRandomString from 'crypto-random-string';
import { transformKey } from './utils';
export class WeComMessenger extends Messenger<WeComBot> { export class WeComMessenger extends Messenger<WeComBot> {
private buffer = ''; private buffer = '';
...@@ -150,7 +151,7 @@ export class WeComMessenger extends Messenger<WeComBot> { ...@@ -150,7 +151,7 @@ export class WeComMessenger extends Messenger<WeComBot> {
private readonly cardPropertyArrayKeyList = { private readonly cardPropertyArrayKeyList = {
action_menu: 'action_list', action_menu: 'action_list',
checkbox: 'option_list', check_box: 'option_list',
button_selection: 'option_list', button_selection: 'option_list',
}; };
...@@ -158,28 +159,30 @@ export class WeComMessenger extends Messenger<WeComBot> { ...@@ -158,28 +159,30 @@ export class WeComMessenger extends Messenger<WeComBot> {
select_list: 'option_list', select_list: 'option_list',
}; };
private parseCardProperties(element: Element, parent?: Element) { private parseCardProperties(element: Element, parentKey = '') {
const data: any = { ...element.attrs }; const data: any = transformKey(element.attrs, snakeCase);
const topKey = snakeCase(element.type);
const arrayKey = const arrayKey =
this.cardPropertyArrayKeyList[element.type] || this.cardPropertyArrayKeyList[topKey] ||
this.cardPropertyArrayPartentKeyList[parent?.type || '']; this.cardPropertyArrayPartentKeyList[parentKey];
for (const prop of element.children) { for (const prop of element.children) {
if (arrayKey && prop.type !== arrayKey) { if (arrayKey && topKey !== arrayKey) {
data[arrayKey] ??= []; data[arrayKey] ??= [];
data[arrayKey].push(this.parseCardProperties(prop, element)); data[arrayKey].push(this.parseCardProperties(prop, topKey));
} }
if (!prop.type) { if (!prop.type) {
continue; continue;
} }
const key = snakeCase(prop.type);
let value: any; let value: any;
if (prop.type.endsWith('_list')) { if (key.endsWith('_list')) {
value = prop.children.map((item) => value = prop.children.map((item) =>
this.parseCardProperties(item, prop), this.parseCardProperties(item, key),
); );
} else { } else {
value = this.parseCardProperties(prop, element); value = this.parseCardProperties(prop, topKey);
} }
data[prop.type] = value; data[key] = value;
} }
if (!data.task_id && element.type.startsWith('wecom:')) { if (!data.task_id && element.type.startsWith('wecom:')) {
data.task_id = cryptoRandomString({ length: 127, type: 'alphanumeric' }); data.task_id = cryptoRandomString({ length: 127, type: 'alphanumeric' });
...@@ -245,7 +248,7 @@ export class WeComMessenger extends Messenger<WeComBot> { ...@@ -245,7 +248,7 @@ export class WeComMessenger extends Messenger<WeComBot> {
const wecomMessageType = type.slice(6); const wecomMessageType = type.slice(6);
return this.sendGenericMessage({ return this.sendGenericMessage({
msgtype: wecomMessageType, msgtype: wecomMessageType,
[wecomMessageType]: attrs, [wecomMessageType]: transformKey(attrs, snakeCase),
}); });
} }
return; return;
......
...@@ -155,3 +155,24 @@ export class WeComToken { ...@@ -155,3 +155,24 @@ export class WeComToken {
agentId: string; agentId: string;
token: string; token: string;
} }
export function transformKey(
obj: Record<string, any>,
transformer: (key: string) => string,
visited = new Set(),
) {
if (typeof obj !== 'object' || visited.has(obj)) return obj;
const result: Record<string, any> = {};
if (visited.has(obj)) return;
visited.add(obj);
for (const key of Object.keys(obj)) {
let value = obj[key];
if (Array.isArray(value)) {
value = value.map((item) => transformKey(item, transformer, visited));
} else {
value = transformKey(value, transformer, visited);
}
result[transformer(key)] = value;
}
return result;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment