Skip to content
This repository has been archived by the owner on Sep 5, 2020. It is now read-only.

Integration: Twitter

GSJawla edited this page Nov 25, 2018 · 9 revisions

Twitter Integration

Proof of Concept

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;

Integration Notes

  1. 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).

  2. After getting access to Twitter developer's account, now create an application by visiting this page : https://developer.twitter.com/en/apps.

  3. 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.

  4. 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).

  5. Create an AWS lambda function to handle messages.

  6. Use AWS API Gateway to create an HTTP endpoint for your lambda function.

  7. Connect your twitter bot to your AWS HTTP endpoint.