-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet.js
71 lines (62 loc) · 1.57 KB
/
tweet.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
import OAuth from 'oauth-1.0a'
import { createHmac } from 'crypto'
import needle from 'needle'
import dotenv from 'dotenv'
dotenv.config()
function tweet(prompt, tweet_id, media_id) {
const oauth = OAuth({
consumer: {
key: process.env.TWITTER_API_KEY,
secret: process.env.TWITTER_API_KEY_SECRET
},
signature_method: 'HMAC-SHA',
hash_function(base_string, key) {
return createHmac('sha1', key)
.update(base_string)
.digest('base64')
},
})
const token = {
key: process.env.TWITTER_ACCESS_TOKEN,
secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
}
let body;
if(media_id === null) {
body = JSON.stringify({
"text": `Error (from DALL-E): ${prompt} \nTry some different prompt.`,
"reply": { "in_reply_to_tweet_id": `${tweet_id}` },
});
} else {
body = JSON.stringify({
"text": `Image for prompt: ${prompt}, \nFollow me: https://twitter.com/GenesysTheBot`,
"reply": { "in_reply_to_tweet_id": `${tweet_id}` },
"media": {
"media_ids": [`${media_id}`]
}
});
}
const authHeader = oauth.toHeader(oauth.authorize({
url: 'https://api.twitter.com/2/tweets',
method: 'POST',
}, token));
const options = {
headers: {
'Authorization': authHeader["Authorization"],
'Content-Type': 'application/json',
}
};
needle.post('https://api.twitter.com/2/tweets', body, options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
/*
response
{
"body": {
"id": "1445880548472328192",
"text": "Are you excited for the weekend?"
}
}
*/
})
}
export { tweet };