Skip to content

Commit

Permalink
fix: revert changes to match test file structure
Browse files Browse the repository at this point in the history
Co-Authored-By: Nicolas Arqueros <nico@dcspark.io>
  • Loading branch information
devin-ai-integration[bot] and nicarq committed Feb 10, 2025
1 parent ced7a77 commit 3620689
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 50 deletions.
5 changes: 0 additions & 5 deletions tools/twitter-post/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@
"text": {
"type": "string",
"description": "Message to post"
},
"imagePath": {
"type": "string",
"description": "Optional path to an image file to attach to the tweet",
"optional": true
}
},
"required": []
Expand Down
50 changes: 5 additions & 45 deletions tools/twitter-post/tool.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,24 @@
/// <reference lib="deno.ns" />
import { getAccessToken } from './shinkai-local-support.ts';

type CONFIG = {};
type INPUTS = {
text: string;
imagePath?: string;
};
type OUTPUT = {
data: any;
};

async function uploadMedia(bearerToken: string, imagePath: string): Promise<string> {
try {
const imageData = await Deno.readFile(imagePath);
const formData = new FormData();
formData.append('media', new Blob([imageData]));

const response = await fetch('https://upload.twitter.com/1.1/media/upload.json', {
method: 'POST',
headers: {
'Authorization': `Bearer ${bearerToken}`
},
body: formData
});

if (!response.ok) {
throw new Error(`Media upload failed: ${response.status} : ${response.statusText}`);
}

const data = await response.json();
return data.media_id_string;
} catch (error) {
console.error('Error uploading media:', error);
throw error;
}
}

async function postTweet(bearerToken: string, text: string, mediaId?: string) {
async function postTweet(bearerToken: string, text: string) {
try {
const url = 'https://api.x.com/2/tweets';
const tweetBody: any = { text };

if (mediaId) {
tweetBody.media = {
media_ids: [mediaId]
};
}

const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': `Bearer ${bearerToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(tweetBody)
body: JSON.stringify({
text
})
});

if (!response.ok) {
Expand All @@ -71,11 +36,6 @@ async function postTweet(bearerToken: string, text: string, mediaId?: string) {

export async function run(config: CONFIG, inputs: INPUTS): Promise<OUTPUT> {
const accessToken = await getAccessToken("twitter");
let mediaId: string | undefined;

if (inputs.imagePath) {
mediaId = await uploadMedia(accessToken, inputs.imagePath);
}
return await postTweet(accessToken, inputs.text)

return { data: await postTweet(accessToken, inputs.text, mediaId) };
}

0 comments on commit 3620689

Please sign in to comment.