Skip to content

Commit

Permalink
fix: cannot like tweet (#48)
Browse files Browse the repository at this point in the history
* fix: cannot add tweet

* refactor: add return typ for parTwitterClient.likeTweet

* style: lint fix
  • Loading branch information
orimdominic authored May 24, 2021
1 parent 21aa1fe commit 91b47d5
Show file tree
Hide file tree
Showing 5 changed files with 5,490 additions and 14 deletions.
1 change: 0 additions & 1 deletion api/twitter-webhook-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default async (
res: VercelResponse
): Promise<VercelResponse> => {
const method = req.method?.toLowerCase();
console.log("method:", method)
switch (method) {
case "get": {
try {
Expand Down
2 changes: 2 additions & 0 deletions src/par-activity/cache.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint @typescript-eslint/no-var-requires: "off" */

import { createNodeRedisClient, WrappedNodeRedisClient } from "handy-redis";

let cache: WrappedNodeRedisClient;
Expand Down
1 change: 0 additions & 1 deletion src/par-activity/handle-tweet-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export async function handleTweetCreate(
any of the errors in one place and responds adequately. It also
kills the whole process on any error, which is what's needed
*/
// eslint-disable @typescript-eslint/no-unused-vars
const [count, engagement, selectionDate, _] = await Promise.all([
await service.getEngagementCount(mention.cmdText as string),
await service.getEngagementType(mention.cmdText as string),
Expand Down
38 changes: 26 additions & 12 deletions src/par-activity/par-twitter-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Twitter from "twitter-lite";
import { TwitterEndpoint, ITweet } from ".";
import request from "request";
import { promisify } from "util";
const post = promisify(request.post);

class ParTwitterClient {
private v1: Twitter;
Expand All @@ -9,20 +12,20 @@ class ParTwitterClient {
* Initialise the parameters for PAR account
*/
constructor() {
const params = {
const oauth = {
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.v1 = new Twitter({
...params,
...oauth,
});
this.v2 = new Twitter({
extension: false,
version: "2",
...params,
...oauth,
});
}

Expand All @@ -46,25 +49,36 @@ class ParTwitterClient {
return resp;
} catch (error) {
// TODO: handle error via sentry
console.error("parTwitterClient.replyMention", error);
console.error("parTwitterClient.replyMention", error, null, 2);
}
}

/**
* Like a tweet
* @param {string} id - The id of the tweet to be liked
*/
async likeTweet(id: string) {
console.log(id);
async likeTweet(id: string): Promise<{ data: { liked: boolean } }> {
try {
const resp = await this.v1.post(`favorites/create.json`, {
id
const resp = await post({
url: `https://api.twitter.com/2/users/${process.env.PICKATRANDOM_USERID}/likes`,
oauth: {
consumer_key: process.env.TWITTER_CONSUMER_KEY as string,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET as string,
token: process.env.TWITTER_PAR_ACCESS_TOKEN as string,
token_secret: process.env.TWITTER_PAR_ACCESS_TOKEN_SECRET as string,
},
json: {
tweet_id: id,
},
});
console.log(resp);
return resp;
return resp.body;
} catch (error) {
// TODO: handle error via sentry
console.error("parTwitterClient.likeTweet", JSON.stringify(error));
console.error(
// TODO: handle with sentry
"parTwitterClient.likeTweet",
JSON.stringify(error, null, 2)
);
return error;
}
}
}
Expand Down
Loading

1 comment on commit 91b47d5

@vercel
Copy link

@vercel vercel bot commented on 91b47d5 May 24, 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.