Commit a30967cc authored by nanahira's avatar nanahira

add full type support

parent eb84676d
......@@ -26,12 +26,10 @@ app.plugin(adapterPlugin, {
encodingAESKey: 'encodingAESKey', // 加密密钥,在应用消息接收设置中设置并填入
});
app.on('wecom/event', (session) => {
console.log(`Got event ${JSON.stringify(session.wecom)}`);
});
app.on('wecom/message', (session) => {
console.log(`Got custom message ${JSON.stringify(session.wecom)}`);
app.on('wecom/LOCATION', (session) => {
console.log(
`Got location for ${session.userId}: ${session.wecom.Longitude} ${session.wecom.Latitude}`,
);
});
app.on('message', (session) => {
......
......@@ -37,7 +37,6 @@ export interface WecomEventBody {
Event?: string;
AgentID: number;
MsgId?: number;
EventKey?: string;
}
export interface WecomChatBody extends WecomEventBody {
......@@ -74,31 +73,6 @@ export interface WecomLinkBody extends WecomEventBody {
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 {
access_token: string;
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 './WeComUser';
export * from './WeComAgentInfo';
export * from './specific/WeComUser';
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 { WecomEventBody, WecomEventResponse } from './def';
import { Adapter, Session } from 'koishi';
import {
ApprovalInfo,
CardEvent,
CardEventSelectedItems,
ChangeContact,
EventKeyBody,
PhotoEventBody,
ScanCodeEventBody,
WecomEventBody,
WecomEventFunction,
} from './def';
import { Adapter } from 'koishi';
import HttpServer from './http';
import { BotConfig, WeComBot } from './bot';
import { AdapterConfig } from './utils';
......@@ -10,8 +20,52 @@ declare module 'koishi' {
wecom?: WecomEventBody;
}
interface EventMap {
'wecom/event'(session: Session): void;
'wecom/message'(session: Session): void;
'wecom/enter_agent': WecomEventFunction;
'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 {
WecomChatBody,
WecomEventResponse,
WecomLinkBody,
WecomLocationBody,
WecomLocationMessageBody,
WecomPicBody,
WecomResponse,
WeComUser,
WecomVideoBody,
WecomVoiceBody,
......@@ -55,10 +53,8 @@ export function adaptSession(bot: WeComBot, input: WecomEventResponse) {
timestamp: body.CreateTime,
};
if (body.MsgType === 'event') {
session.type = 'wecom/event';
session.subtype = body.Event;
session.type = `wecom/${body.Event}`;
} else {
let isKnownMessage = true;
switch (body.MsgType) {
case 'text':
const textBody = body as WecomChatBody;
......@@ -98,18 +94,13 @@ export function adaptSession(bot: WeComBot, input: WecomEventResponse) {
});
break;
default:
isKnownMessage = false;
}
if (isKnownMessage) {
session.type = 'message';
session.subtype = 'private';
session.author = {
userId: session.userId,
};
} else {
session.type = 'wecom/message';
session.subtype = body.MsgType;
return;
}
session.type = 'message';
session.subtype = 'private';
session.author = {
userId: session.userId,
};
}
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