wechaty / plugin-contrib

Wechaty Plugin Ecosystem Contrib Package
https://paka.dev/npm/wechaty-plugin-contrib
Apache License 2.0
33 stars 12 forks source link

New Plugin: Message Awaiter #13

Closed ssine closed 4 years ago

ssine commented 4 years ago

Plugin Name

Message Awaiter

Description

During my experience of developinng bots (telegram, coolq, and newly on wechaty), I find that all the frameworks provide a callback mechanism when message is received. However, this clear and simple api is indirect when developing dialogs with multiple message sending and receiving.

For example, I have a bot that asks what I've done and record it everyday. This happens every 11pm:

1. bot: what have you done today?
2. me : a/b/c
3. bot: give yourself a score?
4. me : 30
5. bot: events recorded.

so after message 1, 3 are sent, the bot begin to wait for my reply. Such dialog pattern is common and intuitive, but is indirect to implement in EventEmitter's api. To do this, we can make a state machine for each dialog, or pass subsequent logic into callback function, but neither of them matches the simplicity of await bot.newMessageFrom(...).

This plugin returns a Promise object that resolves when a new message that satisfies some condition (from whom, text pattern matching, timing) is received (and manages the lifecycle of the listener function), so that the dialog logic in code can continue uninterrupted. The code pattern would look like this:

async function dialog() {
  await bot.say(...)
  let reply = await bot.waitFor(...)
  // do something with reply
  await bot.say(...)
  reply = await bot.waitFor(...)
  // the pattern repeats or ends
}
ssine commented 4 years ago

This is mainly what I have done making bots for telegram / qq, I'm not sure whether wechaty have more elegant way to do this, sorry if I'm trying to solve an non-exist problem.

huan commented 4 years ago

Hi @ssine , thank you very much for explaining your idea in detail, and I fully agree with you that we definitely need a dialog helper function (MessageAwaiter in our case) for helping the chatbot developers to make the multi-turn dialog to be easily processed.

This week I'm also thinking about another way to deal with this problem, like the prompt() command in the CLI program. Maybe we also need a ReadStream for conversation sentence input and a WriteStream for conversation sentence output. Still thinking in the process.

I have left my reviews on your PR, please follow them and do not hesitate to ask me if you have any questions.

We will be able to merge it after you switch your related code to use the matchers helper functions.

Looking forward to use your MessageAwaiter plugin, cheers!

ssine commented 4 years ago

Glad to know that the idea is valuable. Regarding dialog as two streams feels like a higher level of abstraction and would be very useful.

I have updated the PR, thanks :)

ssine commented 4 years ago

close this as PR has merged