index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. require('dotenv').config();
  2. const axios = require('axios');
  3. const BasePlatformService = require('./utils/BasePlatformService');
  4. const { getDb } = require('./utils/MongoDBConnector');
  5. const { decryptToken, warnIfNoKey } = require('./utils/crypto');
  6. const GRAPH_API = 'https://graph.facebook.com/v22.0';
  7. class InstagramService extends BasePlatformService {
  8. constructor() {
  9. super('instagram');
  10. }
  11. // Read selected Instagram Business Accounts from MongoDB.
  12. // Falls back to env vars for backwards compatibility.
  13. async _getAccounts() {
  14. try {
  15. const db = await getDb();
  16. const cred = await db.collection('platform_credentials').findOne({ _id: 'instagram' });
  17. const dbAccounts = (cred?.accounts || []).filter((a) => a.selected);
  18. if (dbAccounts.length > 0) {
  19. return dbAccounts.map((a) => ({ ...a, accessToken: decryptToken(a.accessToken) })).filter((a) => a.accessToken);
  20. }
  21. } catch (_) { /* fall through */ }
  22. // Env var fallback (legacy single-account mode)
  23. const { INSTAGRAM_ACCESS_TOKEN, INSTAGRAM_BUSINESS_ACCOUNT_ID } = process.env;
  24. if (INSTAGRAM_ACCESS_TOKEN && INSTAGRAM_BUSINESS_ACCOUNT_ID) {
  25. return [{ id: INSTAGRAM_BUSINESS_ACCOUNT_ID, accessToken: INSTAGRAM_ACCESS_TOKEN }];
  26. }
  27. return [];
  28. }
  29. async getStatus() {
  30. const accounts = await this._getAccounts();
  31. if (accounts.length === 0) {
  32. return { connected: false, platform: 'instagram', error: 'No Instagram accounts connected — use Settings to connect via Facebook OAuth' };
  33. }
  34. try {
  35. const first = accounts[0];
  36. const res = await axios.get(`${GRAPH_API}/${first.id}`, {
  37. params: {
  38. fields: 'id,name,username,profile_picture_url',
  39. access_token: first.accessToken,
  40. },
  41. });
  42. return {
  43. connected: true,
  44. platform: 'instagram',
  45. username: res.data.username || res.data.name,
  46. displayName: res.data.name,
  47. avatar: res.data.profile_picture_url,
  48. accountCount: accounts.length,
  49. };
  50. } catch (err) {
  51. return { connected: false, platform: 'instagram', error: err.response?.data?.error?.message || err.message };
  52. }
  53. }
  54. async fetchFeed({ limit = 20 } = {}) {
  55. const accounts = await this._getAccounts();
  56. if (accounts.length === 0) throw new Error('No Instagram accounts connected');
  57. const allItems = [];
  58. for (const account of accounts) {
  59. const res = await axios.get(`${GRAPH_API}/${account.id}/media`, {
  60. params: {
  61. fields: 'id,caption,media_type,media_url,thumbnail_url,permalink,timestamp,like_count,comments_count,username',
  62. limit: Math.min(Number(limit), 100),
  63. access_token: account.accessToken,
  64. },
  65. });
  66. const items = (res.data.data || []).map((post) =>
  67. this.normalizeFeedItem({
  68. originalId: post.id,
  69. author: {
  70. name: post.username || account.username || '',
  71. username: post.username || account.username || '',
  72. profileUrl: `https://www.instagram.com/${post.username || account.username || ''}/`,
  73. },
  74. content: post.caption || '',
  75. media: post.media_url
  76. ? [{
  77. url: post.media_url,
  78. type: (post.media_type || 'IMAGE').toLowerCase(),
  79. thumbnail: post.thumbnail_url || post.media_url,
  80. }]
  81. : [],
  82. metrics: {
  83. likes: post.like_count || 0,
  84. comments: post.comments_count || 0,
  85. },
  86. url: post.permalink,
  87. createdAt: post.timestamp,
  88. })
  89. );
  90. allItems.push(...items);
  91. }
  92. try {
  93. const db = await getDb();
  94. const col = db.collection('feeds');
  95. for (const item of allItems) {
  96. await col.updateOne(
  97. { platform: 'instagram', originalId: item.originalId },
  98. { $set: item },
  99. { upsert: true }
  100. );
  101. }
  102. } catch (err) {
  103. this.app.log.error({ action: 'feed_write', platform: 'instagram', outcome: 'failure', err: err.message });
  104. }
  105. return allItems;
  106. }
  107. // Instagram requires media (image_url or video_url) — text-only posts are not supported.
  108. async publishPost({ content, imageUrl, videoUrl, accountId, firstComment } = {}) {
  109. const allAccounts = await this._getAccounts();
  110. if (allAccounts.length === 0) throw new Error('No Instagram accounts connected');
  111. if (!imageUrl && !videoUrl) {
  112. throw new Error('Instagram requires imageUrl or videoUrl — text-only posts are not supported by the Graph API');
  113. }
  114. // If a specific account is requested, target only that account
  115. const accounts = accountId ? allAccounts.filter((a) => a.id === accountId) : allAccounts;
  116. if (accounts.length === 0) throw new Error(`Instagram account ${accountId} not found or not connected`);
  117. const results = [];
  118. for (const account of accounts) {
  119. const containerParams = {
  120. caption: content,
  121. access_token: account.accessToken,
  122. };
  123. if (videoUrl) {
  124. containerParams.media_type = 'REELS';
  125. containerParams.video_url = videoUrl;
  126. } else {
  127. containerParams.image_url = imageUrl;
  128. }
  129. const containerRes = await axios.post(
  130. `${GRAPH_API}/${account.id}/media`,
  131. null,
  132. { params: containerParams }
  133. );
  134. const publishRes = await axios.post(
  135. `${GRAPH_API}/${account.id}/media_publish`,
  136. null,
  137. { params: { creation_id: containerRes.data.id, access_token: account.accessToken } }
  138. );
  139. const postId = publishRes.data.id;
  140. if (firstComment?.trim()) {
  141. try {
  142. await axios.post(`${GRAPH_API}/${postId}/comments`, null, {
  143. params: { message: firstComment.trim(), access_token: account.accessToken },
  144. timeout: 10000,
  145. });
  146. this.app.log.info({ action: 'first_comment', platform: 'instagram', postId, outcome: 'success' });
  147. } catch (err) {
  148. this.app.log.warn({ action: 'first_comment', platform: 'instagram', postId, outcome: 'failure', err: err.response?.data?.error?.message || err.message });
  149. }
  150. }
  151. results.push({ accountId: account.id, username: account.username, postId });
  152. }
  153. return results;
  154. }
  155. }
  156. const service = new InstagramService();
  157. warnIfNoKey('instagram');
  158. service.start(process.env.PORT || 3005);