-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter-bot.js
54 lines (46 loc) · 1.27 KB
/
twitter-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
const axios = require('axios');
const { TwitterApi } = require('twitter-api-v2');
require('dotenv').config();
const userClient = new TwitterApi({
appKey: process.env.API_KEY,
appSecret: process.env.API_SECRET,
accessToken: process.env.ACCESS_TOKEN,
accessSecret: process.env.ACCESS_SECRET
});
const getDadJoke = async () => {
const dadJokeOptions = {
method: 'GET',
url: 'https://dad-jokes.p.rapidapi.com/random/joke',
headers: {
'X-RapidAPI-Key': '4a704ee0bbmsh5351e16703a5e8ep18dd77jsnbca616c253f5',
'X-RapidAPI-Host': 'dad-jokes.p.rapidapi.com'
}
};
try {
const response = await axios.request(dadJokeOptions);
const responseObj = response.data;
return {
setup: responseObj.body[0].setup,
punchLine: responseObj.body[0].punchline
};
}
catch(error) {
console.log(error);
}
}
const postTweet = async () => {
const joke = await getDadJoke();
const tweet = `${joke.setup }\n${joke.punchLine}`;
try {
await userClient.v2.tweet(tweet);
}
catch (error) {
console.log(error);
}
}
exports.handler = async (event) => {
console.log('Lambda Function Triggered by Cloudwatch Event');
await postTweet();
console.log('Tweet Posted Successfully');
return 'Function Tweeted Successfully';
};