Skip to content

Commit

Permalink
feat: reply functionality baked in (#21)
Browse files Browse the repository at this point in the history
* refactor: set pick at random reply fn

* refactor: PAR can reply `real` mentions

* fix: style and build
  • Loading branch information
orimdominic authored May 10, 2021
1 parent 123416d commit 9ec2b11
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 24 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/sanity-check-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ jobs:
tests:
runs-on: ubuntu-latest
env:
TWITTER_CONSUMER_SECRET: "secret"
TEST_CRC_TOKEN: "crc_token"
TEST_CRC_RESPONSE: "sha256=gjiQ6uetgi4t5/+6PvFeAZ+idQ6rmuAfSEo3PnF6QV8="
TWITTER_CONSUMER_SECRET: ${{ secrets.TWITTER_CONSUMER_SECRET }}
TWITTER_CONSUMER_KEY: ${{ secrets.TWITTER_CONSUMER_KEY }}
TEST_CRC_TOKEN: ${{ secrets.TEST_CRC_TOKEN }}
TEST_CRC_RESPONSE: ${{ secrets.TEST_CRC_RESPONSE }}
PICKATRANDOM_SCREEN_NAME: ${{ secrets.PICKATRANDOM_SCREEN_NAME }}
PICKATRANDOM_USERID: ${{ secrets.PICKATRANDOM_USERID }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion api/twitter-webhook-listener.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { VercelRequest, VercelResponse } from "@vercel/node";
import { StatusCodes, getReasonPhrase } from "http-status-codes";
import { getChallengeResponse } from "../src/webhook-listener-helpers";
require("../src/utils/config");
require("../src/config");

export default async (
req: VercelRequest,
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
"dotenv": "^8.2.0",
"http-status-codes": "^2.1.4",
"node-twitter-api": "^1.8.0",
"request": "^2.88.2"
"request": "^2.88.2",
"twitter-error-handler": "^3.1.0",
"twitter-lite": "^1.1.0"
}
}
2 changes: 1 addition & 1 deletion src/bin/create-subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as readline from "readline";
const rl = readline.createInterface(process.stdin, process.stdout);
import * as util from "util";
import * as request from "request";
require("../utils/config");
require("../config");

const post = util.promisify(request.post);

Expand Down
2 changes: 1 addition & 1 deletion src/bin/delete-webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as readline from "readline";
const rl = readline.createInterface(process.stdin, process.stdout);
import * as util from "util";
import * as request from "request";
require("../utils/config");
require("../config");

const del = util.promisify(request.delete);

Expand Down
2 changes: 1 addition & 1 deletion src/bin/get-user-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* It is used to get a user's token for maybe, adding to a subscribe
* list
*/

require("../config");
import twitterAPI from "node-twitter-api";
import readline from "readline";
const rl = readline.createInterface(process.stdin, process.stdout);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/register-webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as readline from "readline";
const rl = readline.createInterface(process.stdin, process.stdout);
import * as util from "util";
import * as request from "request";
require("../utils/config");
require("../config");

const post = util.promisify(request.post);

Expand Down
13 changes: 6 additions & 7 deletions src/utils/config.ts → src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

import { config } from "dotenv";

const envMode = {
DEVELOPMENT: "development",
PRODUCTION: "production",
TEST: "test",
};
enum EnvMode {
Production = "production",
Test = "test",
}

const env = process.env.NODE_ENV || envMode.DEVELOPMENT;
const env = process.env.NODE_ENV;
switch (env) {
case envMode.PRODUCTION:
case EnvMode.Production:
config();
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getChallengeResponse } from "../index";

describe("getChallengeResponse", () => {
it("returns the correct challenge response from a crc token", () => {
require("../../utils/config");
require("../../config");
const testCrcToken = process.env.TEST_CRC_TOKEN as string;
const testCrcResponse = process.env.TEST_CRC_RESPONSE as string;
expect(getChallengeResponse(testCrcToken)).toBe(testCrcResponse);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require("../../utils/config");
require("../../config");

import {
ITweet,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require("../../utils/config");
require("../../config");
import { VercelResponse } from "@vercel/node";
import {
setPickAtRandomAccountActivityHandler,
Expand Down
12 changes: 8 additions & 4 deletions src/webhook-listener-helpers/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const CommandType = {
Cancel: "cancel",
Feedback: "feedback",
};
export enum CommandType {
Cancel = "cancel",
Feedback = "feedback",
}

export enum TwitterEndpoint {
StatusUpdate = "statuses/update",
}
1 change: 1 addition & 0 deletions src/webhook-listener-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from "./IDirectMessageEvent";
export * from "./get-challenge-response";
export * from "./handle-pick-at-random-activity";
export * from "./handle-pick-at-random-tweet-create-events";
export * from "./par-twitter-client";
51 changes: 51 additions & 0 deletions src/webhook-listener-helpers/par-twitter-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Twitter from "twitter-lite";
import { TwitterEndpoint } from ".";
import { ITweet } from "./ITweet";

class ParTwitterClient {
private v1: Twitter;
private v2: Twitter;

/**
* Initialise the parameters for PAR account
*/
constructor() {
this.v1 = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY as string,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET as string,
access_token_key: process.env.TWITTER_PAR_ACCESS_TOKEN as string,
access_token_secret: process.env
.TWITTER_PAR_ACCESS_TOKEN_SECRET as string,
});
this.v2 = new Twitter({
extension: false,
version: "2",
consumer_key: process.env.TWITTER_CONSUMER_KEY as string,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET as string,
access_token_key: process.env.TWITTER_PAR_ACCESS_TOKEN as string,
access_token_secret: process.env
.TWITTER_PAR_ACCESS_TOKEN_SECRET as string,
});
}

/**
* Reply/Acknowledge a real mention with a feedback of the computed
* result
* @param {string} id - The id of the mention to reply
* @param {string} message - The feedback message
* @param {string} author - The screen name of the author
* @returns {Promise<ITweet>} The tweeted feedback
*/
async replyMention(
id: string,
message: string,
author: string
): Promise<ITweet> {
return await this.v1.post(TwitterEndpoint.StatusUpdate, {
status: `@${author} ${message}`,
in_reply_to_status_id: id,
});
}
}

export const parTwitterClient = new ParTwitterClient();
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"lib": ["es2017"],
"lib": ["es2017", "DOM"], // https://github.com/draftbit/twitter-lite/issues/135
"forceConsistentCasingInFileNames": true,
"removeComments": true
},
Expand Down
30 changes: 30 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,13 @@ create-require@^1.1.0:
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==

cross-fetch@^3.0.0:
version "3.1.4"
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.4.tgz#9723f3a3a247bf8b89039f3a380a9244e8fa2f39"
integrity sha512-1eAtFWdIubi6T4XPy6ei9iUFoKpUkIF971QLN8lIvvvwueI65+Nw5haMNKUwfJxabqlIIDODJKGrQ66gxC0PbQ==
dependencies:
node-fetch "2.6.1"

cross-spawn@^6.0.0:
version "6.0.5"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4"
Expand Down Expand Up @@ -3643,6 +3650,11 @@ nice-try@^1.0.4:
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==

[email protected]:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==

node-int64@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
Expand Down Expand Up @@ -3762,6 +3774,11 @@ nwsapi@^2.2.0:
resolved "https://registry.yarnpkg.com/nwsapi/-/nwsapi-2.2.0.tgz#204879a9e3d068ff2a55139c2c772780681a38b7"
integrity sha512-h2AatdwYH+JHiZpv7pt/gSX1XoRGb7L/qSIeuqA6GwYoF9w1vP1cw42TO0aI2pNyshRK5893hNSl+1//vHK7hQ==

oauth-1.0a@^2.2.4:
version "2.2.6"
resolved "https://registry.yarnpkg.com/oauth-1.0a/-/oauth-1.0a-2.2.6.tgz#eadbccdb3bceea412d24586e6f39b2b412f0e491"
integrity sha512-6bkxv3N4Gu5lty4viIcIAnq5GbxECviMBeKR3WX/q87SPQ8E8aursPZUtsXDnxCs787af09WPRBLqYrf/lwoYQ==

oauth-sign@~0.9.0:
version "0.9.0"
resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455"
Expand Down Expand Up @@ -4948,6 +4965,19 @@ tweetnacl@^0.14.3, tweetnacl@~0.14.0:
resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64"
integrity sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q=

twitter-error-handler@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/twitter-error-handler/-/twitter-error-handler-3.1.0.tgz#d68dd03e6ad51141ee59757a7d3939baf2e5f66d"
integrity sha512-aCCrxYb0rQO18jdsD5via87YLhVoLnDZKn1RFD4DEoR0ZN5MM/2bV+FBR+WIZUdBMaW7lUjxun+vTQdwUTW6+g==

twitter-lite@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/twitter-lite/-/twitter-lite-1.1.0.tgz#77f250cc73f14ecc5a493ddb81ff0a3e9ae72680"
integrity sha512-v37I0zyYXjn3/zdm/ii+1RiBFZ2SCB3wLl1N5nfW8TUqoNDYBR66oXBFbQavKEvjg5WqcTUzNhAhe6FkgylPsw==
dependencies:
cross-fetch "^3.0.0"
oauth-1.0a "^2.2.4"

type-check@^0.4.0, type-check@~0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.4.0.tgz#07b8203bfa7056c0657050e3ccd2c37730bab8f1"
Expand Down

1 comment on commit 9ec2b11

@vercel
Copy link

@vercel vercel bot commented on 9ec2b11 May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.