Skip to content

Commit 971198f

Browse files
committed
Add option to specify API options at login
1 parent dcadd69 commit 971198f

File tree

2 files changed

+123
-87
lines changed

2 files changed

+123
-87
lines changed

DOCS.md

Lines changed: 102 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@
2020
- [loginCallback][16]
2121
- [Parameters][17]
2222
- [apiObj][18]
23-
- [genericErrCb][19]
24-
- [Parameters][20]
25-
- [errDataCb][21]
26-
- [Parameters][22]
27-
- [searchAttribute][23]
28-
- [Parameters][24]
29-
- [monitoring][25]
30-
- [monitor][26]
31-
- [Parameters][27]
32-
- [cancelMonitoring][28]
33-
- [retryLoginCallback][29]
34-
- [Parameters][30]
23+
- [optionsObj][19]
24+
- [genericErrCb][20]
25+
- [Parameters][21]
26+
- [errDataCb][22]
27+
- [Parameters][23]
28+
- [searchAttribute][24]
29+
- [Parameters][25]
30+
- [monitoring][26]
31+
- [monitor][27]
32+
- [Parameters][28]
33+
- [cancelMonitoring][29]
34+
- [retryLoginCallback][30]
35+
- [Parameters][31]
3536

3637
## login
3738

@@ -44,42 +45,45 @@ Encapsulates the login by caching the appstate in memory.
4445
### login
4546

4647
Call this to initialize the login module and log into Facebook using
47-
[facebook-chat-api][31].
48+
[facebook-chat-api][32].
4849
See examples/ for example usage.
4950

5051
#### Parameters
5152

52-
- `credentials` **[credentialsObj][32]**
53-
- `callback` **[loginCallback][33]** called after login completed (successfully or unsuccessfully)
54-
- `forceCreds` **[Boolean][34]** if true, forces a login with credentials even if
55-
appstate exists (optional) (optional, default `false`)
53+
- `credentials` **[credentialsObj][33]**
54+
- `callback` **[loginCallback][34]** called after login completed (successfully or unsuccessfully)
55+
- `forceCreds` **[Boolean][35]** if true, forces a login with credentials even if
56+
appstate exists (optional, default `false`)
57+
- `options` **[optionsObj][36]** any options you wish to pass to the API on login;
58+
by default, sets `logLevel` to `error` and `forceLogin` to `true` (auto-approves errors asking
59+
for approval of recent logins for simplicity) (optional, default `defaultOptions`)
5660

5761
### dumpLogin
5862

5963
Dumps the current login into a specified file.
6064

6165
#### Parameters
6266

63-
- `filename` **[string][35]** Name of the file specifying where to store the login
64-
- `callback` **[genericErrCb][36]** Callback to use after writing the file
67+
- `filename` **[string][37]** Name of the file specifying where to store the login
68+
- `callback` **[genericErrCb][38]** Callback to use after writing the file
6569

6670
### loadLogin
6771

6872
Reads a new login into memory from a file.
6973

7074
#### Parameters
7175

72-
- `filename` **[string][35]** Name of the file specifying where the imported login
76+
- `filename` **[string][37]** Name of the file specifying where the imported login
7377
is stored
74-
- `callback` **[genericErrCb][36]** Callback to use after reading the login
78+
- `callback` **[genericErrCb][38]** Callback to use after reading the login
7579

7680
### logout
7781

7882
Logs out of Facebook.
7983

8084
#### Parameters
8185

82-
- `callback` **[errDataCb][37]**
86+
- `callback` **[errDataCb][39]**
8387

8488
### convert
8589

@@ -89,9 +93,9 @@ an fbchat bot with BotCore.
8993

9094
#### Parameters
9195

92-
- `filename` **[string][35]** Name of the file whose location contains the
96+
- `filename` **[string][37]** Name of the file whose location contains the
9397
appstate data to be converted
94-
- `callback` **[errDataCb][37]** Callback to use after conversion completed,
98+
- `callback` **[errDataCb][39]** Callback to use after conversion completed,
9599
passed the converted session
96100

97101
### convertToFile
@@ -100,9 +104,9 @@ A variant of `convert` that directly outputs the converted session to a file.
100104

101105
#### Parameters
102106

103-
- `appstate` **[string][35]** Location of appstate to be converted
104-
- `output` **[string][35]** Where to place the converted session
105-
- `callback` **[genericErrCb][36]** Callback called after conversion
107+
- `appstate` **[string][37]** Location of appstate to be converted
108+
- `output` **[string][37]** Where to place the converted session
109+
- `callback` **[genericErrCb][38]** Callback called after conversion
106110

107111
## credentialsObj
108112

@@ -113,7 +117,7 @@ of several required keys that allow BotCore to log in to both Facebook and the M
113117
(used to cache logins) on your behalf. The keys are listed and explained below.
114118

115119
> **NOTE**: to obtain the values for the `MEMCACHIER_` variables, you must [sign up for a free
116-
> MemCachier account][38] and create a cache. From there, you
120+
> MemCachier account][40] and create a cache. From there, you
117121
> will be able to retrieve the requisite info from your dashboard.
118122
119123
I recommend the following two methods for storing your credentials object due to their ease of use:
@@ -143,49 +147,58 @@ If you are using several bots with BotCore, consider storing your `FACEBOOK_EMAI
143147
`FACEBOOK_PASSWORD` keys with only one of them, and only using your `MEMCACHIER_` variables to log in
144148
from other bots.
145149

146-
Type: [Object][39]
150+
Type: [Object][41]
147151

148152
### Properties
149153

150-
- `MEMCACHIER_USERNAME` **[string][35]** Memcachier username (from dashboard) for storage
151-
- `MEMCACHIER_PASSWORD` **[string][35]** Memcachier password (from dashboard) for storage
152-
- `MEMCACHIER_SERVERS` **[string][35]** Memcachier servers (from dashboard) for storage
153-
- `FACEBOOK_EMAIL` **[string][35]** Facebook account email for login (optional if already logged in once)
154-
- `FACEBOOK_PASSWORD` **[string][35]** Facebook account password for login (optional if already logged in once)
154+
- `MEMCACHIER_USERNAME` **[string][37]** Memcachier username (from dashboard) for storage
155+
- `MEMCACHIER_PASSWORD` **[string][37]** Memcachier password (from dashboard) for storage
156+
- `MEMCACHIER_SERVERS` **[string][37]** Memcachier servers (from dashboard) for storage
157+
- `FACEBOOK_EMAIL` **[string][37]** Facebook account email for login (optional if already logged in once)
158+
- `FACEBOOK_PASSWORD` **[string][37]** Facebook account password for login (optional if already logged in once)
155159

156160
## loginCallback
157161

158-
Type: [Function][40]
162+
Type: [Function][42]
159163

160164
### Parameters
161165

162-
- `err` **[string][35]** indicates errors (null if login is successful)
163-
- `api` **[apiObj][41]** null if login fails, see
164-
[facebook-chat-api][31] for details
166+
- `err` **[string][37]** indicates errors (null if login is successful)
167+
- `api` **[apiObj][43]** null if login fails, see
168+
[facebook-chat-api][32] for details
165169

166170
## apiObj
167171

168172
An API instance of the facebook-chat-api (see
169-
[here][31] for details)
173+
[here][32] for details)
170174

171-
Type: [Object][39]
175+
Type: [Object][41]
176+
177+
## optionsObj
178+
179+
An object containing options to be passed to the underlying
180+
facebook-chat-api instance on login (see
181+
[here][44]
182+
for details)
183+
184+
Type: [Object][41]
172185

173186
## genericErrCb
174187

175-
Type: [Function][40]
188+
Type: [Function][42]
176189

177190
### Parameters
178191

179-
- `err` **[string][35]** Message specifying the error (or null if none)
192+
- `err` **[string][37]** Message specifying the error (or null if none)
180193

181194
## errDataCb
182195

183-
Type: [Function][40]
196+
Type: [Function][42]
184197

185198
### Parameters
186199

187-
- `err` **[string][35]** Message specifying the error (or null if none)
188-
- `success` **[Object][39]** Data returned from the successful operation
200+
- `err` **[string][37]** Message specifying the error (or null if none)
201+
- `success` **[Object][41]** Data returned from the successful operation
189202

190203
## searchAttribute
191204

@@ -197,10 +210,10 @@ the other information.
197210

198211
### Parameters
199212

200-
- `data` **[Object][39]** facebook-chat-api appstate
201-
- `key` **[string][35]** The key to locate
213+
- `data` **[Object][41]** facebook-chat-api appstate
214+
- `key` **[string][37]** The key to locate
202215

203-
Returns **[string][35]** The value of the key (or null if not found)
216+
Returns **[string][37]** The value of the key (or null if not found)
204217

205218
## monitoring
206219

@@ -217,15 +230,15 @@ Begins monitoring a specified API instance.
217230

218231
#### Parameters
219232

220-
- `apiInstance` **[apiObj][41]** An instance of the facebook-chat-api to monitor
221-
- `maintainerId` **[string][35]** User ID of the maintainer to notify on failures
222-
- `botName` **[string][35]** Name of the bot running
223-
- `credentialsObj` **[credentialsObj][32]** Object containing the user credentials
224-
- `botProcessRef` **[process][42]** Node.js process to monitor (optional)
225-
- `retryLoginCallback` **[retryLoginCallback][43]** A callback to send a new API
233+
- `apiInstance` **[apiObj][43]** An instance of the facebook-chat-api to monitor
234+
- `maintainerId` **[string][37]** User ID of the maintainer to notify on failures
235+
- `botName` **[string][37]** Name of the bot running
236+
- `credentialsObj` **[credentialsObj][33]** Object containing the user credentials
237+
- `botProcessRef` **[process][45]** Node.js process to monitor (optional)
238+
- `retryLoginCallback` **[retryLoginCallback][46]** A callback to send a new API
226239
instance to if login failed and a re-attempted login succeeded (optional –
227240
omitting this callback is equivalent to disabling the retry login feature)
228-
- `pingIntervalInMinutes` **[number][44]** The number of minutes between
241+
- `pingIntervalInMinutes` **[number][47]** The number of minutes between
229242
checks that the bot is still running (optional, default `10`)
230243

231244
### cancelMonitoring
@@ -234,11 +247,11 @@ Cancels the monitoring of the current bot process.
234247

235248
## retryLoginCallback
236249

237-
Type: [Function][40]
250+
Type: [Function][42]
238251

239252
### Parameters
240253

241-
- `api` **[apiObj][41]** A new instance of the facebook-chat-api after a successful login
254+
- `api` **[apiObj][43]** A new instance of the facebook-chat-api after a successful login
242255

243256
[1]: #login
244257

@@ -276,54 +289,60 @@ Type: [Function][40]
276289

277290
[18]: #apiobj
278291

279-
[19]: #genericerrcb
292+
[19]: #optionsobj
293+
294+
[20]: #genericerrcb
295+
296+
[21]: #parameters-7
297+
298+
[22]: #errdatacb
280299

281-
[20]: #parameters-7
300+
[23]: #parameters-8
282301

283-
[21]: #errdatacb
302+
[24]: #searchattribute
284303

285-
[22]: #parameters-8
304+
[25]: #parameters-9
286305

287-
[23]: #searchattribute
306+
[26]: #monitoring
288307

289-
[24]: #parameters-9
308+
[27]: #monitor
290309

291-
[25]: #monitoring
310+
[28]: #parameters-10
292311

293-
[26]: #monitor
312+
[29]: #cancelmonitoring
294313

295-
[27]: #parameters-10
314+
[30]: #retrylogincallback
296315

297-
[28]: #cancelmonitoring
316+
[31]: #parameters-11
298317

299-
[29]: #retrylogincallback
318+
[32]: https://github.com/Schmavery/facebook-chat-api
300319

301-
[30]: #parameters-11
320+
[33]: #credentialsobj
302321

303-
[31]: https://github.com/Schmavery/facebook-chat-api
322+
[34]: #logincallback
304323

305-
[32]: #credentialsobj
324+
[35]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
306325

307-
[33]: #logincallback
326+
[36]: #optionsobj
308327

309-
[34]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean
328+
[37]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
310329

311-
[35]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
330+
[38]: #genericerrcb
312331

313-
[36]: #genericerrcb
332+
[39]: #errdatacb
314333

315-
[37]: #errdatacb
334+
[40]: https://www.memcachier.com/users/signup
316335

317-
[38]: https://www.memcachier.com/users/signup
336+
[41]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
318337

319-
[39]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
338+
[42]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
320339

321-
[40]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
340+
[43]: #apiobj
322341

323-
[41]: #apiobj
342+
[44]: https://github.com/Schmavery/facebook-chat-api/blob/master/DOCS.md#apisetoptionsoptions
324343

325-
[42]: https://nodejs.org/api/process.html
344+
[45]: https://nodejs.org/api/process.html
326345

327-
[43]: #retrylogincallback
346+
[46]: #retrylogincallback
328347

329-
[44]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
348+
[47]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number

src/login.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ const messenger = require("facebook-chat-api"); // Chat API
1212
const fs = require("fs");
1313
const ArgumentParser = require("argparse").ArgumentParser;
1414

15+
// Default behavior: minimal logging and auto-approve recent logins
16+
const defaultOptions = {
17+
"logLevel": "error",
18+
"forceLogin": true
19+
}
20+
1521
let mem;
1622

1723
/**
@@ -72,6 +78,14 @@ let mem;
7278
* [here](https://github.com/Schmavery/facebook-chat-api) for details)
7379
*/
7480

81+
/**
82+
* @typedef {Object} optionsObj
83+
* @description An object containing options to be passed to the underlying
84+
* facebook-chat-api instance on login (see
85+
* [here](https://github.com/Schmavery/facebook-chat-api/blob/master/DOCS.md#apisetoptionsoptions)
86+
* for details)
87+
*/
88+
7589
/**
7690
* Call this to initialize the login module and log into Facebook using
7791
* [facebook-chat-api](https://github.com/Schmavery/facebook-chat-api).
@@ -80,9 +94,12 @@ let mem;
8094
* @param {credentialsObj} credentials
8195
* @param {loginCallback} callback called after login completed (successfully or unsuccessfully)
8296
* @param {Boolean} [forceCreds=false] if true, forces a login with credentials even if
83-
* appstate exists (optional)
97+
* appstate exists
98+
* @param {optionsObj} [options=defaultOptions] any options you wish to pass to the API on login;
99+
* by default, sets `logLevel` to `error` and `forceLogin` to `true` (auto-approves errors asking
100+
* for approval of recent logins for simplicity)
84101
*/
85-
exports.login = (credentials, callback, forceCreds = false) => {
102+
exports.login = (credentials, callback, forceCreds = false, options = defaultOptions) => {
86103
// Initialize mem variable for external storage API (Memcachier)
87104
mem = require("memjs").Client.create(credentials.MEMCACHIER_SERVERS, {
88105
"username": credentials.MEMCACHIER_USERNAME,
@@ -94,7 +111,7 @@ exports.login = (credentials, callback, forceCreds = false) => {
94111
console.log("Logging in with saved appstate...");
95112
messenger({
96113
appState: JSON.parse(appstate)
97-
}, (err, api) => {
114+
}, options, (err, api) => {
98115
if (err) {
99116
withCreds(callback);
100117
} else {
@@ -107,7 +124,7 @@ exports.login = (credentials, callback, forceCreds = false) => {
107124
messenger({
108125
email: credentials.FACEBOOK_EMAIL,
109126
password: credentials.FACEBOOK_PASSWORD
110-
}, (err, api) => {
127+
}, options, (err, api) => {
111128
if (err) return console.error(`Fatal error: failed login with credentials`);
112129

113130
mem.set("appstate", JSON.stringify(api.getAppState()), {}, merr => {

0 commit comments

Comments
 (0)