import { cli } from '@jackwener/opencli/registry'; cli({ site: 'instagram', name: 'comment', access: 'write', description: 'Comment on an Instagram post', domain: 'www.instagram.com', args: [ { name: 'username', required: true, positional: true, help: 'Username of the post author', }, { name: 'text', required: true, positional: true, help: 'Comment text' }, { name: 'index', type: 'int', default: 1, help: 'Post index (1 = most recent)' }, ], columns: ['status', 'user', 'text'], pipeline: [ { navigate: 'https://www.instagram.com' }, { evaluate: `(async () => { const username = \${{ args.username | json }}; const commentText = \${{ args.text | json }}; const idx = \${{ args.index }} - 1; const headers = { 'X-IG-App-ID': '936619743392459' }; const opts = { credentials: 'include', headers }; const r1 = await fetch('https://www.instagram.com/api/v1/users/web_profile_info/?username=' + encodeURIComponent(username), opts); if (!r1.ok) throw new Error('User not found: ' + username); const userId = (await r1.json())?.data?.user?.id; const r2 = await fetch('https://www.instagram.com/api/v1/feed/user/' + userId + '/?count=' + (idx + 1), opts); const posts = (await r2.json())?.items || []; if (idx >= posts.length) throw new Error('Post index ' + (idx + 1) + ' not found'); const pk = posts[idx].pk; const csrf = document.cookie.match(/csrftoken=([^;]+)/)?.[1] || ''; const r3 = await fetch('https://www.instagram.com/api/v1/web/comments/' + pk + '/add/', { method: 'POST', credentials: 'include', headers: { ...headers, 'X-CSRFToken': csrf, 'Content-Type': 'application/x-www-form-urlencoded' }, body: 'comment_text=' + encodeURIComponent(commentText), }); if (!r3.ok) throw new Error('Failed to comment: HTTP ' + r3.status); return [{ status: 'Commented', user: username, text: commentText }]; })() ` }, ], });