import { Dingtalk } from '@zhengxs/dingtalk'; import { AuthCredential } from '@zhengxs/dingtalk-auth'; import { HubConnection } from '@zhengxs/dingtalk-event-hubs'; import { FileBoxInterface } from 'file-box'; import { default as Keyv } from 'keyv'; import { DTContactRawPayload, DTMessageRawPayload, DTRoomRawPayload, DTSessionWebhookRawPayload, MessagePayload } from './dingtalk'; import * as PUPPET from 'wechaty-puppet'; export type GetMediaDurationInSeconds = (fileBox: FileBoxInterface) => Promise; export interface PuppetDingTalkOptions extends PUPPET.PuppetOptions { /** * 钉钉应用ID * * 和 `credential` 二选一 */ clientId?: string; /** * 钉钉应用密钥 * * 和 `credential` 二选一 */ clientSecret?: string; /** * 钉钉凭证 * * 和 `clientId`、`clientSecret` 二选一 */ credential?: AuthCredential; /** * 群数据缓存 */ roomsStore?: Keyv; /** * 联系人数据缓存 */ contactsStore?: Keyv; /** * 消息数据缓存 */ messagesStore?: Keyv; /** * 会话 Webhook 数据缓存 */ sessionWebhooksStore?: Keyv; /** * 登出后是否清理缓存 * * 默认为 `true` */ clearCacheAfterLogout?: boolean; /** * 获取音频时长 * * 如果是视频或音频,需要手动传递时长,而且是强制要求传递时长的。 * 但调用钉钉的上传接口,钉钉并没有返回时长,所以需要手动获取时长。 * * 参考:[机器人发送消息的类型](https://open.dingtalk.com/document/orgapp/types-of-messages-sent-by-robots) * * @example 修复音频时长显示错误 * * ```ts * import os from 'node:os'; * import path from 'node:path'; * import { unlinkSync } from 'node:fs'; * * import { PuppetDingTalk } from '@zhengxs/wechaty-puppet-dingtalk' * import { getAudioDurationInSeconds } from 'get-audio-duration' * * const puppet = new PuppetDingTalk({ * async getAudioDurationInSeconds(fileBox) { * // 生成临时文件地址 * const saveTo = path.resolve(os.tmpdir(), fileBox.name) * * // 保存到临时文件 * await fileBox.toFile(saveTo) * * try { * // 获取时长 * return await getAudioDurationInSeconds(saveTo) * } finally { * // 删除临时文件 * unlinkSync(saveTo) * } * }, * }) * ``` */ getAudioDurationInSeconds?: GetMediaDurationInSeconds; /** * 获取视频时长 * * 如果是视频或音频,需要手动传递时长,而且是强制要求传递时长的。 * 但调用钉钉的上传接口,钉钉并没有返回时长,所以需要手动获取时长。 * * 参考:[机器人发送消息的类型](https://open.dingtalk.com/document/orgapp/types-of-messages-sent-by-robots) * * @example 修复视频时长显示错误 * * ```ts * import os from 'node:os'; * import path from 'node:path'; * import { unlinkSync } from 'node:fs'; * * import { PuppetDingTalk } from '@zhengxs/wechaty-puppet-dingtalk' * import { getVideoDurationInSeconds } from 'get-video-duration' * * const puppet = new PuppetDingTalk({ * async getVideoDurationInSeconds(fileBox) { * return await getVideoDurationInSeconds(await fileBox.toStream()) * }, * }) * ``` */ getVideoDurationInSeconds?: GetMediaDurationInSeconds; } export declare class PuppetDingTalk extends PUPPET.Puppet { protected _dkClient: Dingtalk; protected _dkCredential: AuthCredential; protected _dkConnection?: HubConnection; protected _roomsStore: Keyv; protected _contactsStore: Keyv; protected _messagesStore: Keyv; protected _sessionWebhooksStore: Keyv; protected _clearCacheAfterLogout: boolean; protected _getVideoDurationInSeconds: GetMediaDurationInSeconds; protected _getAudioDurationInSeconds: GetMediaDurationInSeconds; constructor(options?: PuppetDingTalkOptions); roomRawPayload(roomId: string): Promise; roomRawPayloadParser(rawPayload: DTRoomRawPayload): Promise; roomMemberList(roomId: string): Promise; roomMemberRawPayload(roomId: string, contactId: string): Promise; roomMemberRawPayloadParser(rawPayload: DTContactRawPayload): Promise; contactRawPayload(contactId: string): Promise; contactRawPayloadParser(rawPayload: DTContactRawPayload): Promise; messageRawPayload(messageId: string): Promise; messageRawPayloadParser(rawPayload: DTMessageRawPayload): Promise; messageSendText(conversationId: string, // TODO 群或联系人ID? content: string, mentionIdList?: string[]): Promise; messageSendUrl(conversationId: string, urlLinkPayload: PUPPET.payloads.UrlLink | MessagePayload): Promise; messageSendFile(conversationId: string, fileBox: FileBoxInterface): Promise; messageImage(messageId: string, imageType: PUPPET.types.Image): Promise; messageFile(messageId: string): Promise; onStart(): Promise; onStop(): Promise; /** * 发送互动卡片 * * See https://open.dingtalk.com/document/orgapp/private-chat-coolapp-develop-interactive-cards */ private unstable__messageSendInteractiveCard; private unstable__say; private unstable__discoverWebhookSession; private unstable_downloadFile; protected unstable_defaultGetAudioDurationInSeconds: GetMediaDurationInSeconds; protected unstable_defaultGetVideoDurationInSeconds: GetMediaDurationInSeconds; }