diff --git a/src/interfaces/summary.interface.ts b/src/interfaces/summary.interface.ts new file mode 100644 index 0000000..fa2a469 --- /dev/null +++ b/src/interfaces/summary.interface.ts @@ -0,0 +1,7 @@ +import { google } from "@google-cloud/language/build/protos/protos"; + +export interface Summary { + wordFrequency: [string, number][]; + replyToStatusId: string; + sentiment: google.cloud.language.v1.IAnalyzeSentimentResponse; +} diff --git a/src/outgoing.ts b/src/outgoing.ts index f1214fd..17e16e9 100644 --- a/src/outgoing.ts +++ b/src/outgoing.ts @@ -1,16 +1,27 @@ +import { Summary } from "./interfaces/summary.interface"; import { logger } from "./utility/logger"; import { twitterClient } from "./utility/twitter/twitter-client"; -export async function sendAnswer(summary: any): Promise { +export async function sendAnswer(summary: Summary): Promise { const client = twitterClient(); const status = summary.wordFrequency .splice(0, 5) - .map((element: any) => element[0]) + .map((element: [string, number]) => element[0]) .join(", "); + if (summary.sentiment.documentSentiment === null) { + throw new Error("documentSentiment is null. "); + } + if (summary.sentiment.documentSentiment === undefined) { + throw new Error("documentSentiment is undefined. "); + } const sentimentScore = summary.sentiment.documentSentiment.score || 0; - const sentimentScoreRounded = Number.parseFloat(sentimentScore).toFixed(2); + const sentimentScoreRounded = Number.parseFloat( + String(sentimentScore) + ).toFixed(2); const magnitudeScore = summary.sentiment.documentSentiment.magnitude || 0; - const magnitudeScoreRounded = Number.parseFloat(magnitudeScore).toFixed(2); + const magnitudeScoreRounded = Number.parseFloat( + String(magnitudeScore) + ).toFixed(2); let sentimentScoreString = "Neutral"; if (sentimentScore <= -0.25 && sentimentScore >= -1) { sentimentScoreString = "Negative"; diff --git a/src/utility/queue.ts b/src/utility/queue.ts index 0d1d1b6..99f3d97 100644 --- a/src/utility/queue.ts +++ b/src/utility/queue.ts @@ -1,5 +1,6 @@ +import { Summary } from "../interfaces/summary.interface"; import { Tweet } from "../interfaces/twitter/tweet.interface"; export const queueQuestion: Tweet[] = []; -export const queueSummary: any[] = []; -export const queueReport: any[] = []; +export const queueSummary: Summary[] = []; +export const queueReport: unknown[] = []; diff --git a/src/utility/twitter/tweet-utilities.ts b/src/utility/twitter/tweet-utilities.ts index 6f37690..53f4e6d 100644 --- a/src/utility/twitter/tweet-utilities.ts +++ b/src/utility/twitter/tweet-utilities.ts @@ -1,5 +1,7 @@ import sw from "stopword"; import TextCleaner from "text-cleaner"; +import { Summary } from "../../interfaces/summary.interface"; +import { Tweet } from "../../interfaces/twitter/tweet.interface"; import { WordFrequency } from "../../interfaces/word-frequency.interface"; import { axiosClient } from "../axios-retry-configuration"; import { analyseSentiment } from "../gcp/sentiment"; @@ -10,7 +12,9 @@ import { regexTwitterHandle, regexURL } from "../regex"; const token = process.env.TWITTER_BEARER_TOKEN || ""; const recentSearchURL = "https://api.twitter.com/2/tweets/search/recent"; -export async function recentSearch(username: string) { +export async function recentSearch( + username: string +): Promise<{ length: number; data: [] }> { // Edit query parameters below const parameters = { query: `from:${username} -is:retweet`, @@ -30,17 +34,19 @@ export async function recentSearch(username: string) { return response.data; } -export async function generateWordFrequency(words: string[]) { +export async function generateWordFrequency( + words: string[] +): Promise<[string, number][]> { if (words.length === 0) { - return; + throw new Error("The received words length is zero. "); } const wordFrequency: WordFrequency = {}; words.forEach((element: string) => { // Keep track of occurences if (Object.prototype.hasOwnProperty.call(wordFrequency, element)) { - wordFrequency[element] += 1; + wordFrequency[String(element)] += 1; } else { - wordFrequency[element] = 1; + wordFrequency[String(element)] = 1; } }); @@ -53,13 +59,13 @@ export async function generateWordFrequency(words: string[]) { }); } -export async function generateWordsArray(tweets: any) { - if (tweets.length === 0) { - return; - } +export async function generateWordsArray(tweets: { + length: number; + data: []; +}): Promise { let recentTweetsConcat = ""; if (tweets["data"]) { - tweets["data"].forEach((element: any) => { + tweets["data"].forEach((element: { text: string }) => { if (element.text) recentTweetsConcat = recentTweetsConcat + " " + element.text; }); @@ -82,7 +88,9 @@ export async function generateWordsArray(tweets: any) { ]); } -export async function generateSummary(question: any) { +export async function generateSummary( + question: Tweet +): Promise { try { const recentTweets = await recentSearch(question.in_reply_to_user_id_str); const wordsArray = await generateWordsArray(recentTweets);