Skip to content
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

New Plugin: Message Awaiter #13

Closed
ssine opened this issue Jun 21, 2020 · 4 comments
Closed

New Plugin: Message Awaiter #13

ssine opened this issue Jun 21, 2020 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@ssine
Copy link
Contributor

ssine commented Jun 21, 2020

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
Copy link
Contributor Author

ssine commented Jun 21, 2020

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
Copy link
Member

huan commented Jun 22, 2020

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
Copy link
Contributor Author

ssine commented Jun 23, 2020

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 :)

@huan huan added the enhancement New feature or request label Jun 23, 2020
@ssine
Copy link
Contributor Author

ssine commented Jun 23, 2020

close this as PR has merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants