Coder Social home page Coder Social logo

Comments (5)

dosubot avatar dosubot commented on June 2, 2024 3

🤖

Hey @liuhq! Great to see you back here. Hope you've been doing well!

I see that you're having trouble instantiating Wechaty in your TypeScript code. This is because Wechaty is being exported as a type, not as a class or function. In TypeScript, types are used for static type checking and do not exist at runtime, hence they cannot be instantiated.

If you want to create an instance of 'Wechaty', you should be using WechatyImpl or WechatySkeleton, which are exported as classes. Here's how you can instantiate WechatyImpl or WechatySkeleton:

import { WechatyImpl, WechatySkeleton } from 'src/wechaty/mod.ts';

const wechatyImpl = new WechatyImpl();
const wechatySkeleton = new WechatySkeleton();

Please note that you might need to pass some arguments to the constructor depending on the implementation of WechatyImpl and WechatySkeleton. You can refer to the source code here for more details.

However, the recommended way to instantiate Wechaty in your code is by using WechatyBuilder.build(). Here's an example:

import { WechatyBuilder } from 'wechaty'

const bot = WechatyBuilder.build()

bot.on('scan',    (qrCode, status) => console.log('https://wechaty.js.org/qrcode/' + encodeURIComponent(qrcode)))
bot.on('login',   user => console.log(`User ${user} logged in`))
bot.on('message', message => console.log(`Message: ${message}`))

bot.start()

In this example, WechatyBuilder.build() is used to create a new instance of Wechaty. Then, several event listeners are attached to the bot: 'scan', 'login', and 'message'. Finally, bot.start() is called to start the bot. You can refer to the source code here for more details.

I hope this helps! If you have any other questions, feel free to ask.

Sources

About Dosu

This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

from wechaty.

Zeng95 avatar Zeng95 commented on June 2, 2024 1

The building the bot section of the beginner-friendly tutorial needs updates.

The recommended way to instantiate Wechaty is by using WechatyBuilder.build().

import { WechatyBuilder } from 'wechaty'

// Initializing bot
const bot = WechatyBuilder.build();

// Handle events of bot
bot.on('scan', onScan);
bot.on('login', onLogin);
bot.on('logout', onLogout);
bot.on('message', onMessage);

// Start bot
bot.start();

from wechaty.

huan avatar huan commented on June 2, 2024

I’d love to accept a PR for fixing that.

from wechaty.

liuhq avatar liuhq commented on June 2, 2024

The building the bot section of the beginner-friendly tutorial needs updates.

The recommended way to instantiate Wechaty is by using WechatyBuilder.build().

import { WechatyBuilder } from 'wechaty'

// Initializing bot
const bot = WechatyBuilder.build();

// Handle events of bot
bot.on('scan', onScan);
bot.on('login', onLogin);
bot.on('logout', onLogout);
bot.on('message', onMessage);

// Start bot
bot.start();

Thx! It works fine.

from wechaty.

huan avatar huan commented on June 2, 2024

BTW: this kind of problems could be very easy to be solved by talking with our Friday BOT @ GPTs - https://chat.openai.com/g/g-s4mx1ODQy-friday-bot

Please check it out if you have a ChatGPT Plus subscription.

Or, welcome to join our Discord community https://discord.gg/7q8NBZbQzt and you can talk with Friday BOT over there as well.

from wechaty.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.