-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
746 lines (648 loc) · 25 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
// Essential imports only
import fetch from 'node-fetch';
import { getSourceTweets } from './kv.js';
import { storeRecentPost } from './replies.js';
// HTML processing functions
function decodeHtmlEntities(text) {
const entities = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
''': "'",
' ': ' ',
'–': '-',
'—': '--',
'…': '...',
'™': 'TM',
'©': '(c)',
'®': '(R)',
'°': 'degrees',
'±': '+/-',
'¶': '(P)',
'§': '(S)',
'“': '"',
'”': '"',
'‘': "'",
'’': "'",
'«': '<<',
'»': '>>',
'×': 'x',
'÷': '/',
'¢': 'c',
'£': 'GBP',
'€': 'EUR',
'•': '*'
};
return text.replace(/&[^;]+;/g, entity => entities[entity] || '');
}
function stripHtmlTags(text) {
// First replace common block elements with space for better sentence separation
text = text
.replace(/<\/(p|div|br|h[1-6]|li)>/gi, ' ')
.replace(/<(p|div|br|h[1-6]|li)[^>]*>/gi, ' ');
// Then remove all remaining HTML tags
text = text.replace(/<[^>]+>/g, '');
// Clean up excessive whitespace
return text.replace(/\s+/g, ' ').trim();
}
// Utility Functions
function debug(message, level = 'info', data = null) {
const timestamp = new Date().toISOString();
const logMessage = `[${timestamp}] ${message}`;
// Always log to console
if (data) {
console.log(logMessage, data);
} else {
console.log(logMessage);
}
// Additional debug logging if enabled
if (process.env.DEBUG_MODE === 'true') {
if (level === 'error') {
console.error(logMessage, data || '');
} else if (level === 'warn') {
console.warn(logMessage, data || '');
}
}
}
// Configuration loader
async function loadConfig() {
const requiredVars = [
'MASTODON_API_URL',
'MASTODON_ACCESS_TOKEN',
'BLUESKY_API_URL',
'BLUESKY_USERNAME',
'BLUESKY_PASSWORD'
];
// Check for required environment variables
const missingVars = requiredVars.filter(varName => !process.env[varName]);
if (missingVars.length > 0) {
throw new Error(`Missing required environment variables: ${missingVars.join(', ')}`);
}
// Parse optional numeric parameters
const markovStateSize = parseInt(process.env.MARKOV_STATE_SIZE || '2', 10);
const markovMinChars = parseInt(process.env.MARKOV_MIN_CHARS || '30', 10);
const markovMaxChars = parseInt(process.env.MARKOV_MAX_CHARS || '280', 10);
const markovMaxTries = parseInt(process.env.MARKOV_MAX_TRIES || '100', 10);
// Parse optional array parameters
const mastodonSourceAccounts = process.env.MASTODON_SOURCE_ACCOUNTS
? process.env.MASTODON_SOURCE_ACCOUNTS.split(',').map(a => a.trim())
: ['Mastodon.social'];
const blueskySourceAccounts = process.env.BLUESKY_SOURCE_ACCOUNTS
? process.env.BLUESKY_SOURCE_ACCOUNTS.split(',').map(a => a.trim())
: ['bsky.social'];
// Parse optional string parameters
const excludedWords = process.env.EXCLUDED_WORDS
? process.env.EXCLUDED_WORDS.split(',').map(w => w.trim())
: [];
// Create configuration object
CONFIG = {
debug: process.env.DEBUG_MODE === 'true',
mastodon: {
url: process.env.MASTODON_API_URL,
token: process.env.MASTODON_ACCESS_TOKEN
},
bluesky: {
service: process.env.BLUESKY_API_URL,
identifier: process.env.BLUESKY_USERNAME,
password: process.env.BLUESKY_PASSWORD
},
markovStateSize,
markovMinChars,
markovMaxChars,
markovMaxTries,
mastodonSourceAccounts,
blueskySourceAccounts,
excludedWords
};
// Duplicate logging
// debug('Configuration loaded', 'info', {
// markovConfig: {
// stateSize: CONFIG.markovStateSize,
// minChars: CONFIG.markovMinChars,
// maxChars: CONFIG.markovMaxChars,
// maxTries: CONFIG.markovMaxTries
// },
// mastodonAccounts: CONFIG.mastodonSourceAccounts,
// blueskyAccounts: CONFIG.blueskySourceAccounts,
// excludedWords: CONFIG.excludedWords
// });
return CONFIG;
}
// Global config object
let CONFIG = null;
function cleanText(text) {
if (!text || typeof text !== 'string') {
return '';
}
// First strip HTML tags
text = stripHtmlTags(text);
// Then decode HTML entities
text = decodeHtmlEntities(text);
// Remove control characters and normalize whitespace
text = text
// eslint-disable-next-line no-control-regex
.replace(/[\u0000-\u001F\u007F-\u009F]/g, '') // Remove control characters
.replace(/\s+/g, ' ')
.trim();
// Enhanced URL and mention removal
text = text
// Remove all common URL patterns including bare domains
.replace(/(?:https?:\/\/)?(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_+.~#?&//=]*)/gi, '')
// Remove any remaining URLs that might have unusual characters
.replace(/\b(?:https?:\/\/|www\.)\S+/gi, '')
// Remove bare domains (e.g., example.com)
.replace(/\b[a-zA-Z0-9][a-zA-Z0-9-]*\.[a-zA-Z]{2,}\b/gi, '')
// Remove mentions (@username) - handle various formats including dots and Unicode
.replace(/@[a-zA-Z0-9_\u0080-\uFFFF](?:[a-zA-Z0-9_\u0080-\uFFFF.-]*[a-zA-Z0-9_\u0080-\uFFFF])?/g, '')
// Remove mention prefixes (e.g., ".@username" or ". @username")
.replace(/(?:^|\s)\.\s*@\w+/g, '')
// Remove RT pattern and any following mentions
.replace(/^RT\b[^a-zA-Z]*(?:@\w+[^a-zA-Z]*)*/, '')
// Remove any remaining colons after mentions
.replace(/(?:^|\s)@\w+:\s*/g, ' ')
// Clean up punctuation and whitespace, including leading dots
.replace(/[:\s]+/g, ' ')
.replace(/^\.\s+/, '')
.trim();
// Remove excluded words
if (CONFIG && CONFIG.excludedWords && CONFIG.excludedWords.length > 0) {
const excludedWordsRegex = new RegExp(`\\b(${CONFIG.excludedWords.join('|')})\\b`, 'gi');
text = text.replace(excludedWordsRegex, '').replace(/\s+/g, ' ').trim();
}
// Final cleanup of any remaining special characters
text = text
// eslint-disable-next-line no-control-regex
.replace(/[\u0000-\u001F\u007F-\u009F]/g, '') // Remove control characters
.replace(/\s+/g, ' ')
.trim();
return text;
}
// Markov Chain Implementation
class MarkovChain {
constructor(stateSize = 2) {
this.stateSize = stateSize;
this.chain = new Map();
this.startStates = [];
}
async addData(texts) {
if (!Array.isArray(texts) || texts.length === 0) {
throw new Error('No valid training data found');
}
const validTexts = texts.filter(text => typeof text === 'string' && text.trim().length > 0);
if (validTexts.length === 0) {
throw new Error('No valid training data found');
}
for (const text of validTexts) {
const words = text.trim().split(/\s+/);
if (words.length < this.stateSize) continue;
for (let i = 0; i <= words.length - this.stateSize; i++) {
const state = words.slice(i, i + this.stateSize).join(' ');
const nextWord = words[i + this.stateSize];
if (!this.chain.has(state)) {
this.chain.set(state, []);
}
if (nextWord) {
this.chain.get(state).push(nextWord);
}
if (i === 0) {
this.startStates.push(state);
}
}
}
if (this.startStates.length === 0) {
throw new Error('No valid training data found');
}
}
async generate({ minChars = 100, maxChars = 280, maxTries = 100 } = {}) {
let attempt = 0;
while (attempt < maxTries) {
try {
const result = await this._generateOnce();
if (result.length >= minChars && result.length <= maxChars) {
return { string: result };
}
} catch (error) {
if (error.message === 'No training data available') {
throw error;
}
// Continue trying if it's just a generation issue
}
attempt++;
}
throw new Error('Failed to generate valid text within constraints');
}
_generateOnce() {
if (this.startStates.length === 0) {
throw new Error('No training data available');
}
const startState = this.startStates[Math.floor(Math.random() * this.startStates.length)];
let currentState = startState;
let result = startState;
let usedStates = new Set([startState]);
let shouldContinue = true;
while (shouldContinue) {
const possibleNextWords = this.chain.get(currentState);
if (!possibleNextWords || possibleNextWords.length === 0) {
shouldContinue = false;
continue;
}
// Shuffle possible next words to increase variation
const shuffledWords = [...possibleNextWords].sort(() => Math.random() - 0.5);
let foundNew = false;
for (const nextWord of shuffledWords) {
const newState = result.split(/\s+/).slice(-(this.stateSize - 1)).concat(nextWord).join(' ');
if (!usedStates.has(newState)) {
result += ' ' + nextWord;
currentState = newState;
usedStates.add(newState);
foundNew = true;
break;
}
}
if (!foundNew) break;
}
return result;
}
}
// Content Management
async function fetchSourceTweets(env) {
try {
if (env && env.SOURCE_TWEETS) {
// In worker environment, fetch from KV
const tweets = await getSourceTweets(env);
if (tweets.length > 0) {
return tweets.map(cleanText);
}
debug('No tweets found in KV storage', 'warn');
return [];
} else {
// Local development: try to fetch from file
try {
const sourceTweetsResponse = await fetch('assets/source-tweets.txt');
if (!sourceTweetsResponse.ok) {
debug('Failed to fetch source tweets from file', 'error');
return [];
}
const content = await sourceTweetsResponse.text();
return content.split('\n')
.map(line => line.trim())
.filter(line => line.length > 0)
.map(cleanText);
} catch (error) {
debug('Error reading source tweets file:', 'error', error);
return [];
}
}
} catch (error) {
debug('Error in fetchSourceTweets:', 'error', error);
return [];
}
}
async function fetchTextContent(env) {
// Fetch both recent posts and source tweets
const [posts, sourceTweets] = await Promise.all([
fetchRecentPosts(),
fetchSourceTweets(env)
]);
// debug(`Fetched ${posts.length} posts from social media`, 'info');
debug(`Fetched ${sourceTweets.length} tweets from source file`, 'info');
return [...posts, ...sourceTweets];
}
async function fetchRecentPosts() {
try {
const posts = [];
// Log source accounts
debug(`Fetching posts from Bluesky accounts:\n ${CONFIG.blueskySourceAccounts.join('\n - ')}`, 'info');
debug(`Fetching posts from Mastodon accounts:\n ${CONFIG.mastodonSourceAccounts.join('\n - ')}`, 'info');
try {
// Fetch from Mastodon
const mastodonResponse = await fetch(`${CONFIG.mastodon.url}/api/v1/timelines/public`, {
headers: {
'Authorization': `Bearer ${CONFIG.mastodon.token}`,
'Accept': 'application/json'
}
});
if (!mastodonResponse.ok) {
const errorData = await mastodonResponse.json();
debug('Mastodon API error', 'error', errorData);
throw new Error(`Mastodon API error: ${errorData.error || 'Unknown error'}`);
}
const mastodonData = await mastodonResponse.json();
if (Array.isArray(mastodonData)) {
// debug(`Retrieved ${mastodonData.length} posts from Mastodon`, 'verbose');
const mastodonPosts = mastodonData
.filter(post => post && post.content)
.map(post => {
const cleanedText = cleanText(post.content);
// debug(`Mastodon post: ${cleanedText}`, 'verbose');
return cleanedText;
})
.filter(text => text.length > 0);
debug(`Processed ${mastodonPosts.length} valid Mastodon posts`, 'verbose');
posts.push(...mastodonPosts);
} else {
debug('Unexpected Mastodon API response format', 'error', mastodonData);
}
} catch (error) {
debug(`Error fetching Mastodon posts: ${error.message}`, 'error');
}
try {
// Get Bluesky auth token
const blueskyToken = await getBlueskyAuth();
if (!blueskyToken) {
debug('Skipping Bluesky fetch due to authentication failure', 'error');
} else {
// Fetch from Bluesky
const blueskyResponse = await fetch(`${CONFIG.bluesky.service}/xrpc/app.bsky.feed.getTimeline`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${blueskyToken}`
}
});
const blueskyData = await blueskyResponse.json();
if (blueskyData && blueskyData.feed && Array.isArray(blueskyData.feed)) {
// debug(`Retrieved ${blueskyData.feed.length} posts from Bluesky`, 'verbose');
const blueskyPosts = blueskyData.feed
.filter(item => item && item.post && item.post.record && item.post.record.text)
.map(item => {
const cleanedText = cleanText(item.post.record.text);
// debug(`Bluesky post: ${cleanedText}`, 'verbose');
return cleanedText;
})
.filter(text => text.length > 0);
debug(`Processed ${blueskyPosts.length} valid Bluesky posts`, 'verbose');
posts.push(...blueskyPosts);
} else {
debug('Unexpected Bluesky API response format', 'error', blueskyData);
}
}
} catch (error) {
debug(`Error fetching Bluesky posts: ${error.message}`, 'error');
}
const validPosts = posts.filter(text => text && text.length > 0);
debug(`Successfully fetched ${validPosts.length} total posts from social media`, 'info');
// Add fallback content if no posts were fetched
if (validPosts.length === 0) {
debug('No posts fetched, using fallback content', 'info');
validPosts.push(
'Hello world! This is a test post.',
'The quick brown fox jumps over the lazy dog.',
'To be, or not to be, that is the question.',
'All that glitters is not gold.',
'A journey of a thousand miles begins with a single step.'
);
}
return validPosts;
} catch (error) {
debug(`Error in fetchRecentPosts: ${error.message}`, 'error');
return [];
}
}
async function getBlueskyAuth() {
try {
const username = process.env.BLUESKY_USERNAME;
const password = process.env.BLUESKY_PASSWORD;
const apiUrl = process.env.BLUESKY_API_URL || 'https://bsky.social';
if (!username || !password) {
debug('Missing Bluesky credentials', 'error');
return null;
}
debug('Authenticating with Bluesky using:', 'info', username);
debug('Using API URL:', 'info', apiUrl);
debug('Sending Bluesky auth request...', 'info');
const response = await fetch(`${apiUrl}/xrpc/com.atproto.server.createSession`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
identifier: username,
password: password
})
});
debug('Auth response status:', 'info', {
status: response.status,
statusText: response.statusText
});
if (!response.ok) {
debug('Bluesky auth failed', 'error', {
status: response.status,
statusText: response.statusText
});
return null;
}
const data = await response.json();
debug('Successfully authenticated with Bluesky', 'info', {
did: data.did,
hasAccessJwt: !!data.accessJwt,
hasRefreshJwt: !!data.refreshJwt
});
return {
did: data.did,
accessJwt: data.accessJwt,
refreshJwt: data.refreshJwt
};
} catch (error) {
debug('Error authenticating with Bluesky:', 'error', error);
return null;
}
}
// Post Generation
async function generatePost(content) {
if (!Array.isArray(content) || content.length === 0) {
throw new Error('Content array is empty');
}
const validContent = content.filter(text => typeof text === 'string' && text.trim().length > 0);
if (validContent.length === 0) {
throw new Error('Content array is empty');
}
try {
const markov = new MarkovChain(CONFIG.markovStateSize);
await markov.addData(validContent);
return await markov.generate({
minChars: CONFIG.markovMinChars,
maxChars: CONFIG.markovMaxChars,
maxTries: CONFIG.markovMaxTries
});
} catch (error) {
debug(`Error generating Markov chain: ${error.message}`, 'error');
throw new Error(error.message);
}
}
// Social Media Integration
async function postToMastodon(content) {
try {
// Check if we're in debug mode
if (process.env.DEBUG_MODE === 'true') {
debug('Debug mode: Would post to Mastodon:', 'info', {
content,
platform: 'mastodon'
});
return true;
}
const response = await fetch(`${process.env.MASTODON_API_URL}/api/v1/statuses`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.MASTODON_ACCESS_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
status: content,
visibility: 'public'
})
});
if (!response.ok) {
const errorData = await response.text();
debug('Failed to post to Mastodon', 'error', {
status: response.status,
statusText: response.statusText,
error: errorData
});
throw new Error(`Failed to post to Mastodon: ${errorData}`);
}
const data = await response.json();
debug('Post created successfully', 'info', {
id: data.id,
url: data.url
});
// Store the post in our cache using the numeric ID
try {
await storeRecentPost('mastodon', data.id, content);
debug('Post stored in cache', 'info', {
id: data.id,
content: content.substring(0, 50) + '...'
});
} catch (error) {
debug('Error storing post:', 'error', error);
// Continue even if storage fails - we don't want to fail the post
}
return true;
} catch (error) {
debug('Error posting to Mastodon:', 'error', error);
return false;
}
}
async function postToBluesky(content) {
try {
// Check if we're in debug mode
if (process.env.DEBUG_MODE === 'true') {
debug('Debug mode: Would post to Bluesky:', 'info', {
content,
platform: 'bluesky'
});
return true;
}
// Get auth token
const auth = await getBlueskyAuth();
if (!auth || !auth.accessJwt || !auth.did) {
throw new Error('Failed to authenticate with Bluesky');
}
debug('Posting to Bluesky', 'info', {
authenticated: true,
did: auth.did
});
const response = await fetch(`${process.env.BLUESKY_API_URL}/xrpc/com.atproto.repo.createRecord`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${auth.accessJwt}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
repo: auth.did,
collection: 'app.bsky.feed.post',
record: {
text: content,
createdAt: new Date().toISOString()
}
})
});
if (!response.ok) {
const errorData = await response.text();
debug('Failed to post to Bluesky', 'error', {
status: response.status,
statusText: response.statusText,
error: errorData
});
throw new Error(`Failed to post to Bluesky: ${errorData}`);
}
const data = await response.json();
debug('Post created successfully', 'info', { uri: data.uri });
// Store the post in our cache
try {
await storeRecentPost('bluesky', data.uri, content);
debug('Post stored in cache', 'info', { uri: data.uri });
} catch (error) {
debug('Error storing post:', 'error', error);
// Continue even if storage fails - we don't want to fail the post
}
return true;
} catch (error) {
debug('Error posting to Bluesky:', 'error', error);
return false;
}
}
async function postToSocialMedia(content) {
try {
const results = await Promise.allSettled([
postToMastodon(content),
postToBluesky(content)
]);
let success = false;
// Check Mastodon result
if (results[0].status === 'fulfilled' && results[0].value) {
debug('Successfully posted to Mastodon', 'essential');
success = true;
} else {
const error = results[0].reason || 'Unknown error';
debug(`Failed to post to Mastodon: ${error}`, 'error');
}
// Check Bluesky result
if (results[1].status === 'fulfilled' && results[1].value) {
debug('Successfully posted to Bluesky', 'essential');
success = true;
} else {
const error = results[1].reason || 'Unknown error';
debug(`Failed to post to Bluesky: ${error}`, 'error');
}
if (!success) {
debug('Failed to post to any platform', 'error');
return false;
}
return true;
} catch (error) {
debug(`Error in postToSocialMedia: ${error.message}`, 'error');
return false;
}
}
// Main Execution
async function main(env) {
try {
// Load configuration
CONFIG = await loadConfig();
debug('Configuration loaded', 'info', CONFIG);
// 30% chance to post
const randomValue = Math.random();
debug(`Random value generated: ${(randomValue * 100).toFixed(2)}%`, 'info');
if (randomValue > 0.3) {
debug('Skipping post based on random chance (above 30% threshold)', 'info');
return;
}
debug('Proceeding with post (within 30% threshold)', 'info');
// Fetch content for generation
const content = await fetchTextContent(env);
if (!content || content.length === 0) {
debug('No content available for generation', 'error');
return;
}
// Generate and post content
const post = await generatePost(content);
if (post) {
await postToSocialMedia(post.string);
}
} catch (error) {
debug('Error in main execution:', 'error', error);
}
}
// Export for worker
export { debug, main, MarkovChain, generatePost, loadConfig, cleanText, getBlueskyAuth };