Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
K
koishi-plugin-adapter-wechaty
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
2
Issues
2
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
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-wechaty
Commits
586def45
Commit
586def45
authored
Feb 01, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
catchup
parent
83a30387
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
114 additions
and
1 deletion
+114
-1
README.md
README.md
+13
-1
src/utils.ts
src/utils.ts
+101
-0
No files found.
README.md
View file @
586def45
# koishi-plugin-adapter-wechaty
Koishi Wechaty Adapter
\ No newline at end of file
Koishi 的
[
Wechaty
](
https://wechaty.js.org/
)
适配器插件
## 安装
### npm
```
bash
npm
install
koishi-plugin-adapter-wechaty
```
## 配置
详见
`config.ts`
部分,或详见 Schema 描述配置。
src/utils.ts
0 → 100644
View file @
586def45
import
{
Contact
,
Friendship
,
Message
,
Wechaty
}
from
'
wechaty
'
;
import
PUPPET
from
'
wechaty-puppet
'
;
import
{
Bot
,
segment
,
Session
}
from
'
koishi
'
;
import
moment
from
'
moment
'
;
import
{
FriendshipType
}
from
'
wechaty-puppet/dist/cjs/src/schemas/friendship
'
;
export
const
adaptContact
=
async
(
contact
:
Contact
):
Promise
<
Bot
.
User
>
=>
{
const
avatar
=
await
contact
.
avatar
();
return
{
userId
:
contact
.
id
,
username
:
contact
.
name
(),
nickname
:
contact
.
name
(),
avatar
:
avatar
&&
`base64://
${
await
avatar
.
toBase64
()}
`
,
};
};
async
function
extractBuffer
(
message
:
Message
,
segmentFactory
:
(
buffer
:
Buffer
)
=>
string
,
):
Promise
<
string
>
{
const
file
=
await
message
.
toFileBox
();
if
(
!
file
)
{
return
;
}
const
buffer
=
await
file
.
toBuffer
();
return
segmentFactory
(
buffer
);
}
export
const
adaptMessage
=
async
(
message
:
Message
):
Promise
<
Bot
.
Message
>
=>
{
let
content
:
string
;
switch
(
message
.
type
())
{
case
PUPPET
.
types
.
Message
.
Text
:
const
text
=
message
.
text
();
content
=
text
.
replace
(
/@
([^\s]
+
)(\s?)
/g
,
(
_
,
name
,
suffix
)
=>
segment
.
at
(
name
)
+
suffix
,
);
break
;
case
PUPPET
.
types
.
Message
.
Image
:
content
=
await
extractBuffer
(
message
,
(
b
)
=>
segment
.
image
(
b
));
break
;
case
PUPPET
.
types
.
Message
.
Audio
:
content
=
await
extractBuffer
(
message
,
(
b
)
=>
segment
.
audio
(
b
));
break
;
case
PUPPET
.
types
.
Message
.
Video
:
content
=
await
extractBuffer
(
message
,
(
b
)
=>
segment
.
video
(
b
));
break
;
case
PUPPET
.
types
.
Message
.
Attachment
:
content
=
await
extractBuffer
(
message
,
(
b
)
=>
segment
.
file
(
b
));
break
;
case
PUPPET
.
types
.
Message
.
Url
:
const
url
=
await
message
.
toUrlLink
();
content
=
segment
(
'
url
'
,
{
url
:
url
.
url
(),
description
:
url
.
description
(),
thumb
:
url
.
thumbnailUrl
(),
title
:
url
.
title
(),
thumbnailUrl
:
url
.
thumbnailUrl
(),
});
case
PUPPET
.
types
.
Message
.
Contact
:
const
contact
=
await
message
.
toContact
();
content
=
segment
(
'
contact
'
,
{
userId
:
contact
.
id
,
username
:
contact
.
name
(),
nickname
:
contact
.
name
(),
});
default
:
return
;
}
if
(
!
content
)
{
return
;
}
const
source
=
message
.
from
();
const
room
=
message
.
room
();
const
result
:
Bot
.
Message
=
{
author
:
await
adaptContact
(
source
),
userId
:
source
.
id
,
messageId
:
message
.
id
,
timestamp
:
moment
(
message
.
date
()).
unix
()
*
1000
,
content
,
};
if
(
room
)
{
result
.
guildId
=
result
.
channelId
=
room
.
id
;
}
return
result
;
};
export
const
SessionFactory
=
{
friendship
:
(
friendship
:
Friendship
):
Partial
<
Session
>
=>
{
const
session
:
Partial
<
Session
>
=
{};
const
from
=
friendship
.
contact
();
session
.
userId
=
from
.
id
;
session
.
messageId
=
friendship
.
id
;
switch
(
friendship
.
type
())
{
case
FriendshipType
.
Receive
:
session
.
type
=
'
friend-request
'
;
break
;
}
return
session
;
},
};
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