Commit a30967cc authored by nanahira's avatar nanahira

add full type support

parent eb84676d
...@@ -26,12 +26,10 @@ app.plugin(adapterPlugin, { ...@@ -26,12 +26,10 @@ app.plugin(adapterPlugin, {
encodingAESKey: 'encodingAESKey', // 加密密钥,在应用消息接收设置中设置并填入 encodingAESKey: 'encodingAESKey', // 加密密钥,在应用消息接收设置中设置并填入
}); });
app.on('wecom/event', (session) => { app.on('wecom/LOCATION', (session) => {
console.log(`Got event ${JSON.stringify(session.wecom)}`); console.log(
}); `Got location for ${session.userId}: ${session.wecom.Longitude} ${session.wecom.Latitude}`,
);
app.on('wecom/message', (session) => {
console.log(`Got custom message ${JSON.stringify(session.wecom)}`);
}); });
app.on('message', (session) => { app.on('message', (session) => {
......
...@@ -37,7 +37,6 @@ export interface WecomEventBody { ...@@ -37,7 +37,6 @@ export interface WecomEventBody {
Event?: string; Event?: string;
AgentID: number; AgentID: number;
MsgId?: number; MsgId?: number;
EventKey?: string;
} }
export interface WecomChatBody extends WecomEventBody { export interface WecomChatBody extends WecomEventBody {
...@@ -74,31 +73,6 @@ export interface WecomLinkBody extends WecomEventBody { ...@@ -74,31 +73,6 @@ export interface WecomLinkBody extends WecomEventBody {
PicUrl: string; PicUrl: string;
} }
export interface CardEventSelectedItems {
SelectedItem: CardEventSelectedItem | CardEventSelectedItem[];
}
export interface CardEventSelectedItem {
QuestionKey: string;
OptionIds: CardEventOptionIds;
}
export interface CardEventOptionIds {
OptionId: string | string[];
}
export interface WecomCardEventBody extends WecomEventBody {
TaskId: string;
CardType: string;
SelectedItems?: CardEventSelectedItems;
}
export interface WecomLocationBody extends WecomEventBody {
Latitude: number;
Longitude: number;
Precision: number;
}
export interface TokenReturnMessage extends WecomApiResponse { export interface TokenReturnMessage extends WecomApiResponse {
access_token: string; access_token: string;
expires_in: number; expires_in: number;
......
import { WecomEventBody } from './def';
import { Session } from 'koishi';
type MayBeArray<T> = T | T[];
export interface EventKeyBody {
EventKey: string;
}
export interface ScanCodeEventBody extends EventKeyBody {
ScanCodeInfo: {
ScanType: string;
ScanResult: string;
};
}
export interface PhotoEventBody extends EventKeyBody {
SendPicsInfo: {
Count: number;
PicsList: {
item: MayBeArray<{
PicMd5Sum: string;
}>;
};
};
}
export interface CardEventSelectedItems {
SelectedItem: CardEventSelectedItem | CardEventSelectedItem[];
}
export interface CardEventSelectedItem {
QuestionKey: string;
OptionIds: CardEventOptionIds;
}
export interface CardEventOptionIds {
OptionId: string | string[];
}
export interface WecomCardEventBody {
TaskId: string;
CardType: string;
SelectedItems?: CardEventSelectedItems;
}
export interface CardEvent extends EventKeyBody {
TaskId: string;
CardType: string;
ResponseCode: number;
}
// eslint-disable-next-line @typescript-eslint/ban-types
export type WecomEventFunction<T = {}> = (
session: Session & { wecom: WecomEventBody & T },
) => void;
export * from './def'; export * from './def';
export * from './WeComUser'; export * from './specific/WeComUser';
export * from './WeComAgentInfo'; export * from './specific/WeComAgentInfo';
export * from './specific/ApprovalInfo';
export * from './specific/ChangeContact';
export * from './events';
export interface ApprovalInfo {
ThirdNo: string;
OpenSpName: string;
OpenTemplateId: string;
OpenSpStatus: string;
ApplyTime: string;
ApplyUserName: string;
ApplyUserId: string;
ApplyUserParty: string;
ApplyUserImage: string;
ApprovalNodes: ApprovalNodes;
NotifyNodes: NotifyNodes;
approverstep: string;
}
interface NotifyNodes {
NotifyNode: NotifyNode | NotifyNode[];
}
interface NotifyNode {
ItemName: string;
ItemUserId: string;
ItemImage: string;
}
interface ApprovalNodes {
ApprovalNode: ApprovalNode | ApprovalNode[];
}
interface ApprovalNode {
NodeStatus: string;
NodeAttr: string;
NodeType: string;
Items: Items;
}
interface Items {
Item: Item | Item[];
}
interface Item {
ItemName: string;
ItemUserId: string;
ItemImage: string;
ItemStatus: string;
ItemSpeech: string;
ItemOpTime: string;
}
import { ChangeContactUser } from './ChangeContactUser';
export * from './ChangeContactUser';
export type ChangeContact<
K extends keyof ChangeContactMap = keyof ChangeContactMap,
> = {
ChangeType: K;
} & ChangeContactMap[K];
export interface ChangeContactParty {
Id: number;
Name: string;
ParentId: number;
}
export interface ChangeContactMap {
create_user: ChangeContactUser;
update_user: ChangeContactUser;
delete_user: {
UserID: string;
};
create_party: ChangeContactParty & { Order: number };
update_party: ChangeContactParty;
delete_party: {
Id: number;
};
update_tag: {
TagId: number;
AddUserItems: string;
DelUserItems: string;
AddPartyItems: string;
DelPartyItems: string;
};
}
export interface ChangeContactUser {
UserID: string;
Name: string;
Department: string;
MainDepartment: string;
IsLeaderInDept: string;
DirectLeader: string;
Position: string;
Mobile: string;
Gender: string;
Email: string;
BizMail: string;
Status: string;
Avatar: string;
Alias: string;
Telephone: string;
Address: string;
ExtAttr: ExtAttr;
}
interface ExtAttr {
Item: Item[];
}
interface Item {
Name: string;
Type: string;
Text?: Text;
Web?: Web;
}
interface Web {
Title: string;
Url: string;
}
interface Text {
Value: string;
}
// import 'source-map-support/register'; // import 'source-map-support/register';
import { WecomEventBody, WecomEventResponse } from './def'; import {
import { Adapter, Session } from 'koishi'; ApprovalInfo,
CardEvent,
CardEventSelectedItems,
ChangeContact,
EventKeyBody,
PhotoEventBody,
ScanCodeEventBody,
WecomEventBody,
WecomEventFunction,
} from './def';
import { Adapter } from 'koishi';
import HttpServer from './http'; import HttpServer from './http';
import { BotConfig, WeComBot } from './bot'; import { BotConfig, WeComBot } from './bot';
import { AdapterConfig } from './utils'; import { AdapterConfig } from './utils';
...@@ -10,8 +20,52 @@ declare module 'koishi' { ...@@ -10,8 +20,52 @@ declare module 'koishi' {
wecom?: WecomEventBody; wecom?: WecomEventBody;
} }
interface EventMap { interface EventMap {
'wecom/event'(session: Session): void; 'wecom/enter_agent': WecomEventFunction;
'wecom/message'(session: Session): void; 'wecom/subscribe': WecomEventFunction;
'wecom/unsubscribe': WecomEventFunction;
'wecom/LOCATION': WecomEventFunction<{
Latitude: number;
Longitude: number;
Precision: number;
}>;
'wecom/batch_job_result': WecomEventFunction<{
BatchJob: {
JobId: string;
JobType: string;
ErrCode: number;
ErrMsg: string;
};
}>;
'wecom/click': WecomEventFunction<EventKeyBody>;
'wecom/view': WecomEventFunction<EventKeyBody>;
'wecom/scan': WecomEventFunction<ScanCodeEventBody>;
'wecom/scancode_waitmsg': WecomEventFunction<ScanCodeEventBody>;
'wecom/pic_sysphoto': WecomEventFunction<PhotoEventBody>;
'wecom/pic_photo_or_album': WecomEventFunction<PhotoEventBody>;
'wecom/pic_weixin': WecomEventFunction<PhotoEventBody>;
'wecom/location_select': WecomEventFunction<
EventKeyBody & {
SendLocationInfo: {
Location_X: number;
Location_Y: number;
Scale: number;
Label: string;
Poiname: string;
};
}
>;
'wecom/share_agent_change': WecomEventFunction;
'wecom/share_chain_change': WecomEventFunction;
'wecom/template_card_event': WecomEventFunction<
CardEvent & {
SelectedItems: CardEventSelectedItems;
}
>;
'wecom/template_card_menu_event': WecomEventFunction<CardEvent>;
'wecom/open_approval_change': WecomEventFunction<{
ApprovalInfo: ApprovalInfo;
}>;
'wecom/change_contact': WecomEventFunction<ChangeContact>;
} }
} }
......
...@@ -4,10 +4,8 @@ import { ...@@ -4,10 +4,8 @@ import {
WecomChatBody, WecomChatBody,
WecomEventResponse, WecomEventResponse,
WecomLinkBody, WecomLinkBody,
WecomLocationBody,
WecomLocationMessageBody, WecomLocationMessageBody,
WecomPicBody, WecomPicBody,
WecomResponse,
WeComUser, WeComUser,
WecomVideoBody, WecomVideoBody,
WecomVoiceBody, WecomVoiceBody,
...@@ -55,10 +53,8 @@ export function adaptSession(bot: WeComBot, input: WecomEventResponse) { ...@@ -55,10 +53,8 @@ export function adaptSession(bot: WeComBot, input: WecomEventResponse) {
timestamp: body.CreateTime, timestamp: body.CreateTime,
}; };
if (body.MsgType === 'event') { if (body.MsgType === 'event') {
session.type = 'wecom/event'; session.type = `wecom/${body.Event}`;
session.subtype = body.Event;
} else { } else {
let isKnownMessage = true;
switch (body.MsgType) { switch (body.MsgType) {
case 'text': case 'text':
const textBody = body as WecomChatBody; const textBody = body as WecomChatBody;
...@@ -98,18 +94,13 @@ export function adaptSession(bot: WeComBot, input: WecomEventResponse) { ...@@ -98,18 +94,13 @@ export function adaptSession(bot: WeComBot, input: WecomEventResponse) {
}); });
break; break;
default: default:
isKnownMessage = false; return;
}
if (isKnownMessage) {
session.type = 'message';
session.subtype = 'private';
session.author = {
userId: session.userId,
};
} else {
session.type = 'wecom/message';
session.subtype = body.MsgType;
} }
session.type = 'message';
session.subtype = 'private';
session.author = {
userId: session.userId,
};
} }
return session; return session;
} }
......
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