Skip to content

Commit

Permalink
Merge pull request #56 from dcSpark/feature/twitter-post
Browse files Browse the repository at this point in the history
twitter-post-tool
  • Loading branch information
acedward authored Jan 20, 2025
2 parents 792897e + ad3227c commit f844485
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tools/twitter-post/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "Twitter Post",
"description": "Function to post a tweet to Twitter.",
"author": "Shinkai",
"version": "1.0.0",
"url": "https://help.shinkai.com/official-twitter-tool",
"keywords": [
"twitter",
"X",
"post",
"social media"
],
"configurations": {
"type": "object",
"properties": {},
"required": []
},
"parameters": {
"type": "object",
"properties": {
"text": {
"type": "string",
"description": "Message to post"
}
},
"required": []
},
"result": {
"type": "object",
"properties": {
"data": {
"type": "string",
"description": "The data returned by the Twitter API"
}
},
"required": [
"data"
]
},
"sqlTables": [],
"sqlQueries": [],
"tools": [],
"oauth": [
{
"name": "twitter",
"version": "2.0",
"authorizationUrl": "https://twitter.com/i/oauth2/authorize",
"redirectUrl": "https://secrets.shinkai.com/redirect",
"tokenUrl": "https://api.x.com/2/oauth2/token",
"clientId": "",
"clientSecret": "",
"scopes": [
"tweet.read",
"tweet.write",
"users.read",
"offline.access"
],
"responseType": "code",
"pkceType": "plain",
"refreshToken": "true"
}
]
}
41 changes: 41 additions & 0 deletions tools/twitter-post/tool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { getAccessToken } from './shinkai-local-support.ts';

type CONFIG = {};
type INPUTS = {
text: string;
};
type OUTPUT = {
};

async function postTweet(bearerToken: string, text: string) {
try {
const url = 'https://api.x.com/2/tweets';
const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': `Bearer ${bearerToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
text
})
});

if (!response.ok) {
throw new Error(`Server returned ${response.status} : ${response.statusText}`);
}

const data = await response.json();
console.log('Tweet posted:', data);
return data;
} catch (error) {
console.error('Error posting tweet:', error);
return error;
}
}

export async function run(config: CONFIG, inputs: INPUTS): Promise<OUTPUT> {
const accessToken = await getAccessToken("twitter");
return await postTweet(accessToken, inputs.text)

}

0 comments on commit f844485

Please sign in to comment.