UNPKG

@agentdao/core

Version:

Core functionality, skills, and ready-made UI components for AgentDAO - Web3 subscriptions, content generation, social media, help support, live chat, RSS fetching, web search, and agent pricing integration

312 lines (311 loc) 12.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SocialMediaSkill = void 0; const axios_1 = __importDefault(require("axios")); class SocialMediaSkill { constructor(config) { // Require Twitter keys if Twitter is enabled if (config.platforms?.twitter?.enabled) { const t = config.platforms.twitter; if (!t.apiKey || !t.apiSecret || !t.accessToken || !t.accessTokenSecret) { throw new Error('No Twitter API key(s) provided. Please add your keys in Settings > API Keys.'); } } this.config = config; } async postToAll(content, media) { const results = []; const platforms = Object.keys(this.config.platforms).filter(platform => this.config.platforms[platform]?.enabled); for (const platform of platforms) { try { const result = await this.postToPlatform(platform, content, media); results.push(result); } catch (error) { results.push({ platform, postId: '', url: '', success: false, error: error instanceof Error ? error.message : 'Unknown error', timestamp: new Date() }); } } return results; } async postToPlatform(platform, content, media) { const platformConfig = this.config.platforms[platform]; if (!platformConfig?.enabled) { throw new Error(`Platform ${platform} is not enabled`); } switch (platform) { case 'twitter': return await this.postToTwitter(content, media); case 'linkedin': return await this.postToLinkedIn(content, media); case 'facebook': return await this.postToFacebook(content, media); case 'instagram': return await this.postToInstagram(content, media); default: throw new Error(`Platform ${platform} not supported`); } } async schedulePost(platform, content, scheduledTime) { if (!this.config.database || !this.config.database.endpoint) { throw new Error('No database endpoint configured for scheduling posts. Please configure a database endpoint to use this feature.'); } const scheduledPost = { id: this.generatePostId(), platform, content, scheduledTime, status: 'scheduled', mediaAttachments: [] }; try { await fetch(this.config.database.endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json', ...(this.config.database.apiKey ? { 'Authorization': `Bearer ${this.config.database.apiKey}` } : {}) }, body: JSON.stringify({ type: 'schedule_post', data: scheduledPost, agentId: this.config.agentId, agentName: this.config.agentName, domain: this.config.domain, timestamp: new Date().toISOString() }) }); } catch (error) { throw new Error(`Failed to schedule post: ${error instanceof Error ? error.message : 'Unknown error'}`); } return scheduledPost; } async getDraftPosts() { if (!this.config.database || !this.config.database.endpoint) { throw new Error('No database endpoint configured for retrieving drafts. Please configure a database endpoint to use this feature.'); } try { const response = await fetch(`${this.config.database.endpoint}?type=drafts`, { headers: { ...(this.config.database.apiKey ? { 'Authorization': `Bearer ${this.config.database.apiKey}` } : {}) } }); if (!response.ok) { throw new Error(`Failed to fetch drafts: ${response.statusText}`); } const data = await response.json(); return data.drafts || []; } catch (error) { throw new Error(`Failed to get draft posts: ${error instanceof Error ? error.message : 'Unknown error'}`); } } async saveDraft(content, platforms) { if (!this.config.database || !this.config.database.endpoint) { throw new Error('No database endpoint configured for saving drafts. Please configure a database endpoint to use this feature.'); } const draftId = this.generatePostId(); const draft = { id: draftId, content, platforms, createdAt: new Date(), updatedAt: new Date() }; try { await fetch(this.config.database.endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json', ...(this.config.database.apiKey ? { 'Authorization': `Bearer ${this.config.database.apiKey}` } : {}) }, body: JSON.stringify({ type: 'save_draft', data: draft, agentId: this.config.agentId, agentName: this.config.agentName, domain: this.config.domain, timestamp: new Date().toISOString() }) }); } catch (error) { throw new Error(`Failed to save draft: ${error instanceof Error ? error.message : 'Unknown error'}`); } return draftId; } async editDraft(draftId, changes) { if (!this.config.database || !this.config.database.endpoint) { throw new Error('No database endpoint configured for editing drafts. Please configure a database endpoint to use this feature.'); } try { const response = await fetch(this.config.database.endpoint, { method: 'PUT', headers: { 'Content-Type': 'application/json', ...(this.config.database.apiKey ? { 'Authorization': `Bearer ${this.config.database.apiKey}` } : {}) }, body: JSON.stringify({ type: 'edit_draft', draftId, changes, agentId: this.config.agentId, agentName: this.config.agentName, domain: this.config.domain, timestamp: new Date().toISOString() }) }); if (!response.ok) { throw new Error(`Failed to edit draft: ${response.statusText}`); } const data = await response.json(); return data.draft; } catch (error) { throw new Error(`Failed to edit draft: ${error instanceof Error ? error.message : 'Unknown error'}`); } } async respondToComments(postId, responses) { for (const response of responses) { try { await this.postComment(response.commentId, response.response, response.platform); } catch (error) { console.error(`Failed to respond to comment ${response.commentId}:`, error); return false; } } return true; } // Platform-specific posting methods async postToTwitter(content, media) { const config = this.config.platforms.twitter; if (!config) throw new Error('Twitter not configured'); try { // This would use Twitter API v2 const response = await axios_1.default.post('https://api.twitter.com/2/tweets', { text: content }, { headers: { 'Authorization': `Bearer ${config.accessToken}`, 'Content-Type': 'application/json' } }); return { platform: 'twitter', postId: response.data.data.id, url: `https://twitter.com/${config.username}/status/${response.data.data.id}`, success: true, timestamp: new Date() }; } catch (error) { throw new Error(`Twitter posting failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } async postToLinkedIn(content, media) { const config = this.config.platforms.linkedin; if (!config) throw new Error('LinkedIn not configured'); try { // This would use LinkedIn API const response = await axios_1.default.post('https://api.linkedin.com/v2/ugcPosts', { author: `urn:li:person:${config.companyId}`, lifecycleState: 'PUBLISHED', specificContent: { 'com.linkedin.ugc.ShareContent': { shareCommentary: { text: content }, shareMediaCategory: 'NONE' } }, visibility: { 'com.linkedin.ugc.MemberNetworkVisibility': 'PUBLIC' } }, { headers: { 'Authorization': `Bearer ${config.accessToken}`, 'Content-Type': 'application/json' } }); return { platform: 'linkedin', postId: response.data.id, url: `https://linkedin.com/feed/update/${response.data.id}`, success: true, timestamp: new Date() }; } catch (error) { throw new Error(`LinkedIn posting failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } async postToFacebook(content, media) { const config = this.config.platforms.facebook; if (!config) throw new Error('Facebook not configured'); try { // This would use Facebook Graph API const response = await axios_1.default.post(`https://graph.facebook.com/v18.0/${config.pageId}/feed`, { message: content }, { headers: { 'Authorization': `Bearer ${config.accessToken}`, 'Content-Type': 'application/json' } }); return { platform: 'facebook', postId: response.data.id, url: `https://facebook.com/${response.data.id}`, success: true, timestamp: new Date() }; } catch (error) { throw new Error(`Facebook posting failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } async postToInstagram(content, media) { const config = this.config.platforms.instagram; if (!config) throw new Error('Instagram not configured'); try { // This would use Instagram Basic Display API or Graph API // Instagram posting is more complex and requires media if (!media || media.length === 0) { throw new Error('Instagram posts require media'); } // Mock response for now return { platform: 'instagram', postId: `ig_${Date.now()}`, url: `https://instagram.com/p/ig_${Date.now()}`, success: true, timestamp: new Date() }; } catch (error) { throw new Error(`Instagram posting failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } async postComment(commentId, response, platform) { // This would post a reply to a comment using platform-specific APIs console.log(`Posting comment reply to ${commentId} on ${platform}: ${response}`); } generatePostId() { return `post_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`; } } exports.SocialMediaSkill = SocialMediaSkill;