-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
149 lines (123 loc) · 3.1 KB
/
index.ts
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
import AtProto from '@atproto/api';
import { cborDecodeMulti, cborDecode } from '@atproto/common';
import WebSocket from 'ws';
import { CarReader } from '@ipld/car/reader';
import process from 'process';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const VAMPETAS_DIR = path.join(__dirname, 'vampetas');
const agent = new AtProto.BskyAgent({
service: 'https://bsky.social',
});
/**
* Bot credentials.
* TODO: Change to custom handle.
*/
await agent.login({
identifier: process.env.BLUESKY_USERNAME!,
password: process.env.BLUESKY_PASSWORD!,
});
function getRandomVampetaImage(): Uint8Array {
const files = fs.readdirSync(VAMPETAS_DIR);
const images = files.filter((file) => (
/**
* Bluesky only supports PNG and JPG images.
*/
/\.(png|jpg)$/i.test(file)
));
const n = Math.floor(Math.random() * images.length);
const imagePath = images[n];
const image = fs.readFileSync(
path.join(VAMPETAS_DIR, imagePath),
);
return new Uint8Array(image);
}
type Payload =
& AtProto.ComAtprotoSyncSubscribeRepos.Commit
& {
ops?: AtProto.ComAtprotoSyncSubscribeRepos.RepoOp[],
};
const ws = new WebSocket(
'wss://bsky.network/xrpc/com.atproto.sync.subscribeRepos'
);
/**
* TODO: Needs to add error and close event handlers.
*/
ws.on('message', async (data: Uint8Array) => {
const [, payload] = cborDecodeMulti(
data,
) as [unknown, Payload];
try {
const {
ops,
blocks,
repo,
} = payload;
if (!Array.isArray(ops)) {
return;
}
const [op] = ops;
if (op?.action !== 'create') {
return;
}
const cr = await CarReader.fromBytes(blocks);
const block = await cr.get(op.cid);
if (!block?.bytes) {
return;
}
const post = cborDecode(
block.bytes,
) as AtProto.AppBskyFeedPost.Record;
/**
* Only replies to posts that contain text the and
* belongs to a thread.
*/
const isNotTextNorReply = !post?.text && !post?.reply;
if (isNotTextNorReply) {
return;
}
const hasVampetacoHashTag = /#vampeta(ç|c)o/gmi.test(post.text);
if (!hasVampetacoHashTag) {
return;
}
const { data: { blob: image } } = await agent.uploadBlob(
getRandomVampetaImage(),
{ encoding: 'image/png' },
);
const createdAt = new Date().toISOString();
const uri = `at://${repo}/${op.path}`;
const reply = {
createdAt,
text: '',
reply: {
/**
* Reply to the post where the hashtag was found.
*/
root: post.reply!.root,
parent: {
uri,
cid: op.cid.toString(),
},
},
embed: {
$type: 'app.bsky.embed.images',
images: [
{
image,
alt: 'Foto do jogador Vampeta em momentos duvidosos.'
},
],
},
};
await agent.post(reply);
/**
* Just for debugging purposes.
*/
console.log(reply);
} catch (exception) {
console.error(exception);
}
});