I am trying to build a Twitter bot in with node and typescript. I have a Twitter Dev account and API OAuth credentials set up. I am also using cloudflare wrangler. The project's purpose is simply to do an API request to Tweet. I set up a local cloudflare wrangler typescript project with this command wrangler generate <my-project-name> https://github.com/cloudflare/worker-typescript-template.
When I generate the OAuth from my Twitter Developer account I checked OAuth v1 and v2 and for my callback URL I gave http://127.0.0.1:8787 (my local running instance). In the src directory of my project I have 3 files handler.ts, twitterClient.ts, and index.ts. The following are the files' contents:
twitterClient.ts
import { TwitterApi } from 'twitter-api-v2'
const client = new TwitterApi ({
appKey: "<MY APP KEY>",
appSecret: "<MY APP SECRET>",
accessToken: "<MY ACCESS TOKEN>",
accessSecret: "<MY ACCESS SECRET>",
});
export const rwClient = client.readWrite
handler.ts
export async function handleRequest(request: Request): Promise<Response> {
return new Response(`Hello worker! this is a ${request.method} request`, {
headers: { 'content-type': 'text/plain' },
});
}
index.ts
import { handleRequest } from './handler'
import { rwClient } from './twitterClient'
const tweet = async () => {
try {
await rwClient.v1.tweet('I Tweeted this via an API call')
}
catch (e) {
console.error(e)
}
return new Response('Hello worker!')
}
addEventListener('fetch', (event) => {
event.respondWith(tweet())
})
In addition to the actual app's code above, below are the configuration files of the app.
wrangler.toml
name = "<MY APP NAME>"
type = "webpack"
webpack_config = "webpack.config.js"
zone_id = ""
account_id = "<MY ACCOUNT ID>"
route = ""
workers_dev = true
compatibility_date = "2022-04-22"
[build]
command = "npm install && npm run build"
[build.upload]
format = "service-worker"
webpack.config.js
const path = require('path')
module.exports = {
entry: './src/index.ts',
output: {
filename: 'worker.js',
path: path.join(__dirname, 'dist'),
},
devtool: 'cheap-module-source-map',
mode: 'development',
resolve: {
extensions: ['.ts', '.tsx', '.js'],
},
node: {
fs: 'empty',
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: {
// transpileOnly is useful to skip typescript checks occasionally:
// transpileOnly: true,
},
},
],
},
}
I run my local cloudflare wrangler instance like this wrangler dev, and I get this in my terminal.
<myusername>@<mycomputername> <myappdirectory> % wrangler dev
Listening on http://127.0.0.1:8787
Detected changes...
Ignoring stale first change
I then use Postman and just do a blank get request to http://127.0.0.1:8787 and this is what I get in my terminal:
<myusername>@<mycomputername> <myappdirectory> % wrangler dev
Listening on http://127.0.0.1:8787
Detected changes...
Ignoring stale first change
{unknown error}
[2022-04-22 16:45:09] GET <APP NAME>.<USER NAME>.workers.dev/ HTTP/1.1 200 OK
Notice the {unknown error} I get in my output. I believe this is from the line console.error(e) in const tweet in the file index.ts
So finally... What's the problem? Why won't the my API call cause my Twitter account to tweet "I Tweeted this via an API call"? Also how can I get a clear error message?
One more minor question. Is it unsafe to have a callback url over "http" instead of "https"? Could someone listen to my API call and extract my API credentials?