meta-messenger.js 0.0.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 ADDED
@@ -0,0 +1,166 @@
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