Comments (5)
🤖
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
- 'UrlLink' only refers to a type, but is being used as a value here.
- src/wechaty/mod.ts
- src/wechaty/wechaty-base.ts
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.
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.
I’d love to accept a PR for fixing that.
from wechaty.
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.
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)
- 部署公司内网超时500ms HOT 1
- DEADLINE_EXCEEDED: Timeout after 5000 ms HOT 1
- js能正常跑起来,但是 wechaty 包依赖有警告 HOT 1
- wechaty启动报错:@grpc/grpc-js/src/subchannel-address.ts:40 return 'port' in address HOT 2
- 如何监听Friendship,添加好友发送成功 HOT 1
- 如何监听Friendship 根据手机号查询是空的微信号 HOT 3
- 微信如何回复图片或文件消息 HOT 4
- FileBox不存在 HOT 1
- 是否支持企业微信? HOT 3
- 是否可以在回复的消息中@某个人 HOT 5
- 从getting-started项目启动报Maximum number of redirects exceeded HOT 4
- wechaty不能监听到登录后(包括登录)的消息了 HOT 1
- 扫码登录之后直接微信闪退 HOT 1
- wechaty是不更新了吗 HOT 2
- 这两天机器人一直登录几分钟,立马掉线,这是什么问题 HOT 3
- UNAVAILABLE: Network closed for unknown reason HOT 1
- 如果自己想实现企业微信的流程,我需要如何去做 HOT 1
- 名片问题 HOT 3
- 自己部署wechaty HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wechaty.