Skip to content

Commit

Permalink
Merge pull request #42 from anzharip/feat/healthcheck
Browse files Browse the repository at this point in the history
feat: add healthcheck endpoint
  • Loading branch information
anzharip authored Nov 22, 2020
2 parents d608f86 + 693c32b commit b02b86a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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!");
}

Expand Down
18 changes: 18 additions & 0 deletions src/utility/healthcheck.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
);

0 comments on commit b02b86a

Please sign in to comment.