How to use the wechaty.Wechaty.instance function in wechaty

To help you get started, we’ve selected a few wechaty examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wechaty / wechaty-getting-started / examples / basic / the-worlds-shortest-chatbot-code-in-6-lines.js View on Github external
*   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 { Wechaty } = require('wechaty')

Wechaty.instance() // Singleton
.on('scan',     (qrcode, status)  => console.log(`Scan QR Code to login: ${status}\nhttps://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(qrcode)}`))
.on('login',    user              => console.log(`User ${user} logined`))
.on('message',  message           => console.log(`Message: ${message}`))
.start()
github wechaty / wechaty / examples / the-worlds-shortest-chatbot-code-in-6-lines.js View on Github external
*   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 { Wechaty } = require('wechaty') // import Wechaty from 'wechaty'

Wechaty.instance() // Singleton
.on('scan',     (qrcode, status)  => console.log(`Scan QR Code to login: ${status}\nhttps://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(qrcode)}`))
.on('login',    user              => console.log(`User ${user} logined`))
.on('message',  message           => console.log(`Message: ${message}`))
.start()
github BingKui / WeChatRobot / robot.js View on Github external
const {
    groupMessage,
    groupJoinMessage,
    groupLeaveMessage,
    groupTopicChange,
} = require('./feature/group.js');

const { singleMessage } = require('./feature/single.js');

const { isAutoChangeAvatar } = require('./config/config.js');

// 引入数据库链接文件
require('./tools/mongo.js');

// 实例化机器人对象
const bot = Wechaty.instance();

// 全局对象,保存当前登录的用户
let userSelf = null;

// 监听扫码登录方法
bot.on('scan', (qrCode, statusCode) => {
    // 判断状态码
    if (!/201|200/.test(statusCode)) {
        QrcodeTerminal.generate(qrCode);
        console.log('扫描二维码登录~~~~');
    }
});

// 监听登录方法
bot.on('login', async (userContact) => {
    userSelf = await contactInfo(userContact);
github wechaty / wechaty / examples / hot-reload-bot / index.js View on Github external
*
 * @deprecated
 * Use hot-import-bot instead
 * See: https://github.com/Chatie/wechaty/tree/master/examples/hot-import-bot
 *
 * DEV: docker run -ti -e --rm --volume="$(pwd)":/bot zixia/wechaty index.js
 * PROD: docker run -ti -e NODE_ENV=production --rm --volume="$(pwd)":/bot zixia/wechaty index.js
 *
 * @author: Gcaufy
 */
const fs          = require('fs');
const path        = require('path');
const { Wechaty } = require('wechaty');

const isProd = process.env.NODE_ENV === 'production';
const bot = Wechaty.instance();

const EVENT_LIST = ['scan', 'logout', 'login', 'friend', 'room-join', 'room-leave', 'room-topic', 'message', 'heartbeat', 'error'];

// Load lisenter
const loadListener = (evt) => {
    let fn;
    try {
        fn = require(`./listener/${evt}`);
        console.log(`binded listener: ${evt}`);
    } catch (e) {
        fn = () => void 0;
        if (e.toString().indexOf('Cannot find module') > -1) {
            console.warn(`listener ${evt} is not defined.`);
        } else {
            console.error(e);
        }
github BingKui / WeChatRobot / feature / group.js View on Github external
// 群组聊天相关功能
const { Wechaty } = require('wechaty');
const { mentioned } = require('../tools/tools.js');
const { contactListInfo, contactInfo } = require('../tools/contactTools.js');
const dialogMessage = require('./dialog.js');
const { messageText, messageInfo } = require('../tools/messageTools.js');

const bot = Wechaty.instance();

/**
 * @description 回复群聊中的 @ 消息,并回 @ 回去
 * @param {Message} message 消息对象
 * @param {MessageInfo} info 消息内容对象
 * @param {ContactInfo} self 本身信息
 */
const groupMessage = async (message, info, self) => {
    // 自己发的消息,直接返回
    if (info.isSelf) {
        return;
    }
    // 如果 @ 到自己,在进行回复
    if (mentioned(info.mentionInfo, self.name)) {
        // 处理消息进行回复
        await dialogMessage(message);
github wechaty / wechaty / test / fixture / docker / js-bot.js View on Github external
const { Wechaty } = require('wechaty')

const bot = Wechaty.instance()                                                                                                               
console.log(bot.version())
github wechaty / wechaty / examples / monster / index.js View on Github external
/**
 * Based on the Wechaty hot import bot example
 *
 * Hot import Wechaty listenser functions after change the source code without restart the program
 *
 * P.S. We are using the hot-import module:
 *   * Hot Module Replacement(HMR) for Node.js
 *   * https://www.npmjs.com/package/hot-import
 *
 */

const finis = require('finis')
const { Wechaty } = require('wechaty')

const bot = Wechaty.instance({ profile: "default"})

async function main() {

  bot
    .on('scan',     './listeners/on-scan')
    .on('login',    './listeners/on-login')
    .on('message',  './listeners/on-message')
    .on('friend',   './listeners/on-friend')
    .start()
    .catch(async function(e) {
      console.log(`Init() fail: ${e}.`)
      await bot.stop()
      process.exit(1)
    })
}
github wechaty / wechaty-getting-started / examples / advanced / busy-bot.js View on Github external
if (token) {
  log.info('Wechaty', 'TOKEN: %s', token)

  bot = Wechaty.instance({ profile: token })
  const ioClient = new IoClient({
    token,
    wechaty: bot,
  })

  ioClient.start().catch(e => {
    log.error('Wechaty', 'IoClient.init() exception: %s', e)
    bot.emit('error', e)
  })
} else {
  log.verbose('Wechaty', 'TOKEN: N/A')
  bot = Wechaty.instance()
}

bot
.on('scan', (qrcode, status) => {
  qrTerm.generate(qrcode, { small: true })
  console.log(`${status}: ${qrcode} - Scan QR Code of the url to login:`)
})
.on('logout'	, user => log.info('Bot', `${user.name()} logouted`))
.on('error'   , e => log.info('Bot', 'error: %s', e))

.on('login', async function(user) {
  const msg = `${user.name()} logined`

  log.info('Bot', msg)
  await this.say(msg)