-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from dcSpark/feature/twitter-post
twitter-post-tool
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |