@juzi/wechaty
Version:
Wechaty is a RPA SDK for Chatbot Makers.
163 lines • 6.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContactSelfImpl = void 0;
const wechaty_puppet_1 = require("@juzi/wechaty-puppet");
const guard_qr_code_value_js_1 = require("../pure-functions/guard-qr-code-value.js");
const contact_js_1 = require("./contact.js");
const validation_js_1 = require("../user-mixins/validation.js");
const poolify_js_1 = require("../user-mixins/poolify.js");
const MixinBase = (0, poolify_js_1.poolifyMixin)(contact_js_1.ContactImpl)();
/**
* Bot itself will be encapsulated as a ContactSelf.
*
* > Tips: this class is extends Contact
* @example
* const bot = new Wechaty()
* await bot.start()
* bot.on('login', (user: ContactSelf) => {
* console.log(`user ${user} login`)
* })
*/
class ContactSelfMixin extends MixinBase {
static async find(query) {
if (!this.wechaty.isLoggedIn) {
return undefined;
}
try {
const contact = await super.find(query);
if (contact && contact.id === this.wechaty.puppet.currentUserId) {
return contact;
}
}
catch (e) {
wechaty_puppet_1.log.silly('ContactSelf', 'find() exception: %s', e.message);
}
return undefined;
}
/**
* GET / SET bot avatar
*
* @param {FileBox} [file]
* @returns {(Promise<void | FileBox>)}
*
* @example <caption> GET the avatar for bot, return {Promise<FileBox>}</caption>
* // Save avatar to local file like `1-name.jpg`
*
* bot.on('login', (user: ContactSelf) => {
* console.log(`user ${user} login`)
* const file = await user.avatar()
* const name = file.name
* await file.toFile(name, true)
* console.log(`Save bot avatar: ${contact.name()} with avatar file: ${name}`)
* })
*
* @example <caption>SET the avatar for a bot</caption>
* import { FileBox } from 'wechaty'
* bot.on('login', (user: ContactSelf) => {
* console.log(`user ${user} login`)
* const fileBox = FileBox.fromUrl('https://wechaty.github.io/wechaty/images/bot-qr-code.png')
* await user.avatar(fileBox)
* console.log(`Change bot avatar successfully!`)
* })
*
*/
async avatar(file) {
wechaty_puppet_1.log.verbose('Contact', 'avatar(%s)', file ? file.name : '');
if (!file) {
const filebox = await super.avatar();
return filebox;
}
if (this.id !== this.wechaty.puppet.currentUserId) {
throw new Error('set avatar only available for user self');
}
await this.wechaty.puppet.contactAvatar(this.id, file);
}
/**
* Get bot qrcode
*
* @returns {Promise<string>}
*
* @example
* import { generate } from 'qrcode-terminal'
* bot.on('login', (user: ContactSelf) => {
* console.log(`user ${user} login`)
* const qrcode = await user.qrcode()
* console.log(`Following is the bot qrcode!`)
* generate(qrcode, { small: true })
* })
*/
async qrcode() {
wechaty_puppet_1.log.verbose('Contact', 'qrcode()');
if (this.id !== this.wechaty.puppet.currentUserId) {
throw new Error('only can get qrcode for the currentUser');
}
const qrcodeValue = await this.wechaty.puppet.contactSelfQRCode();
return (0, guard_qr_code_value_js_1.guardQrCodeValue)(qrcodeValue);
}
name(name) {
wechaty_puppet_1.log.verbose('ContactSelf', 'name(%s)', name || '');
if (typeof name === 'undefined') {
return super.name();
}
if (this.id !== this.wechaty.puppet.currentUserId) {
throw new Error('only can set name for user self');
}
return this.wechaty.puppet.contactSelfName(name).then(this.sync.bind(this));
}
realName(realName) {
wechaty_puppet_1.log.verbose('ContactSelf', 'realName(%s)', realName || '');
if (typeof realName === 'undefined') {
return super.realName();
}
if (this.id !== this.wechaty.puppet.currentUserId) {
throw new Error('only can set realName for user self');
}
return this.wechaty.puppet.contactSelfRealName(realName).then(this.sync.bind(this));
}
aka(aka) {
wechaty_puppet_1.log.verbose('ContactSelf', 'aka(%s)', aka || '');
if (typeof aka === 'undefined') {
return super.aka();
}
if (this.id !== this.wechaty.puppet.currentUserId) {
throw new Error('only can set aka for user self');
}
return this.wechaty.puppet.contactSelfAka(aka).then(this.sync.bind(this));
}
/**
* Change bot signature
*
* @param signature The new signature that the bot will change to
*
* @example
* bot.on('login', async user => {
* console.log(`user ${user} login`)
* try {
* await user.signature(`Signature changed by wechaty on ${new Date()}`)
* } catch (e) {
* console.error('change signature failed', e)
* }
* })
*/
async signature(signature) {
wechaty_puppet_1.log.verbose('ContactSelf', 'signature()');
if (this.id !== this.wechaty.puppet.currentUserId) {
throw new Error('only can change signature for user self');
}
return this.wechaty.puppet.contactSelfSignature(signature).then(this.sync.bind(this));
}
async roomAlias(room, alias) {
wechaty_puppet_1.log.verbose('ContactSelf', 'roomAlias()');
if (typeof alias === 'undefined') {
return room.alias(this);
}
if (!(await room.has(this))) {
throw new Error('cannot edit room alias because you are not in room');
}
return this.wechaty.puppet.contactSelfRoomAlias(room.id, alias);
}
}
class ContactSelfImpl extends (0, validation_js_1.validationMixin)(ContactSelfMixin)() {
}
exports.ContactSelfImpl = ContactSelfImpl;
//# sourceMappingURL=contact-self.js.map