New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wechaty handling in plain ES6 classes sample #1066

Closed
suntong opened this Issue Jan 23, 2018 · 10 comments

Comments

Projects
None yet
3 participants
@suntong
Copy link
Contributor

suntong commented Jan 23, 2018

Just a simple noob request,

Can someone provide me with a simple example that handles wechaty in plain ES6 classes please?

The reason I'm asking is that,

  • I understand that wechaty was initially developed long before ES6 is mature enough to be used, however,
    • Looking at the ES6 support in Node.js, I can see really really few ES6 features are unsupported now. I.e., only very few extreme end-cases are left.
    • moreover, I don't know how well the transpiling is, e.g., for the advanced ES6 features like map, filter and reduce, I don't know how efficient the transpiled code is.
    • Starting with version 8.5.0, Node.js supports ES modules natively, so we can do import {add} from './lib.mjs'; directly in ES6 now.
    • I.e., the ES6 is ready for prime time use, writing in TypeScript then have it transpiled into ES6 code seems like a redundant step now. (I'm not saying that TypeScript is not helpful, it is still good in big projects that you have some dummies really easily to shoot their own feet)
  • I'm a long time programmer but never did JavaScript programming before, so it'll be a challenge for me to put all these puzzles together, Node.js, ES6 class that handles wechaty the OO way, etc, etc.
  • Just FTA, I'm planning to contribute my code that logs weixin charts as html files back to the wechaty community, so your help is not in vain. I've long been planning write a tool to do that, however, the platform that I initially chose was so unstable that I have to look for something else, and start a brand new journey to learn and use JavaScript/TypeScript.

Let me know if you could help, so that I can elaborate more on what I have in mind (but still should be very simple for JavaScript programmers).

Thanks for helping!

@ubinliu

This comment has been minimized.

Copy link

ubinliu commented Jan 25, 2018

the same to me, help

@huan

This comment has been minimized.

Copy link
Member

huan commented Jan 25, 2018

Hi @suntong, Thanks for the detail description of your requirement.

TL;DR

The official example code in README is in plain ES6, which you can find it at https://github.com/Chatie/wechaty#the-worlds-shortest-chatbot-code-6-lines-of-javascript

About the relation between ES6 and TypeScript

We are writing Wechaty in ES6 with TypeScript support because the TypeScript is a superset of ES6.

You can know more about TypeScript at https://channel9.msdn.com/Events/Build/2017/B8088/ , which is a fantastic introduction video for TypeScript, it's also my favorite.

Please let me know if you still have any questions, and I'll appreciate if you can make any contribution to the community.

Thank you very much!

@suntong

This comment has been minimized.

Copy link
Contributor Author

suntong commented Jan 25, 2018

thanks for the reply @zixia, look like most of wechaty examples are indeed written in plain ES6, so looks to me they should be able to run even without the TypeScript transpiling!

So, all my above suggestions still hold, i.e.,

  • writing in TypeScript then have it transpiled into ES6 code seems like a redundant step now.
  • moreover, most of wechaty examples are in fact plain ES6, not TypeScript (because of missing type declaration for every variable, which is the # 1 feature that TypeScript boasts with), so making such extra transpiling step seems even more unnecessary to me.

Further more, even though the-worlds-shortest-chatbot-code-6-lines-of-javascript is in plain ES6, however it is named as .ts file so it is treated as such. There are still lots of logistics issues that I don't know for sure, since I never use ES6/javascript/TypeScript before. E.g.,

These are the only things that I've read/known, and I presume there will be many many logistics issues to overcome to get it working, especially for me, who has never used ES6/javascript/TypeScript before.

So would you make it works and put the steps into a wiki please @zixia?

thanks a lot!

@suntong

This comment has been minimized.

Copy link
Contributor Author

suntong commented Jan 25, 2018

If you do, @zixia or anybody, would you start with the code from

https://blog.chatie.io/guide/2017/01/01/getting-started-wechaty.html

instead of the the-worlds-shortest-chatbot-code-6-lines-of-javascript please?

Moreover, please define two ES6 objects, wxMessage and wxRoom, whose member function will be taking care of the message and room handling. E.g., instead of

.on('message', async function(m){
    const contact = m.from()
    const content = m.content()
    ...

that async function(m) need to be a member function of a wxMessage class. How to declare such object and instantiate it, how to call it, etc, etc, I still have no idea.

and can I ask for one more thing? For my task of logging weixin charts as html files, I am only logging rooms that contain a special regexp, which will not be hard-coded, but read from some kind of config file.
how to do that in ES6, I've done a lot of reading and am still not sure what's the best way to proceed. So I appreciate that you can demo how to read config file to set wxMessage member data.

If you can also throw in a line or two to deal with #1067, that'd be fantastic. Then I can port my "3 8 24 game" over next.

@suntong

This comment has been minimized.

Copy link
Contributor Author

suntong commented Feb 6, 2018

Hi @ALL,

To recap the whole thread in one sentence:

Writing in TypeScript then have it transpiled into ES6 code is a redundant step now. Moreover, most of wechaty examples are in fact plain ES6, not TypeScript (because of missing type declaration for every variable, which is the # 1 feature that TypeScript boasts with), so making such extra transpiling step is even more unnecessary.

So I've squirreled a wechaty customization module starting point with the following feature:

  • In plain JavaScript language, no need the superficial transpiling step.
  • the message and room handling are in separated scripts.
  • can read from a config file
  • can do rooms/messages matching using regexp that are defined in the config file
  • can do hot reload, when the .js script(s) updated
  • able to send notifications or alert by email

As I've never done any JavaScript programming before, this is the best I can do so far. So I'm closing the issue for now. Please check out my code here.

As for my task of logging weixin charts as html files, I'll be updating the above code continuously. Please monitor the progress over there if anyone is interested, as I'm not going to make any further announcements here.

cheers

@suntong suntong closed this Feb 6, 2018

@huan

This comment has been minimized.

Copy link
Member

huan commented Feb 7, 2018

Hello @suntong,

Thank you for sharing your opinions with the community and keep thinking about it.

The following is my two cents:

  1. I never transpile TypeScript into ES6 by hand. I use ts-node instead of node to run TS programs directly (except when I'm publishing code to NPM). See: https://www.npmjs.com/package/ts-node
  2. We have already put the message and room handers into separated scripts, with Hot Module Reload supported. See:
    1. For TypeScript: https://github.com/Chatie/wechaty/pull/935/files
    2. For JavaScript: https://github.com/Chatie/wechaty/tree/master/example/hot-reload-bot and
    3. Blog Article: http://blog.chatie.io/developer/2017/03/20/added-hot-reload-for-bots.html

I hope that information will be helpful for you.

Good luck!

@huan huan added the question label Feb 7, 2018

@suntong

This comment has been minimized.

Copy link
Contributor Author

suntong commented Feb 7, 2018

Thanks @zixia,

  • I know that transpiling happens automatically, and I believe it is even true with ts-node. I.e., although no manual transpiling is happening, that extra superficial step is still taking place.
  • I also know that wechaty has many good and useful modules, like Hot Module Reload, etc. However, for a newbie like me, the most helpful thing is not the 6-line code, but a comprehensive one that include all useful features, because newbie like me can't put them together. (PS, just added the "able to send notifications or alert by email" to my above list)

Thanks for answering any way.

@suntong

This comment has been minimized.

Copy link
Contributor Author

suntong commented Feb 10, 2018

did some good reading just now and want to share it back in this thread,

When are you better off without Typescript? --
When you can’t afford an extra transpilation tax

There are no plans to support TypeScript natively in the browsers. Chrome did some experiment, but later cancelled support. I suspect this has something to do with unnecessary runtime overhead.
If someone wants training wheels, they can install them. But bikes shouldn’t come with permanent training wheels. This means that you will always have to transpile your TypeScript code before running them.
For standard ES6, it’s a whole different story. When ES6 is supported by most browsers, the current ES6 to ES5 transpilation will become unnecessary.
ES6 is the biggest change to the JavaScript language, and I believe most programmers will just settle with it. But those brave few who want to try the next version of JavaScript’s experimental features, or the features not yet implemented on all browsers — they will need to transpile anyway.

The philosophy that I resist anything that MS proposes has a long history of me, as a programmer, who was forced onto the MS band wagon for their shinny new toys, then thrown under the bus when MS abandon them. Not many programmers start the love-hate affair with MS since DOS2.0, through DOS3.0 all they way to DOS5.0. Not many programmers have ever heard of OLE, DDE, DAO, ADO, and the stories behind their rise and fall, yet I was the one who bite the bullet and gone through them all. Even today, I'm still living through the consequences of MS abandoning silverlight, for all these past several years.

Once again,

The old Microsoft used to take standard tools — Java for example — and add proprietary non-standard features to them — in this case resulting in J++. Then they would try to force developers to choose between the two.
TypeScript is exactly the same approach — this time for JavaScript. By the way, this isn’t Microsoft’s first fork of JavaScript. In 1996, they forked JavaScript to create JScript.

So, all in all, thank but no thanks to MS' JScript, or TypeScript, I'll stick to ES6 instead.

Sorry for the ranting. I know TypeScript does has its place, and the first part of the above article list many of them. After all, it's my personal choice.

@huan

This comment has been minimized.

Copy link
Member

huan commented Feb 10, 2018

Yes, I had the same feeling as you at the first beginning when I saw TypeScript because I did not like Microsoft too...

However, after some study, I finally fall in love with TypeScript and I have a strong belief so did you.

Before you keep hating it any more, I'd like to suggest you have a look at this talk:
https://channel9.msdn.com/Events/Build/2017/B8088/

Anders Hejlsberg will tell you why TypeScript is really amazing and it will help us a lot.

@suntong

This comment has been minimized.

Copy link
Contributor Author

suntong commented Feb 10, 2018

;-) hehe, they made it a hour-long video without giving any highlight/summary, :-), were they afraid that the highlights would be just so-so, or it is the way of their forceful thinking that made such decision? Haha, I'm just joking with you. quoting a sentence I like from the above article,

TypeScript haters are gonna hate, either because of fear of change or because they know somebody who knows somebody who is afraid of it. Life goes on and TypeScript introduces new features to its community anyway.

that's totally about me, :-), following the Don’t Transpile JavaScript for Node.js advice, :-).

huan added a commit that referenced this issue Mar 13, 2018

hot-reload-bot adding hot-reloading config close #1066, close #1100 (#…
…1123)

* - [+] duplicate hot-import-bot as monster

* - [*] more logical order

* - [*] enable resume connection

* - [+] add from  media-file-bot.ts demo

* - [+] add emoji cleansing

* - [+] add error & signal handling

* - [+] add configuration files hot-import

* - [*] update README

* - [*] fix for PR, step 1

* - [*] fix for PR, step 2: on-message.js

* - [*] fix for PR, step 3, update README & rename run-monster-bot.sh

* - [+] add "Run" section to descript how to start the demo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment