generated from wechaty/puppet-mock
-
Notifications
You must be signed in to change notification settings - Fork 7
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
Receive and send audio messages #21
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
12981db
Add audio message support and example
zhangfand 7cd79f5
0.5.44
zhangfand 39d94c8
0.5.45
zhangfand 138e71e
0.5.46
zhangfand f19b12c
Add lock file
zhangfand 6271417
Address CRs; Delete incorrectly included yarn.lock file
zhangfand 2a03cd0
Add port number in echo.ts
zhangfand 99a9ade
Echo text or image message
zhangfand 745391c
Set version to 0.5.44
zhangfand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/** | ||
* Wechaty - https://github.com/chatie/wechaty | ||
* | ||
* @copyright 2016-2018 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 { | ||
EventErrorPayload, | ||
EventLoginPayload, | ||
EventLogoutPayload, | ||
EventMessagePayload, | ||
EventScanPayload, | ||
FileBox, | ||
MessageType | ||
} from 'wechaty-puppet' | ||
|
||
import {PuppetOA} from '../src/mod' | ||
|
||
/** | ||
* | ||
* 1. Declare your Bot! | ||
* | ||
*/ | ||
const puppet = new PuppetOA(); | ||
|
||
// 2. Register event handlers for Bot | ||
puppet | ||
.on('logout', onLogout) | ||
.on('login', onLogin) | ||
.on('scan', onScan) | ||
.on('error', onError) | ||
.on('message', onMessage) | ||
|
||
// 3. Start the bot! | ||
puppet.start() | ||
.catch(async e => { | ||
console.error('Bot start() fail:', e) | ||
await puppet.stop() | ||
process.exit(-1) | ||
}) | ||
|
||
/** | ||
* | ||
* 4. You are all set. ;-] | ||
* | ||
*/ | ||
|
||
/** | ||
* | ||
* 5. Define Event Handler Functions for: | ||
* `scan`, `login`, `logout`, `error`, and `message` | ||
* | ||
*/ | ||
function onScan(payload: EventScanPayload) { | ||
if (payload.qrcode) { | ||
// Generate a QR Code online via | ||
// http://goqr.me/api/doc/create-qr-code/ | ||
const qrcodeImageUrl = [ | ||
'https://api.qrserver.com/v1/create-qr-code/?data=', | ||
encodeURIComponent(payload.qrcode), | ||
].join('') | ||
console.info(`[${payload.status}] ${qrcodeImageUrl}\nScan QR Code above to log in: `) | ||
} else { | ||
console.info(`[${payload.status}]`) | ||
} | ||
} | ||
|
||
function onLogin(payload: EventLoginPayload) { | ||
console.info(`${payload.contactId} login`) | ||
puppet.messageSendText(payload.contactId, 'Wechaty login').catch(console.error) | ||
zhangfand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
function onLogout(payload: EventLogoutPayload) { | ||
console.info(`${payload.contactId} logouted`) | ||
} | ||
|
||
function onError(payload: EventErrorPayload) { | ||
console.error('Bot error:', payload.data) | ||
/* | ||
if (bot.logonoff()) { | ||
bot.say('Wechaty error: ' + e.message).catch(console.error) | ||
} | ||
*/ | ||
} | ||
|
||
/** | ||
* | ||
* 6. The most important handler is for: | ||
* dealing with Messages. | ||
* | ||
*/ | ||
async function onMessage(event: EventMessagePayload) { | ||
const payload = await puppet.messagePayload(event.messageId) | ||
console.info('onMessage:', JSON.stringify(payload)) | ||
switch (payload.type) { | ||
case MessageType.Text: | ||
await puppet.messageSendText(payload.fromId!, payload.text!); | ||
break | ||
case MessageType.Audio: | ||
// Forward the received audio content back to the user. | ||
// HACK(zhangfan): We use .filename to pass url to the the message audio content. | ||
// Note that setting name in .fromUrl is necessary because .messageSend relies on it to | ||
// infer the Content-Type to the send audio file back to users. | ||
// TODO(zhangfan): This is fragile. Fix it. | ||
let fileBox = FileBox.fromUrl(payload.filename!, "message.amr") | ||
console.log(`file name: ${fileBox.name}; mimetype: ${fileBox.mimeType}`) | ||
await puppet.messageSend(payload.fromId!, fileBox, "voice"); | ||
break | ||
default: | ||
console.info(`onMessage: unsupported message type: ${payload.type}`) | ||
} | ||
} | ||
|
||
|
||
/** | ||
* | ||
* 7. Output the Welcome Message | ||
* | ||
*/ | ||
const welcome = ` | ||
Puppet Version: ${puppet.version()} | ||
|
||
Please wait... I'm trying to login in... | ||
|
||
` | ||
console.info(welcome) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
https://github.com/wechaty/wechaty-getting-started/blob/7c3ebd90f4b8cb7f22bf49ca419debbdde275437/examples/ding-dong-bot.ts#L21-L24
Please use our wechaty.js.org to generate the online qrcode. because the qrserver.com is too slow and always out of service.
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.
See the comment below. TL;DR I deleted it in the newest commit.