@wechatbot/wechatbot 2.3.0 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -51,12 +51,22 @@ const bot = new WeChatBot({
|
|
|
51
51
|
storageDir: '~/.wechatbot',
|
|
52
52
|
logLevel: 'info', // 'debug' | 'info' | 'warn' | 'error' | 'silent'
|
|
53
53
|
loginCallbacks: {
|
|
54
|
-
onQrUrl: (url) => renderQrCode(url),
|
|
54
|
+
onQrUrl: (url) => renderQrCode(url), // without it, the QR URL is logged at warn level
|
|
55
55
|
onScanned: () => console.log('Scanned!'),
|
|
56
|
+
onVerifyCode: (isRetry) => askUser(), // digits WeChat shows on the phone
|
|
56
57
|
},
|
|
58
|
+
botAgent: 'MyApp/1.0', // sent as base_info.bot_agent
|
|
59
|
+
messageCacheSize: 1000, // recent messages kept to fill quoted text; 0 disables
|
|
60
|
+
routeTag: undefined, // deployment route tag, only if WeChat gave you one
|
|
61
|
+
verifyPin: false, // ask WeChat to require the phone PIN when binding
|
|
57
62
|
})
|
|
58
63
|
```
|
|
59
64
|
|
|
65
|
+
`loginCallbacks` set here apply to every login, including the automatic
|
|
66
|
+
re-login after a session expires. `bot.login({ callbacks })` overrides them
|
|
67
|
+
field by field. Without `onVerifyCode`, the SDK reads the PIN from stdin, and
|
|
68
|
+
fails with an `AuthError` when stdin is not a terminal.
|
|
69
|
+
|
|
60
70
|
### Proxy
|
|
61
71
|
|
|
62
72
|
Node's built-in `fetch` ignores `HTTPS_PROXY`. To send requests through a proxy:
|
|
@@ -123,6 +133,62 @@ await bot.reply(msg, { url: 'https://example.com/photo.jpg' })
|
|
|
123
133
|
await bot.send(userId, { image: buffer, caption: 'Hi!' })
|
|
124
134
|
```
|
|
125
135
|
|
|
136
|
+
A caption is sent as its own text message before the media. WeChat rejects
|
|
137
|
+
text and media in one message.
|
|
138
|
+
|
|
139
|
+
`reply`, `send` and `sendRaw` return a `SendResult`:
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
const { messageId, captionMessageId } = await bot.reply(msg, 'Hello!')
|
|
143
|
+
// messageId: server ID (uint64 as a string). A later quote of this reply refers to it.
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
WeChat lets a bot message a user only within 24 hours of the user's last
|
|
147
|
+
message, and only a few times before the user writes again. A rejected send
|
|
148
|
+
throws an `ApiError` with `isSendRejected === true` (`ret=-2`):
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
try {
|
|
152
|
+
await bot.send(userId, 'Reminder')
|
|
153
|
+
} catch (err) {
|
|
154
|
+
if (err instanceof ApiError && err.isSendRejected) {
|
|
155
|
+
// wait until the user messages the bot again
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Quoted messages
|
|
161
|
+
|
|
162
|
+
Newer WeChat clients often send only the **ID** of a quoted message, not its
|
|
163
|
+
text. The SDK keeps the last `messageCacheSize` received and sent messages in
|
|
164
|
+
memory and fills `quotedMessage.text` when it has seen the quoted message.
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
bot.onMessage((msg) => {
|
|
168
|
+
const q = msg.quotedMessage
|
|
169
|
+
if (q) {
|
|
170
|
+
q.messageId // server ID of the quoted message
|
|
171
|
+
q.text // filled from the cache when known; for a partial quote, the quoted part
|
|
172
|
+
q.fromBot // true when the user quoted one of the bot's messages
|
|
173
|
+
q.partialText // present when only part of the message was quoted
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
The cache is in memory only; a restart clears it.
|
|
179
|
+
|
|
180
|
+
### Underscores in text
|
|
181
|
+
|
|
182
|
+
WeChat renders bot text as Markdown, so `file_name.txt` can show as
|
|
183
|
+
`filename.txt`. `protectUnderscores(text)` replaces `_` outside code spans with
|
|
184
|
+
the full-width `_` (U+FF3F), which WeChat displays as is. Text a user copies
|
|
185
|
+
then contains `_`, so the SDK does not do this automatically.
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
import { protectUnderscores } from '@wechatbot/wechatbot'
|
|
189
|
+
await bot.reply(msg, protectUnderscores('Saved EPS_manuscript_CN.pdf'))
|
|
190
|
+
```
|
|
191
|
+
|
|
126
192
|
### Typing
|
|
127
193
|
|
|
128
194
|
| Method | Description |
|
|
@@ -171,6 +237,7 @@ bot.on('close', () => { })
|
|
|
171
237
|
|
|
172
238
|
```typescript
|
|
173
239
|
interface IncomingMessage {
|
|
240
|
+
messageId?: string // server ID (uint64 as a string)
|
|
174
241
|
userId: string
|
|
175
242
|
text: string
|
|
176
243
|
type: 'text' | 'image' | 'voice' | 'file' | 'video'
|
|
@@ -182,6 +249,15 @@ interface IncomingMessage {
|
|
|
182
249
|
quotedMessage?: QuotedMessage
|
|
183
250
|
raw: WireMessage
|
|
184
251
|
}
|
|
252
|
+
|
|
253
|
+
interface QuotedMessage {
|
|
254
|
+
messageId?: string
|
|
255
|
+
title?: string
|
|
256
|
+
text?: string
|
|
257
|
+
type?: 'text' | 'image' | 'voice' | 'file' | 'video'
|
|
258
|
+
fromBot?: boolean
|
|
259
|
+
partialText?: PartialText
|
|
260
|
+
}
|
|
185
261
|
```
|
|
186
262
|
|
|
187
263
|
### Storage Interface
|