UNPKG

@juzi/wechaty-puppet-whatsapp

Version:
199 lines (197 loc) 6.24 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* eslint-disable promise/always-return */ /* eslint-disable sort-keys */ /* eslint-disable no-console */ /** * Wechaty - https://github.com/chatie/wechaty * * @copyright 2016-2018 Huan LI <zixia@zixia.net> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ const PUPPET = __importStar(require("@juzi/wechaty-puppet")); const qrcode_terminal_1 = __importDefault(require("qrcode-terminal")); const mod_js_1 = require("../src/mod.js"); const config_js_1 = require("../src/config.js"); /** * * 1. Declare your Bot! * */ const puppet = new mod_js_1.PuppetWhatsapp({ puppeteerOptions: { puppeteer: { headless: false, }, }, }); /** * * 2. Register event handlers for Bot * */ puppet .on('logout', onLogout) .on('login', onLogin) .on('scan', onScan) .on('error', onError) .on('message', onMessage) .on('ready', onReady); /** * * 3. Start the bot! * */ puppet.start() .then(() => { puppet.on('dirty', onDirty); }) .catch(async (e) => { console.error('Bot start() fail:', e); await puppet.stop(); process.exit(-1); }); /** * * 4. You are all set. ;-] * */ /** * * 5. Define Event Handler Functions for: * `scan`, `login`, `logout`, `error`, and `message` * */ function onScan(payload) { if (payload.qrcode) { qrcode_terminal_1.default.generate(payload.qrcode, { small: true }); const qrcodeImageUrl = [ 'https://wechaty.js.org/qrcode/', payload.qrcode, ].join(''); console.info(`[${payload.status}] ${qrcodeImageUrl}\nScan QR Code above to log in: `); } else { console.info(`[${payload.status}]`); } } function onLogin(payload) { console.info(`${payload.contactId} login`); } function onLogout(payload) { console.info(`${payload.contactId} logouted`); } function onError(payload) { console.error('Bot error:', payload.data); /* if (bot.logonoff()) { bot.say('Wechaty error: ' + e.message).catch(console.error) } */ } /** * * 6. The most important handler is for: * dealing with Messages. * */ async function onMessage(payload) { const msgPayload = await puppet.messagePayload(payload.messageId); // eslint-disable-next-line no-console console.log(msgPayload); if (msgPayload.type === 8) { console.log(await puppet.messageLocation(msgPayload.id)); } if ((/ding/i.test(msgPayload.text || ''))) { await puppet.messageSendText(msgPayload.talkerId, 'dong'); // await puppet.messageSendLocation(msgPayload.talkerId!, { // latitude: -37.8773906, // longitude: 145.0449860, // name: 'Monash University', // accuracy: 15, // address: 'Melbourne Victoria Australia', // }) } if (msgPayload.text?.startsWith('http') && msgPayload.roomId) { await puppet.roomAvatar(msgPayload.roomId, config_js_1.FileBox.fromUrl(msgPayload.text)); } } async function onDirty(payload) { console.log(`onDirty(${JSON.stringify(payload)})`); if (payload.payloadType === PUPPET.types.Dirty.Contact) { const contactId = payload.payloadId; const contact = await puppet.contactPayload(contactId); console.log('updated contact: ', JSON.stringify(contact)); } if (payload.payloadType === PUPPET.types.Dirty.Message) { const messageId = payload.payloadId; const message = await puppet.messagePayload(messageId); console.log('updated message: ', JSON.stringify(message)); } } async function onReady() { console.log('onReady()'); } /** * * 7. Output the Welcome Message * */ const welcome = ` Puppet Version: ${puppet.version()} Please wait... I'm trying to login in... `; console.info(welcome); process.on('unhandledRejection', (reason, promise) => { console.error('Config', '###########################'); console.error('Config', `unhandledRejection: ${reason} ${promise}`); console.error('Config', '###########################'); promise.catch(err => { const message = err.message; console.error('Config', `process.on(unhandledRejection) promise.catch(${message})`); }); }); process.on('uncaughtException', e => { console.error('Config', '###########################'); console.error('Config', `uncaughtException: ${e.stack}`); console.error('Config', '###########################'); }); //# sourceMappingURL=ding-dong-bot.js.map