generated from nhcarrigan/template
feat: use json schema to get all announcements
This commit is contained in:
@@ -11,7 +11,7 @@ import { TwitterApi } from "twitter-api-v2";
|
||||
* @param content - The main body of the announcement.
|
||||
* @returns A message indicating the success or failure of the operation.
|
||||
*/
|
||||
export const announceOnTwitter = async(content: string): Promise<string> => {
|
||||
export const announceOnTwitter = async(content: Array<string>): Promise<string> => {
|
||||
if (
|
||||
process.env.TWITTER_CONSUMER_KEY === undefined
|
||||
|| process.env.TWITTER_CONSUMER_SECRET === undefined
|
||||
@@ -27,8 +27,13 @@ export const announceOnTwitter = async(content: string): Promise<string> => {
|
||||
appSecret: process.env.TWITTER_CONSUMER_SECRET,
|
||||
});
|
||||
|
||||
const [ firstPost, ...restOfPosts ] = content;
|
||||
const failedReplies: Array<string> = [];
|
||||
if (firstPost === undefined) {
|
||||
return "No posts to send to Twitter.";
|
||||
}
|
||||
const result = await twitterClient.v2.
|
||||
tweet(content).
|
||||
tweet(firstPost).
|
||||
catch((error: unknown) => {
|
||||
return error instanceof Error
|
||||
? error.message
|
||||
@@ -37,5 +42,18 @@ export const announceOnTwitter = async(content: string): Promise<string> => {
|
||||
if (typeof result === "string") {
|
||||
return `Failed to send message to Twitter. ${result}`;
|
||||
}
|
||||
return "Successfully sent message to Twitter.";
|
||||
let { id } = result.data;
|
||||
for (const post of restOfPosts) {
|
||||
// eslint-disable-next-line no-await-in-loop -- We need to do this sequentially.
|
||||
const twitterResponse = await twitterClient.v2.reply(post, id);
|
||||
if (typeof twitterResponse !== "string") {
|
||||
const { id: replyId } = twitterResponse.data;
|
||||
id = replyId;
|
||||
continue;
|
||||
}
|
||||
failedReplies.push(post);
|
||||
}
|
||||
return `Successfully sent initial post to Twitter. ${failedReplies.length > 0
|
||||
? `Failed to send ${failedReplies.length.toString()} replies: ${failedReplies.join(", ")}`
|
||||
: `All ${(content.length - 1).toString()} replies were sent successfully.`}`;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user