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

[How to guides] Dealing with messages #980

Merged
merged 33 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
9face34
added code for dealing messages
vasvi-sood Jun 25, 2021
8e487de
0.8.303
vasvi-sood Jun 25, 2021
e194ba8
improved description
vasvi-sood Jun 30, 2021
8043a24
0.8.304
vasvi-sood Jun 30, 2021
b28788e
corrected typo
vasvi-sood Jun 30, 2021
6fa7aaa
0.8.305
vasvi-sood Jun 30, 2021
e3c8047
Merge branch 'master' into dealing-messages
abhishek-iiit Jul 5, 2021
de51567
added prerequisites
vasvi-sood Jul 21, 2021
e7126b8
corrected typo
vasvi-sood Jul 21, 2021
45576f2
0.8.310
vasvi-sood Jul 21, 2021
983336e
corrected spelling mistakes
vasvi-sood Jul 21, 2021
6a9091a
0.8.311
vasvi-sood Jul 21, 2021
5a6c1b3
indented block
vasvi-sood Sep 13, 2021
51676ff
0.8.312
vasvi-sood Sep 13, 2021
1cacd6b
minor change
vasvi-sood Sep 17, 2021
4ae7238
0.8.313
vasvi-sood Sep 17, 2021
724cebe
fixed nodejs version and friend-bot link
vasvi-sood Sep 24, 2021
749089a
0.8.314
vasvi-sood Sep 24, 2021
f9cfc00
0.8.315
vasvi-sood Sep 24, 2021
07a92f7
0.8.316
vasvi-sood Sep 24, 2021
9045dbb
Merge branch 'master' into dealing-messages
vasvi-sood Oct 2, 2021
22d859c
Merge branch 'main' into dealing-messages
Rohitesh-Kumar-Jain Oct 16, 2021
382451e
Merge branch 'main' into dealing-messages
vasvi-sood Oct 17, 2021
1824844
Merge branch 'main' into dealing-messages
vasvi-sood Oct 17, 2021
7d2858b
Merge branch 'main' into dealing-messages
abhishek-iiit Oct 24, 2021
ca33dce
Merge branch 'main' into dealing-messages
abhishek-iiit Oct 27, 2021
a2c665d
added changes
abhishek-iiit Oct 27, 2021
6c48cd6
0.10.28
abhishek-iiit Oct 27, 2021
6cff3d9
0.10.29
abhishek-iiit Oct 27, 2021
01eeedb
Merge branch 'main' into dealing-messages
abhishek-iiit Nov 3, 2021
6ae67c2
Merge branch 'main' into dealing-messages
shraddhavp Nov 14, 2021
3e0691d
Merge branch 'main' into dealing-messages
shraddhavp Nov 17, 2021
038201c
Merge branch 'main' into dealing-messages
shraddhavp Jan 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions docusaurus/docs/howto/message.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ title: 'Dealing with message'
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'

Automation of messages can be done easily with wechaty onMessage function. This guide will give you a step by step overview of how to respond to self messages or messages in a room.

## Mention
shraddhavp marked this conversation as resolved.
Show resolved Hide resolved

Only a message in the room can mention(@) others.
Use this Mention feature to send a (@ mention) to others in the room.This function works if the message recieved by the onMessage function belongs to a room.

<Tabs
groupId="programming-languages"
Expand Down Expand Up @@ -73,7 +75,7 @@ async function onMessage(message) {
// "Hello"

const talker = room.talker()
await room.say`Thanks for mention me! ${talker}`
await room.say`Thanks for mentioning me! ${talker}`
}
}

Expand Down Expand Up @@ -139,8 +141,13 @@ class MyBot(Wechaty):
</TabItem>
</Tabs>

The expected output of the JavaScript code is:
![Message](../../static/img/docs/howto/message/message1.png)

## Self message

Use this Self message feature to reply to the bot.This function works if the message recieved by the onMessage function has been sent by the bot to itself.

<Tabs
groupId="programming-languages"
defaultValue="ts"
Expand Down Expand Up @@ -180,14 +187,17 @@ bot.on('message', onMessage)
```js
const { Message } = require('wechaty')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import { Message } from 'wechaty'


async function onMessage(message) {
if (message.self()) {
const talker = message.talker()
const bot = message.wechaty.userSelf()
assert(talker === bot, 'Message is sent from bot')
async function onMessage (msg) {

log.info('StarterBot', msg.toString())
const contact = msg.talker()
console.log(contact);
shraddhavp marked this conversation as resolved.
Show resolved Hide resolved
console.log("message self",msg.self());
if (msg.self()) {
const b = msg.wechaty.userSelf()
assert(talker === b, 'Message is sent from bot')
console.info('Message is sent from bot')
}
}

bot.on('message', onMessage)
```
Expand Down Expand Up @@ -259,3 +269,6 @@ class MyBot(Wechaty):

</TabItem>
</Tabs>

The expected output of the JavaScript code is:
![Message](../../static/img/docs/howto/message/message.png)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.