Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

feat: facebook stories

Nevo David7aa50e5e93a1
diff --git a/apps/frontend/src/components/new-launch/providers/facebook/facebook.provider.tsx b/apps/frontend/src/components/new-launch/providers/facebook/facebook.provider.tsx
index 4b246b4a8..a3bbb70e9 100644
--- a/apps/frontend/src/components/new-launch/providers/facebook/facebook.provider.tsx
+++ b/apps/frontend/src/components/new-launch/providers/facebook/facebook.provider.tsx
@@ -6,28 +6,66 @@ import {
} from '@gitroom/frontend/components/new-launch/providers/high.order.provider';
import { FacebookDto } from '@gitroom/nestjs-libraries/dtos/posts/providers-settings/facebook.dto';
import { Input } from '@gitroom/react/form/input';
+import { Select } from '@gitroom/react/form/select';
import { useSettings } from '@gitroom/frontend/components/launches/helpers/use.values';
import { FacebookPreview } from '@gitroom/frontend/components/new-launch/providers/facebook/facebook.preview';
+import { useT } from '@gitroom/react/translation/get.transation.service.client';
+
+const postType = [
+ {
+ value: 'post',
+ label: 'Post',
+ },
+ {
+ value: 'story',
+ label: 'Story',
+ },
+];
export const FacebookSettings = () => {
- const { register } = useSettings();
+ const t = useT();
+ const { register, watch } = useSettings();
+ const postCurrentType = watch('post_type');
return (
- <Input
- label={
- 'Embedded URL (only for text Post)'
- }
- {...register('url')}
- />
+ <>
+ <Select
+ label="Post Type"
+ {...register('post_type', {
+ value: 'post',
+ })}
+ >
+ <option value="">{t('select_post_type', 'Select Post Type...')}</option>
+ {postType.map((item) => (
+ <option key={item.value} value={item.value}>
+ {item.label}
+ </option>
+ ))}
+ </Select>
+
+ {postCurrentType !== 'story' && (
+ <Input
+ label={'Embedded URL (only for text Post)'}
+ {...register('url')}
+ />
+ )}
+ </>
);
};
-export default withProvider({
+export default withProvider<FacebookDto>({
postComment: PostComment.COMMENT,
minimumCharacters: [],
SettingsComponent: FacebookSettings,
CustomPreviewComponent: FacebookPreview,
dto: FacebookDto,
- checkValidity: undefined,
+ checkValidity: async ([firstPost, ...otherPosts] = [], settings) => {
+ if (settings?.post_type === 'story') {
+ if (!firstPost?.length) {
+ return 'Story should have at least one media';
+ }
+ }
+ return true;
+ },
maximumCharacters: 63206,
});
diff --git a/libraries/nestjs-libraries/src/dtos/posts/providers-settings/facebook.dto.ts b/libraries/nestjs-libraries/src/dtos/posts/providers-settings/facebook.dto.ts
index 5743e03af..cc6899cb4 100644
--- a/libraries/nestjs-libraries/src/dtos/posts/providers-settings/facebook.dto.ts
+++ b/libraries/nestjs-libraries/src/dtos/posts/providers-settings/facebook.dto.ts
@@ -1,8 +1,12 @@
-import { IsOptional, ValidateIf, IsUrl } from 'class-validator';
+import { IsIn, IsOptional, ValidateIf, IsUrl } from 'class-validator';
export class FacebookDto {
@IsOptional()
@ValidateIf(p => p.url)
@IsUrl()
url?: string;
+
+ @IsIn(['post', 'story'])
+ @IsOptional()
+ post_type?: 'post' | 'story';
}
diff --git a/libraries/nestjs-libraries/src/integrations/social/facebook.provider.ts b/libraries/nestjs-libraries/src/integrations/social/facebook.provider.ts
index 3b0d11d19..96e43c9f9 100644
--- a/libraries/nestjs-libraries/src/integrations/social/facebook.provider.ts
+++ b/libraries/nestjs-libraries/src/integrations/social/facebook.provider.ts
@@ -12,7 +12,12 @@ import { FacebookDto } from '@gitroom/nestjs-libraries/dtos/posts/providers-sett
import { DribbbleDto } from '@gitroom/nestjs-libraries/dtos/posts/providers-settings/dribbble.dto';
import { Integration } from '@prisma/client';
import { hasExtension } from '@gitroom/helpers/utils/has.extension';
+import { timer } from '@gitroom/helpers/utils/timer';
+import { Rules } from '@gitroom/nestjs-libraries/chat/rules.description.decorator';
+@Rules(
+ "Facebook posts can be text only, or include photos or a video. If it's a story, it must have at least one attachment (photo or video), and each media is published as a separate story."
+)
export class FacebookProvider extends SocialAbstract implements SocialProvider {
identifier = 'facebook';
name = 'Facebook Page';
@@ -413,10 +418,103 @@ export class FacebookProvider extends SocialAbstract implements SocialProvider {
postDetails: PostDetails<FacebookDto>[]
): Promise<PostResponse[]> {
const [firstPost] = postDetails;
+ const isStory = firstPost?.settings?.post_type === 'story';
let finalId = '';
let finalUrl = '';
- if (hasExtension(firstPost?.media?.[0]?.path, 'mp4')) {
+ if (isStory) {
+ let lastPostId = '';
+ for (const media of firstPost?.media || []) {
+ const isVideoStory = hasExtension(media.path, 'mp4');
+ if (isVideoStory) {
+ const { video_id, upload_url } = await (
+ await this.fetch(
+ `https://graph.facebook.com/v20.0/${id}/video_stories?upload_phase=start&access_token=${accessToken}`,
+ {
+ method: 'POST',
+ },
+ 'start video story upload'
+ )
+ ).json();
+
+ await this.fetch(
+ upload_url,
+ {
+ method: 'POST',
+ headers: {
+ Authorization: `OAuth ${accessToken}`,
+ file_url: media.path,
+ },
+ },
+ 'upload video story'
+ );
+
+ let videoStatus = 'in_progress';
+ while (videoStatus !== 'ready') {
+ const { status } = await (
+ await this.fetch(
+ `https://graph.facebook.com/v20.0/${video_id}?fields=status&access_token=${accessToken}`,
+ undefined,
+ '',
+ 0,
+ true
+ )
+ ).json();
+ videoStatus = status?.video_status || 'in_progress';
+ if (videoStatus === 'error') {
+ throw new Error('Video processing failed');
+ }
+ if (videoStatus !== 'ready') {
+ await timer(10000);
+ }
+ }
+
+ const { post_id: storyPostId } = await (
+ await this.fetch(
+ `https://graph.facebook.com/v20.0/${id}/video_stories?upload_phase=finish&video_id=${video_id}&access_token=${accessToken}`,
+ {
+ method: 'POST',
+ },
+ 'finish video story upload'
+ )
+ ).json();
+
+ lastPostId = storyPostId;
+ } else {
+ const { id: photoId } = await (
+ await this.fetch(
+ `https://graph.facebook.com/v20.0/${id}/photos?access_token=${accessToken}`,
+ {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ url: media.path,
+ published: false,
+ }),
+ },
+ 'upload photo story'
+ )
+ ).json();
+
+ const { post_id: storyPostId } = await (
+ await this.fetch(
+ `https://graph.facebook.com/v20.0/${id}/photo_stories?photo_id=${photoId}&access_token=${accessToken}`,
+ {
+ method: 'POST',
+ },
+ 'publish photo story'
+ )
+ ).json();
+
+ lastPostId = storyPostId;
+ }
+ }
+
+ finalId = lastPostId;
+ finalUrl = `https://www.facebook.com/stories/${lastPostId}`;
+ } else if (hasExtension(firstPost?.media?.[0]?.path, 'mp4')) {
const {
id: videoId,
permalink_url,