2022-12-07 12:17:31 -05:00
import { Client } from "twitter-api-sdk" ;
import Twit from "twit" ;
import dotenv from "dotenv" ;
dotenv . config ( ) ;
import { Configuration , OpenAIApi } from "openai" ;
import fs from "fs/promises" ;
/*################################## TWITTER API #########################################*/
const config = {
consumer _key : process . env . TWITTER _API _KEY ,
consumer _secret : process . env . TWITTER _API _SECRET _KEY ,
access _token : process . env . TWITTER _ACCESS _TOKEN ,
access _token _secret : process . env . TWITTER _ACCESS _TOKEN _SECRET ,
timeout _ms : 60 * 1000 ,
strictSSL : true ,
} ;
const T = new Twit ( config ) ;
const client = new Client ( process . env . TWITTER _BEARER _TOKEN ) ; //twitter api client
const botName = process . env . BOT _USERNAME ; //Use the same Twitter username whose API key and token are being used.
/*############################# REPLY TO TWITTER API #################################*/
async function replyToTweet ( joke , author _id , id ) {
const data = {
status : ` ${ joke } ` , //the joke
in _reply _to _user _id : author _id , //the id of the user who tweeted
in _reply _to _status _id : id , //the id of the tweet
auto _populate _reply _metadata : true , //auto populate the reply metadata
} ;
try {
const resp = await T . post ( "statuses/update" , data ) ;
if ( resp ) {
console . log ( "replied" ) ;
}
} catch ( error ) {
console . log ( error ) ;
}
}
/*############################# GET THE TWEETS FROM TWITTER API ##########################*/
async function getExitingRule ( ) { //get the existing rules
try {
const rules = await client . tweets . getRules ( ) ;
return rules ;
} catch ( error ) {
console . log ( error ) ;
}
}
async function deleteAndSetNewRules ( ) { //delete the existing rules and set new rules
try {
const rules = await getExitingRule ( ) ;
// if rules includes id in data then delete the rules
if ( rules . data ) {
console . log ( "rule exists, now deleting" ) ;
const ids = rules . data . map ( ( rule ) => rule . id ) ;
await client . tweets . addOrDeleteRules ( {
delete : {
ids : ids ,
}
} ) ;
}
console . log ( "setting new rules" ) ;
await client . tweets . addOrDeleteRules ( {
add : [
{
value : ` @ ${ botName } has:mentions `
}
]
} ) ;
} catch ( error ) {
console . log ( error ) ;
}
}
async function getMentionedTweet ( ) {
try {
console . log ( "running" ) ;
await deleteAndSetNewRules ( ) ;
const stream = await client . tweets . searchStream ( {
"tweet.fields" : [
"author_id" , // The ID of the user who posted the tweet
"id" , // The ID of the tweet
"in_reply_to_user_id" // The ID of the user the tweet is replying to
] ,
"expansions" : [
"referenced_tweets.id.author_id" // The ID of the user who posted the referenced tweet
]
} ) ;
for await ( const response of stream ) {
if ( response . data . text . includes ( ` @ ${ botName } ` ) ) { //check if the tweet contains the bot's username
/* IF BOT IS MENTIONED **IN** THE TWEET */
if ( response . includes . tweets === undefined ) { //check if the tweet is a reply to another tweet
2022-12-07 12:24:52 -05:00
const tweet = JSON . stringify ( response . data . text , null , 2 ) . replace ( /(https?:\/\/[^\s]+)/g , '' ) . replace ( /"/g , '' ) . trim ( ) ; r
console . log ( response . data )
const reply = "works"
await replyToTweet ( reply , response . data . author _id , response . data . id ) ;
2022-12-07 12:17:31 -05:00
} else {
/ * I F B O T I S M E N T I O N E D * * U N D E R * * T H E T W E E T T H E N I T W I L L R E P L Y T O W H O E V E R M E N T I O N E D
THE BOT BUT WILL TAKE QUESTIONS FROM THE ORIGINAL AUTHORS TWEET * /
2022-12-07 12:24:52 -05:00
let userID = response . data . author _id
console . log ( "User ID: " )
2022-12-07 12:17:31 -05:00
const tweet = JSON . stringify ( response . includes . tweets [ 0 ] . text , null , 2 ) . replace ( /(https?:\/\/[^\s]+)/g , '' ) . replace ( /"/g , '' ) . trim ( ) ; //remove the urls and double quotes from the tweet and trim the spaces
2022-12-07 12:24:52 -05:00
const reply = "Not Programmed Yet!" //get the joke from the openai api
await replyToTweet ( reply , response . data . author _id , response . data . id ) ; // reply to the
2022-12-07 12:17:31 -05:00
}
}
}
} catch ( error ) {
console . log ( error ) ;
}
}
getMentionedTweet ( ) ;