diff --git a/README.md b/README.md index cd9ebf6..92f4cdd 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,16 @@ # tweet-summary-bot -A twitter bot written in Typescript to check the most frequent words and sentiment analysis by a particular twitter user. Currently focused on English and Bahasa Indonesia. +A twitter bot written in Typescript to check the most frequent words and sentiment analysis by a particular twitter user. Currently focused on English and Bahasa Indonesia. -## How it works: -1. Reply to a tweet of the user that you want to analyze, and mention the user defined in the `TWITTER_ACCOUNT_TO_LISTEN` environment variable. -2. The account set with `TWITTER_API_KEY, TWITTER_API_SECRET_KEY, TWITTER_ACCESS_TOKEN_KEY, TWITTER_ACCESS_TOKEN_SECRET_KEY, TWITTER_BEARER_TOKEN` will reply to the your tweet with the information. +## How it works: + +1. Reply to a tweet of the user that you want to analyze, and mention the user defined in the `TWITTER_ACCOUNT_TO_LISTEN` environment variable. +2. The account set with `TWITTER_API_KEY, TWITTER_API_SECRET_KEY, TWITTER_ACCESS_TOKEN_KEY, TWITTER_ACCESS_TOKEN_SECRET_KEY, TWITTER_BEARER_TOKEN` will reply to the your tweet with the information. ![Screen Shot 2020-11-07 at 10 59 23](https://user-images.githubusercontent.com/10259593/98431468-4cec7b00-20e8-11eb-892d-380bc7a63364.png) ## Environment Variables + ```bash TWITTER_API_KEY=xxx TWITTER_API_SECRET_KEY=xxx @@ -17,14 +19,18 @@ TWITTER_ACCESS_TOKEN_SECRET_KEY=xxx TWITTER_BEARER_TOKEN=xxx TWITTER_ACCOUNT_TO_LISTEN=@sometwitterhandle GOOGLE_API_TRANSLATE_TARGET_LANGUAGE=en -GOOGLE_APPLICATION_CREDENTIALS=google-application-credentials/credentials.json +GOOGLE_APPLICATION_CREDENTIALS=google-application-credentials/credentials.json +HEALTHCHECK_PORT=3000 ``` -Notes: +Notes: + 1. More info about `GOOGLE_API_TRANSLATE_TARGET_LANGUAGE`: https://cloud.google.com/translate/docs/languages 2. More info about `GOOGLE_APPLICATION_CREDENTIALS`: https://cloud.google.com/docs/authentication/getting-started +3. You can do a periodic healthcheck on this service by hitting the http://0.0.0.0:3000/status endpoint. ## Running the app + ``` # development $ npm run start:dev diff --git a/src/index.ts b/src/index.ts index a01dea5..5f1dc3b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import "dotenv/config"; import { retrieveQuestion } from "./incoming"; import { sendAnswer } from "./outgoing"; +import { server } from "./utility/healthcheck"; import { logger } from "./utility/logger"; import { queueQuestion, queueSummary } from "./utility/queue"; import { generateSummary } from "./utility/twitter/tweet-utilities"; @@ -30,6 +31,11 @@ async function app() { } }, 1000); + server.listen(Number(process.env.HEALTHCHECK_PORT) || 3000); + server.on("listening", () => { + logger.info("Healthcheck is up"); + }); + logger.info("Service has been started!"); } diff --git a/src/utility/healthcheck.ts b/src/utility/healthcheck.ts new file mode 100644 index 0000000..abb83d3 --- /dev/null +++ b/src/utility/healthcheck.ts @@ -0,0 +1,18 @@ +import { createServer, IncomingMessage, ServerResponse } from "http"; + +export const server = createServer( + (request: IncomingMessage, response: ServerResponse) => { + if (request.url === "/status") { + if (request.method === "GET") { + response.end( + JSON.stringify({ + status: "up", + }) + ); + } + } else { + response.statusCode = 404; + response.end(); + } + } +);