@juzi/wechaty
Version:
Wechaty is a RPA SDK for Chatbot Makers.
160 lines • 5.69 kB
JavaScript
import { log, } from '@juzi/wechaty-puppet';
import { guardQrCodeValue, } from '../pure-functions/guard-qr-code-value.js';
import { ContactImpl, } from './contact.js';
import { validationMixin } from '../user-mixins/validation.js';
import { poolifyMixin } from '../user-mixins/poolify.js';
const MixinBase = poolifyMixin(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) {
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) {
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() {
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 guardQrCodeValue(qrcodeValue);
}
name(name) {
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) {
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) {
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) {
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) {
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 validationMixin(ContactSelfMixin)() {
}
export { ContactSelfImpl, };
//# sourceMappingURL=contact-self.js.map