-
Notifications
You must be signed in to change notification settings - Fork 1
Integration: Twitter
Example of sending message to users as direct message.
const T = require("./Twit.js");
const my_user_name = require("../config").userName;
const timeout = 1000 * 60 * 5; // timeout to send the message 5 min
const AutoDM = () => {
const stream = T.stream("user");
console.log("Start Sending Auto Direct Message πππ");
stream.on("follow", SendMessage);
};
const SendMessage = user => {
const { screen_name, name } = user.source;
const obj = {
screen_name,
text: GenerateMessage(name)
};
// the follow stream track if I follow author person too.
if (screen_name != my_user_name) {
console.log(" ππππ New Follower πππππ ");
setTimeout(() => {
T.post("direct_messages/new", obj)
.catch(err => {
console.error("error", err.stack);
})
.then(result => {
console.log(`Message sent successfully To ${screen_name} πͺπͺ`);
});
}, timeout);
}
};
const GenerateMessage = name => {
const days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
];
const d = new Date();
const dayName = days[d.getDay()];
return `Hi ${name} Thanks for .... \n Happy ${dayName} ππ `; // your message
};
module.exports = AutoDM;
-
Visit https://developer.twitter.com/content/developer-twitter/en.html and create a Twitter's developer's account (you can get a developer's account using your normal twitter account).
-
After getting access to Twitter developer's account, now create an application by visiting this page : https://developer.twitter.com/en/apps.
-
After creating the app, there will be a tab named "Keys and Access Tokens", scroll down and click Create my access token. Thatβs it, now you should have your Consumer Key and Consumer Secret, as well as your Access Token and Access Token Secret.
-
Store these newly generated keys somewhere. In future, they will be entered in our app as environment variables because directly typing the api keys in source code is discouraged (security issues).
-
Create an AWS lambda function to handle messages.
-
Use AWS API Gateway to create an HTTP endpoint for your lambda function.
-
Connect your twitter bot to your AWS HTTP endpoint.