meta-messenger.js 0.0.1 → 0.0.5

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 CHANGED
@@ -1,166 +1,174 @@
1
- # Unofficial Facebook Messenger API
2
-
3
- Facebook now has an official API for chat bots [here](https://developers.facebook.com/docs/messenger-platform).
4
-
5
- This API is one of the ways to automate chat functions on user accounts.
6
- Using the [mautrix-meta](https://github.com/mautrix/meta) library (Go) through FFI binding, this library supports (almost) all features that the original library has implemented through a high-level JavaScript API.
7
-
8
- > [!CAUTION]
9
- > **Using this feature on user accounts is prohibited under Meta's Terms of Service and may result in account suspension.**
10
- > **I am not responsible for any Meta accounts that are suspended due to using this library.**
11
-
12
- ## Installation
13
- ```bash
14
- npm install meta-messenger.js
15
- ```
16
-
17
- ### Requirements
18
- - Node.js >= 22
19
- - Cookies from a logged-in Facebook/Messenger account
20
-
21
- ## Usage Example
22
- ```typescript
23
- import { Client, Utils } from 'meta-messenger.js'
24
- import { readFileSync } from 'fs'
25
-
26
- // Read cookies from file (supports multiple formats)
27
- const cookies = Utils.parseCookies(readFileSync('cookies.json', 'utf-8'))
28
-
29
- // Create a new client
30
- const client = new Client(cookies)
31
-
32
- // Listen to events
33
- client.on('message', async (message) => {
34
- console.log(`Message from ${message.senderId}: ${message.text}`)
35
-
36
- // Simple echo bot
37
- if (message.text === 'ping') {
38
- await client.sendMessage(message.threadId, 'pong')
39
- }
40
- })
41
-
42
- // Connect
43
- await client.connect()
44
- console.log(`Logged in as: ${client.user?.name}`)
45
- ```
46
-
47
- ![img](screenshots/demo1.png)
48
-
49
- ## [Documentation](DOCS.md)
50
-
51
- ## Main Features
52
-
53
- ### Send Messages
54
- ```typescript
55
- // Simple message
56
- await client.sendMessage(threadId, 'Hello!')
57
-
58
- // Message with reply
59
- await client.sendMessage(threadId, {
60
- text: 'This is a reply',
61
- replyToId: 'mid.xxx'
62
- })
63
-
64
- // Message with mention
65
- await client.sendMessage(threadId, {
66
- text: 'Hello @friend!',
67
- mentions: [{ userId: 123456, offset: 6, length: 7 }]
68
- })
69
- ```
70
-
71
- ### Send Media
72
- ```typescript
73
- import { readFileSync } from 'fs'
74
-
75
- // Send image
76
- const image = readFileSync('photo.jpg')
77
- await client.sendImage(threadId, image, 'photo.jpg', 'Caption here')
78
-
79
- // Send video
80
- const video = readFileSync('video.mp4')
81
- await client.sendVideo(threadId, video, 'video.mp4')
82
-
83
- // Send sticker
84
- await client.sendSticker(threadId, 369239263222822) // thumbs up
85
- ```
86
-
87
- ### Save Session
88
-
89
- To avoid having to get new cookies each time, you should save the device data (for E2EE) to a file:
90
-
91
- ```typescript
92
- import { writeFileSync, readFileSync } from 'fs'
93
-
94
- // Save device data when it changes
95
- client.on('deviceDataChanged', ({ deviceData }) => {
96
- writeFileSync('device.json', deviceData)
97
- })
98
-
99
- // Load device data on startup
100
- const deviceData = readFileSync('device.json', 'utf-8')
101
- const client = new Client(cookies, { deviceData })
102
- ```
103
-
104
- ### Listen to Messages
105
-
106
- ```typescript
107
- client.on('message', (message) => {
108
- console.log(`[${message.threadId}] ${message.senderId}: ${message.text}`)
109
-
110
- // Handle attachments
111
- for (const att of message.attachments || []) {
112
- if (att.type === 'image') {
113
- console.log(` Image: ${att.url}`)
114
- } else if (att.type === 'sticker') {
115
- console.log(` Sticker: ${att.stickerId}`)
116
- }
117
- }
118
- })
119
-
120
- // E2EE messages
121
- client.on('e2eeMessage', (message) => {
122
- console.log(`[E2EE] ${message.senderJid}: ${message.text}`)
123
- })
124
- ```
125
-
126
- ## Supported Cookie Formats
127
-
128
- The API supports multiple cookie formats through `Utils.parseCookies()`:
129
-
130
- 1. **C3C UFC Utility / EditThisCookie**
131
- 2. **Simple object**
132
- 3. **Cookie header string**
133
- 4. **Netscape cookie file format**
134
- 5. **Base64 encoded** (any of the above formats)
135
-
136
- Required cookies: `c_user`, `xs`, `datr`, `fr`
137
-
138
- ## FAQs
139
-
140
- 1. **How do I get cookies?**
141
- > Log in to Facebook in your browser, open DevTools (F12), go to Application > Cookies tab, and copy the values for `c_user`, `xs`, `datr`, `fr`.
142
-
143
- 2. **Why am I not receiving E2EE messages?**
144
- > Make sure you have enabled E2EE with `enableE2EE: true` and wait for the `fullyReady` event before processing messages.
145
-
146
- 3. **What's the difference between message and e2eeMessage events?**
147
- > - `message`: Regular messages, metadata visible to Facebook
148
- > - `e2eeMessage`: End-to-end encrypted messages, only sender and receiver can read
149
-
150
- 4. **Why do I need to wait for fullyReady?**
151
- > The `fullyReady` event is emitted when both socket and E2EE (if enabled) are ready. If you process messages before this, you may miss some E2EE messages.
152
-
153
- ## License
154
-
155
- GNU Affero General Public License v3.0
156
-
157
- According to mautrix-meta License
158
-
159
- ## Credits
160
-
161
- - Claude Opus 4.5 - Supports understanding and developing this library
162
- - [mautrix-meta](https://github.com/mautrix/meta) - A Matrix-Facebook Messenger and Instagram DM puppeting bridge.
163
- - [whatsmeow](https://github.com/tulir/whatsmeow) - Go library for the WhatsApp web multidevice API
164
- - [facebook-chat-api](https://github.com/Schmavery/facebook-chat-api) - Inspired the API Style
165
- - [whatsmeow-node](https://github.com/vinikjkkj/whatsmeow-node) - Reference for writing NodeJS library with FFI.
166
- - [koffi](https://codeberg.org/Koromix/rygel/src/branch/master/src/koffi) - Fast and easy-to-use C FFI module for Node.js
1
+ # Unofficial Facebook Messenger API
2
+
3
+ <a href="https://www.npmjs.com/package/meta-messenger.js"><img alt="npm version" src="https://img.shields.io/npm/v/meta-messenger.js.svg?style=flat-square"></a>
4
+ <a href="https://www.npmjs.com/package/meta-messenger.js"><img src="https://img.shields.io/npm/dm/meta-messenger.js.svg?style=flat-square" alt="npm downloads"></a>
5
+
6
+ Facebook now has an official API for chat bots [here](https://developers.facebook.com/docs/messenger-platform).
7
+
8
+ This API is one of the ways to automate chat functions on user accounts.
9
+ Using the [mautrix-meta](https://github.com/mautrix/meta) library (Go) through FFI binding, this library supports (almost) all features that the original library has implemented through a high-level JavaScript API.
10
+
11
+ > [!CAUTION]
12
+ > **Using this feature on user accounts is prohibited under Meta's Terms of Service and may result in account suspension.**
13
+ > **I am not responsible for any Meta accounts that are suspended due to using this library.**
14
+
15
+ ## Installation
16
+ ```bash
17
+ npm install meta-messenger.js
18
+ ```
19
+
20
+ ### Requirements
21
+ - Node.js >= 22
22
+ - Cookies from a logged-in Facebook/Messenger account
23
+
24
+ ## Usage Example
25
+ ```typescript
26
+ import { Client, Utils } from 'meta-messenger.js'
27
+ import { readFileSync } from 'fs'
28
+
29
+ // Read cookies from file (supports multiple formats)
30
+ const cookies = Utils.parseCookies(readFileSync('cookies.json', 'utf-8'))
31
+
32
+ // Create a new client
33
+ const client = new Client(cookies)
34
+
35
+ // Listen to events
36
+ client.on('message', async (message) => {
37
+ console.log(`Message from ${message.senderId}: ${message.text}`)
38
+
39
+ // Simple echo bot
40
+ if (message.text === 'ping') {
41
+ await client.sendMessage(message.threadId, 'pong')
42
+ }
43
+ })
44
+
45
+ // Connect
46
+ await client.connect()
47
+ console.log(`Logged in as: ${client.user?.name}`)
48
+ ```
49
+
50
+ ![img](screenshots/demo1.png)
51
+
52
+ ## [Documentation](DOCS.md)
53
+
54
+ ## Main Features
55
+
56
+ ### Send Messages
57
+ ```typescript
58
+ // Simple message
59
+ await client.sendMessage(threadId, 'Hello!')
60
+
61
+ // Message with reply
62
+ await client.sendMessage(threadId, {
63
+ text: 'This is a reply',
64
+ replyToId: 'mid.xxx'
65
+ })
66
+
67
+ // Message with mention
68
+ await client.sendMessage(threadId, {
69
+ text: 'Hello @friend!',
70
+ mentions: [{ userId: 123456, offset: 6, length: 7 }]
71
+ })
72
+ ```
73
+
74
+ ### Send Media
75
+ ```typescript
76
+ import { readFileSync } from 'fs'
77
+
78
+ // Send image
79
+ const image = readFileSync('photo.jpg')
80
+ await client.sendImage(threadId, image, 'photo.jpg', 'Caption here')
81
+
82
+ // Send video
83
+ const video = readFileSync('video.mp4')
84
+ await client.sendVideo(threadId, video, 'video.mp4')
85
+
86
+ // Send sticker
87
+ await client.sendSticker(threadId, 369239263222822) // thumbs up
88
+ ```
89
+
90
+ ### Save Session
91
+
92
+ To avoid having to get new cookies each time, you should save the device data (for E2EE) to a file:
93
+
94
+ ```typescript
95
+ import { writeFileSync, readFileSync } from 'fs'
96
+
97
+ // Save device data when it changes
98
+ client.on('deviceDataChanged', ({ deviceData }) => {
99
+ writeFileSync('device.json', deviceData)
100
+ })
101
+
102
+ // Load device data on startup
103
+ const deviceData = readFileSync('device.json', 'utf-8')
104
+ const client = new Client(cookies, { deviceData })
105
+ ```
106
+
107
+ ### Listen to Messages
108
+
109
+ ```typescript
110
+ client.on('message', (message) => {
111
+ console.log(`[${message.threadId}] ${message.senderId}: ${message.text}`)
112
+
113
+ // Handle attachments
114
+ for (const att of message.attachments || []) {
115
+ if (att.type === 'image') {
116
+ console.log(` Image: ${att.url}`)
117
+ } else if (att.type === 'sticker') {
118
+ console.log(` Sticker: ${att.stickerId}`)
119
+ }
120
+ }
121
+ })
122
+
123
+ // E2EE messages
124
+ client.on('e2eeMessage', (message) => {
125
+ console.log(`[E2EE] ${message.senderJid}: ${message.text}`)
126
+ })
127
+ ```
128
+
129
+ ## Supported Cookie Formats
130
+
131
+ The API supports multiple cookie formats through `Utils.parseCookies()`:
132
+
133
+ 1. **C3C UFC Utility / EditThisCookie**
134
+ 2. **Simple object**
135
+ 3. **Cookie header string**
136
+ 4. **Netscape cookie file format**
137
+ 5. **Base64 encoded** (any of the above formats)
138
+
139
+ Required cookies: `c_user`, `xs`, `datr`, `fr`
140
+
141
+ ## FAQs
142
+
143
+ 1. **How do I get cookies?**
144
+ > Log in to Facebook in your browser, open DevTools (F12), go to Application > Cookies tab, and copy the values for `c_user`, `xs`, `datr`, `fr`.
145
+
146
+ 2. **Why am I not receiving E2EE messages?**
147
+ > Make sure you have enabled E2EE with `enableE2EE: true` and wait for the `fullyReady` event before processing messages.
148
+
149
+ 3. **What's the difference between message and e2eeMessage events?**
150
+ > - `message`: Regular messages, metadata visible to Facebook
151
+ > - `e2eeMessage`: End-to-end encrypted messages, only sender and receiver can read
152
+
153
+ 4. **Why do I need to wait for fullyReady?**
154
+ > The `fullyReady` event is emitted when both socket and E2EE (if enabled) are ready. If you process messages before this, you may miss some E2EE messages.
155
+
156
+ ## License
157
+
158
+ GNU Affero General Public License v3.0
159
+
160
+ According to mautrix-meta License
161
+
162
+ ## Credits
163
+
164
+ - Claude Opus 4.5 - Supports understanding and developing this library
165
+ - [mautrix-meta](https://github.com/mautrix/meta) - A Matrix-Facebook Messenger and Instagram DM puppeting bridge.
166
+ - [whatsmeow](https://github.com/tulir/whatsmeow) - Go library for the WhatsApp web multidevice API
167
+ - [facebook-chat-api](https://github.com/Schmavery/facebook-chat-api) - Inspired the API Style
168
+ - [whatsmeow-node](https://github.com/vinikjkkj/whatsmeow-node) - Reference for writing NodeJS library with FFI.
169
+ - [koffi](https://codeberg.org/Koromix/rygel/src/branch/master/src/koffi) - Fast and easy-to-use C FFI module for Node.js
170
+
171
+ ## Disclaimer
172
+
173
+ > All product and company names are trademarks™ or registered® trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them.
174
+ > "Facebook", "Messenger", "Meta", "Instagram" is a registered trademark of Meta, Inc., used under license agreement.
package/dist/index.js CHANGED
@@ -995,7 +995,7 @@ var Utils = class _Utils extends null {
995
995
  * ```
996
996
  */
997
997
  static toCookieArray(cookies, domain = ".facebook.com") {
998
- return Object.entries(cookies).filter(([_, value]) => value !== void 0).map(([name, value]) => ({
998
+ return Object.entries(cookies).filter(([, value]) => value !== void 0).map(([name, value]) => ({
999
999
  name,
1000
1000
  value,
1001
1001
  domain,