generated from nhcarrigan/template
feat: more automated announcements (#8)
### Explanation Makes my life so much easier. ### Issue _No response_ ### Attestations - [x] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [x] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [x] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [x] I have pinned the dependencies to a specific patch version. ### Style - [x] I have run the linter and resolved any errors. - [x] My pull request uses an appropriate title, matching the conventional commit standards. - [x] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [ ] My contribution adds new code, and I have added tests to cover it. - [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [ ] All new and existing tests pass locally with my changes. - [ ] Code coverage remains at or above the configured threshold. ### Documentation _No response_ ### Versioning Minor - My pull request introduces a new non-breaking feature. Reviewed-on: #8 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit was merged in pull request #8.
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
interface FacebookErrorResponse {
|
||||
error: {
|
||||
code: number;
|
||||
message: string;
|
||||
type: string;
|
||||
};
|
||||
}
|
||||
|
||||
interface FacebookSuccessResponse {
|
||||
id: string;
|
||||
}
|
||||
|
||||
type FacebookResponse = FacebookErrorResponse | FacebookSuccessResponse;
|
||||
|
||||
/**
|
||||
* Forwards an announcement to our Facebook Page.
|
||||
* @param content - The main body of the announcement.
|
||||
* @returns A message indicating the success or failure of the operation.
|
||||
*/
|
||||
export const announceOnFacebook = async(content: string): Promise<string> => {
|
||||
if (
|
||||
process.env.FACEBOOK_PAGE_TOKEN === undefined
|
||||
|| process.env.FACEBOOK_PAGE_ID === undefined
|
||||
) {
|
||||
return "Facebook credentials are not set.";
|
||||
}
|
||||
|
||||
if (content.trim().length === 0) {
|
||||
return "No content to send to Facebook.";
|
||||
}
|
||||
|
||||
const pageId = process.env.FACEBOOK_PAGE_ID;
|
||||
const accessToken = process.env.FACEBOOK_PAGE_TOKEN;
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://graph.facebook.com/v21.0/${pageId}/feed`,
|
||||
{
|
||||
body: new URLSearchParams({
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Facebook API requires snake_case.
|
||||
access_token: accessToken,
|
||||
message: content,
|
||||
}),
|
||||
headers: {
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- HTTP header name.
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
method: "POST",
|
||||
},
|
||||
);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- Fetch does not accept generic.
|
||||
const result = (await response.json()) as FacebookResponse;
|
||||
|
||||
if ("error" in result) {
|
||||
const errorMessage = result.error.message === ""
|
||||
? JSON.stringify(result.error)
|
||||
: result.error.message;
|
||||
return `Failed to send message to Facebook. ${errorMessage}`;
|
||||
}
|
||||
|
||||
if ("id" in result) {
|
||||
return `Successfully sent post to Facebook. Post ID: ${result.id}`;
|
||||
}
|
||||
|
||||
return `Failed to send message to Facebook. Unexpected response: ${JSON.stringify(result)}`;
|
||||
} catch (error: unknown) {
|
||||
return `Failed to send message to Facebook. ${
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: String(error)
|
||||
}`;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user