Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
K
koishi-plugin-adapter-wecom
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
3rdeye
koishi-plugin-adapter-wecom
Commits
004ed37c
Commit
004ed37c
authored
Dec 18, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
snake case for elements
parent
be53352e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
23 deletions
+47
-23
dev/index.ts
dev/index.ts
+10
-10
src/message.ts
src/message.ts
+16
-13
src/utils.ts
src/utils.ts
+21
-0
No files found.
dev/index.ts
View file @
004ed37c
...
...
@@ -22,6 +22,9 @@ app.plugin(WeComBot, {
corpId
:
'
corpId
'
,
// 企业 ID,在企业信息 https://work.weixin.qq.com/wework_admin/frame#profile 中查看
agentId
:
'
agentId
'
,
// 应用 ID,在应用管理中查看
secret
:
'
secret
'
,
// 应用密钥,在应用管理中查看
path
:
'
/wecom
'
,
// 回调 API 路径
token
:
'
token
'
,
// 回调 API token,在应用消息接收设置中设置并填入
encodingAESKey
:
'
encodingAESKey
'
,
// 加密密钥,在应用消息接收设置中设置并填入
menus
:
[
{
type
:
'
view
'
,
// 打开网页
...
...
@@ -45,9 +48,6 @@ app.plugin(WeComBot, {
],
},
],
path
:
'
/wecom
'
,
// 回调 API 路径
token
:
'
token
'
,
// 回调 API token,在应用消息接收设置中设置并填入
encodingAESKey
:
'
encodingAESKey
'
,
// 加密密钥,在应用消息接收设置中设置并填入
});
app
.
on
(
'
wecom/LOCATION
'
,
(
session
)
=>
{
...
...
@@ -107,15 +107,15 @@ app.command('dish').action(async (argv) => {
card_type
:
'
multiple_interaction
'
,
},
[
segment
(
'
main
_t
itle
'
,
{
segment
(
'
main
T
itle
'
,
{
title
:
'
晚上想吃什么?
'
,
desc
:
'
有很多好吃的呢!
'
,
}),
segment
(
'
select
_l
ist
'
,
{},
[
segment
(
'
select
L
ist
'
,
{},
[
segment
(
''
,
{
question
_k
ey
:
'
main
'
,
question
K
ey
:
'
main
'
,
title
:
'
主食
'
,
},
[
...
...
@@ -126,7 +126,7 @@ app.command('dish').action(async (argv) => {
segment
(
''
,
{
question
_k
ey
:
'
meat
'
,
question
K
ey
:
'
meat
'
,
title
:
'
肉类
'
,
},
[
...
...
@@ -138,11 +138,11 @@ app.command('dish').action(async (argv) => {
segment
(
''
,
{
question
_k
ey
:
'
soup
'
,
question
K
ey
:
'
soup
'
,
title
:
'
汤
'
,
},
[
segment
(
'
option
_l
ist
'
,
{},
[
segment
(
'
option
L
ist
'
,
{},
[
segment
(
''
,
{
id
:
'
tomato
'
,
text
:
'
番茄汤
'
}),
segment
(
''
,
{
id
:
'
mushroom
'
,
text
:
'
蘑菇汤
'
}),
segment
(
''
,
{
id
:
'
fish
'
,
text
:
'
鱼汤
'
}),
...
...
@@ -150,7 +150,7 @@ app.command('dish').action(async (argv) => {
],
),
]),
segment
(
'
submit
_b
utton
'
,
{
segment
(
'
submit
B
utton
'
,
{
text
:
'
点菜!
'
,
key
:
'
submit
'
,
}),
...
...
src/message.ts
View file @
004ed37c
import
{
Dict
,
Element
,
Messenger
,
segment
}
from
'
koishi
'
;
import
{
Dict
,
Element
,
Messenger
,
segment
,
snakeCase
}
from
'
koishi
'
;
import
type
WeComBot
from
'
./index
'
;
import
{
CommonOutMessage
,
...
...
@@ -14,6 +14,7 @@ import fs from 'fs';
import
path
from
'
path
'
;
import
FileType
from
'
file-type
'
;
import
cryptoRandomString
from
'
crypto-random-string
'
;
import
{
transformKey
}
from
'
./utils
'
;
export
class
WeComMessenger
extends
Messenger
<
WeComBot
>
{
private
buffer
=
''
;
...
...
@@ -150,7 +151,7 @@ export class WeComMessenger extends Messenger<WeComBot> {
private
readonly
cardPropertyArrayKeyList
=
{
action_menu
:
'
action_list
'
,
checkbox
:
'
option_list
'
,
check
_
box
:
'
option_list
'
,
button_selection
:
'
option_list
'
,
};
...
...
@@ -158,28 +159,30 @@ export class WeComMessenger extends Messenger<WeComBot> {
select_list
:
'
option_list
'
,
};
private
parseCardProperties
(
element
:
Element
,
parent
?:
Element
)
{
const
data
:
any
=
{
...
element
.
attrs
};
private
parseCardProperties
(
element
:
Element
,
parentKey
=
''
)
{
const
data
:
any
=
transformKey
(
element
.
attrs
,
snakeCase
);
const
topKey
=
snakeCase
(
element
.
type
);
const
arrayKey
=
this
.
cardPropertyArrayKeyList
[
element
.
type
]
||
this
.
cardPropertyArrayPartentKeyList
[
parent
?.
type
||
''
];
this
.
cardPropertyArrayKeyList
[
topKey
]
||
this
.
cardPropertyArrayPartentKeyList
[
parent
Key
];
for
(
const
prop
of
element
.
children
)
{
if
(
arrayKey
&&
prop
.
type
!==
arrayKey
)
{
if
(
arrayKey
&&
topKey
!==
arrayKey
)
{
data
[
arrayKey
]
??
=
[];
data
[
arrayKey
].
push
(
this
.
parseCardProperties
(
prop
,
element
));
data
[
arrayKey
].
push
(
this
.
parseCardProperties
(
prop
,
topKey
));
}
if
(
!
prop
.
type
)
{
continue
;
}
const
key
=
snakeCase
(
prop
.
type
);
let
value
:
any
;
if
(
prop
.
type
.
endsWith
(
'
_list
'
))
{
if
(
key
.
endsWith
(
'
_list
'
))
{
value
=
prop
.
children
.
map
((
item
)
=>
this
.
parseCardProperties
(
item
,
prop
),
this
.
parseCardProperties
(
item
,
key
),
);
}
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:
'
))
{
data
.
task_id
=
cryptoRandomString
({
length
:
127
,
type
:
'
alphanumeric
'
});
...
...
@@ -245,7 +248,7 @@ export class WeComMessenger extends Messenger<WeComBot> {
const
wecomMessageType
=
type
.
slice
(
6
);
return
this
.
sendGenericMessage
({
msgtype
:
wecomMessageType
,
[
wecomMessageType
]:
attrs
,
[
wecomMessageType
]:
transformKey
(
attrs
,
snakeCase
)
,
});
}
return
;
...
...
src/utils.ts
View file @
004ed37c
...
...
@@ -155,3 +155,24 @@ export class WeComToken {
agentId
:
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
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment