-
Notifications
You must be signed in to change notification settings - Fork 11
Add MessageAwaiter contrib #12
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
Changes from 3 commits
c6a96ad
81ae564
5c77e19
31575bf
c470d84
d5b2a67
cf4b1f1
ebfd201
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /** | ||
| * Wechaty - https://github.com/wechaty/wechaty | ||
| * | ||
| * @copyright 2016-now Huan LI <[email protected]> | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
| import { Wechaty } from 'wechaty' | ||
|
|
||
| import { | ||
| DingDong, | ||
| EventLogger, | ||
| QRCodeTerminal, | ||
| MessageAwaiter, | ||
| } from '../src' // from 'wechaty-plugin-contrib' | ||
|
|
||
| const bot = new Wechaty({ | ||
| name: 'message-awaiter-bot', | ||
| }) | ||
|
|
||
| bot.use( | ||
| QRCodeTerminal(), | ||
| DingDong(), | ||
| EventLogger(), | ||
| MessageAwaiter() | ||
| ) | ||
|
|
||
| bot.on('message', async (msg) => { | ||
| if (msg.text() === 'repeat me') { | ||
|
|
||
| await msg.say('what to repeat?') | ||
| let repeatMsg = await bot.waitForMessage({ contactId: msg.from()?.id, roomId: msg.room()?.id }) | ||
| await repeatMsg.say(repeatMsg.text()) | ||
|
|
||
| } else if (msg.text() === 'test') { | ||
|
|
||
| await msg.say('please reply a message with digits in a minute') | ||
| try { | ||
| let repeatMsg = await bot.waitForMessage({ | ||
| contactId: msg.from()?.id, | ||
| regex: /\d/, | ||
| roomId: msg.room()?.id, | ||
| timeoutSecond: 60, | ||
| }) | ||
| await repeatMsg.say(repeatMsg.text()) | ||
| } catch (err) { | ||
| await msg.say(String(err)) | ||
| } | ||
|
|
||
| } | ||
| }) | ||
|
|
||
| bot.start() | ||
| .catch(console.error) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /** | ||
| * Author: Siyao Liu https://github.com/ssine | ||
| * Date: June 2020 | ||
| */ | ||
| import { | ||
| Wechaty, | ||
| WechatyPlugin, | ||
| Message, | ||
| log, | ||
| } from 'wechaty' | ||
|
|
||
| type MessageAwaiterArgs = { | ||
| contactId?: string, | ||
| roomId?: string, | ||
| regex?: RegExp, | ||
| timeoutSecond?: number | ||
| } | ||
|
|
||
| declare module 'wechaty' { | ||
| class Wechaty { | ||
|
|
||
| waitForMessage(args: MessageAwaiterArgs): Promise<Message> | ||
|
|
||
| } | ||
| } | ||
|
|
||
| export function MessageAwaiter (): WechatyPlugin { | ||
| log.verbose('WechatyPluginContrib', 'MessageAwaiter') | ||
|
|
||
| return function MessageAwaiterPlugin (wechaty: Wechaty) { | ||
| log.verbose('WechatyPluginContrib', 'MessageAwaiter installing on %s ...', wechaty) | ||
|
|
||
| wechaty.waitForMessage = async (args: MessageAwaiterArgs): Promise<Message> => { | ||
| let { contactId, roomId, regex, timeoutSecond } = args | ||
| let waitTime = new Date() | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can create the const matchContact = matchers.contactMatcher(args.contact)
const matchRoom = matchers.roomMatcher(args.room)
const matchString = matchers.stringMatcher(args.text) |
||
| return new Promise<Message>((resolve, reject) => { | ||
|
|
||
| let callback = async (message: Message) => { | ||
| let messageFrom = message.from() | ||
|
||
| let messageRoom = message.room() | ||
| if (message.date() < waitTime) return | ||
| if (contactId && messageFrom?.id !== contactId) return | ||
| if (roomId && messageRoom?.id !== roomId) return | ||
| if (regex && !regex.test(message.text())) return | ||
| wechaty.off('message', callback) | ||
| resolve(message) | ||
| } | ||
|
|
||
| wechaty.on('message', callback) | ||
|
|
||
| if (timeoutSecond) { | ||
| setTimeout(() => { | ||
| wechaty.off('message', callback) | ||
| reject(Error('timed out')) | ||
| }, timeoutSecond * 1000) | ||
| } | ||
|
|
||
| log.verbose('begin waiting for message from %s', contactId) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The wechaty-plugin-contrib has standard helper functions/interfaces to deal with those parameters.
Please read the related code and keep this part consistent with other plugins.