DEV Community

Cover image for Many bot accounts using 1 Twitter Developer account
random
random

Posted on

Many bot accounts using 1 Twitter Developer account

Hey everyone! Did you know that there's a way to read/write/send messages to other twitter accounts with your Twitter Developer Account? (All discussions below are in context of the Twitter API v1.1)

How is that possible?🤔

pic1
In your Twitter Developer Account, you can find:

  1. API key and secret - belongs to your Twitter App(which you made inside your Twitter Developer Account)
  2. Access token and secret - belongs to the Twitter Account where you want to tweet/send messages etc..(The Access token & secret given in your Twitter App will let you tweet/send messages etc.. to the Twitter Account which you used to register for a Developer Account)

How do we do it?👩‍💻

We can get API key & secret from your Twitter App. But how do we get the Access token & secret of your bot account where you want to tweet? Unfortunately, there is no GUI feature currently to support this. We have to make our own server to run the authorisation of the bot account and get access tokens.

  1. Go to your Twitter Developer Account.
  2. Create a standalone app - this will give you your API key and secret. You can Choose your App Permissions also here.(For example: Read, Write, and Direct Messages)
  3. Enable 3-legged OAuth.
  4. Enter the callback URLs and website URLs as:
  5. Create a new folder in your computer and create 2 new files - authorize.js and package.json
  6. Get the file contents here.
  7. In your terminal, run npm install to install the Nodejs dependencies.
  8. Run node authorize.js to start the server.
  9. Double check that you are logged in to your Twitter bot account and visit http://localhost:3456.
  10. Click "authenticate"
  11. You will be redirected to a page where you can collect your Access token & secret.
  12. Yay! Now you can tweet in your bot account.🤖
  13. Still a bit worried whether your Twitter app has been connected to your Bot account? Still logged in to your bot account, go to https://twitter.com/settings/connected_apps to see if your Twitter App is connected.

Conclusion:

Congrats! You can now control your bot account with a single Twitter Developer Account.

f85547cc09fbd80b484f656ea30b7eab

References:

  1. https://gist.github.com/moonmilk/035917e668872013c1bd
  2. https://twittercommunity.com/t/multiple-bot-accounts/128332/2

Discussion (15)

Collapse
andypiper profile image
Andy Piper

This is super useful, thanks for sharing!

Collapse
vigneshkraj profile image
vigneshkraj

Hello,
I got the access token by the way followed like above. But how can I create BOT's ? and whats the use of getting those access token.
Say for eg., I have a parent acct called "A" which i'm holding all the keys(API,Access) for A, now i jus enabling 3-legged Oauth for other account "B". Am I right?

Collapse
randomjkbn profile image
random Author

Well..yes. Suppose u have ur own twitter handle and used it to apply for a Twitter Developer Account. You want ur Twitter bot to have it's own twitter handle(a different account). You have 2 options:

  1. Apply for a Developer Account for ur Twitter Bot.(getting ur application accepted will take time)
  2. Use your Twitter Developer Account to get access tokens and u can use them your bot accounts.(a maximum of 10 bots/apps can be supported as if now)
Thread Thread
vigneshkraj profile image
vigneshkraj

Thanks for your timely reply.
Still having a doubt, I have to use tweepy library.. for that it require
consumer_key
consumer_secret

access_token
access_token_secret
But in our case, by using above method i can get only access_token & access_token_secret... I require Consumer Key and secret also to generate.

Could you please look and share your thoughts. It will be useful for me. Actually I need around 50+ twitter account API key along with all 4 Consumer & access keys.
Thanks in advance

Thread Thread
randomjkbn profile image
random Author

In developer portal -> Projects & Apps -> Standalone Apps -> {Your App name} -> keys & tokens -> Consumer keys(API Key & Secret)

If u head over there, u will get consumer_key and consumer_secret. Check the first diagram in this blog post for reference.

res.cloudinary.com/practicaldev/im...

Thread Thread
vigneshkraj profile image
vigneshkraj

Thanks again for your reply. But here I'm having an issue, let me explain.
I have an account called "A", which I got approved for Twitter API access keys(all 4). Above method is done(oAuth enabled,callback URL given all fine) for account "A".

Now as per your prev comment guidance, I went to dev portal -> creating std alone apps for my another account "B"(given same in Website URL in account A dev account). Hence I got consumer keys & bearer key & access keys were generated as per this forum. So finally i got all the 4 keys.
Now while I use it in program, it calls always account A only, not account B.

That's my prblem. Thanks again.

Thread Thread
randomjkbn profile image
random Author

In the python program, access_token and access_token_secret should be what you obtain after running the nodejs program and visiting localhost:3456 (while logged into account B).

consumer_key and consumer_secret should be the keys from the standalone app from developer account A. Did u do it like this?

Thread Thread
vigneshkraj profile image
vigneshkraj • Edited on

Wow.. Lovely.. superb... Thanks a ton your immediate response as always.
And yes, I missed this point "visiting localhost:3456 (while logged into account B)." meaning, I was logged in from Acct A.

So , by using one proper API dev Approved account, can enable oAuth for one more account B like the above steps.
Hope can tag only one account called B or more than 1 like C, D also ? Thanks again.

Thread Thread
randomjkbn profile image
random Author

Nice! Theoretically speaking it should work as long as u have the access_token and access_token_secret of whichever account B,C or D.

It's an interesting question. I haven't tried it out for more than 1 bot account though in a single standalone app since I've never worked on such a use case.

You could try it out and if u do find something interesting, do let us know!

Thread Thread
vigneshkraj profile image
vigneshkraj

Yes...... !! It's working actually. As per our case, Account A is our primary.. on that i have created standalone account B, API keys will be provided in this app itself and have generated the tokens by using above forum(autorize.js). Like the same way, i have created one more account called C. Have tried the same way. It is got generated & tried the sample in my code. It works !!!

Let me check further and I will update u if i find anything unusual... Thanks a ton.....!! Good day

Thread Thread
randomjkbn profile image
random Author

Nice!

Collapse
randomjkbn profile image
random Author

This is just the first step. To build a bot, you'll have to program your Twitter bot in any programming language of your choice.

There's a fantastic collection of resources in the Botwiki community. You could check it out to see how some seriously cool bots were built!

botwiki.org/resources/twitterbots/...

Collapse
a7mdeno profile image
a7mdeno

Hello, I received the Access Token and Secret Token of my bot account, but in order to link my bot with my dev account, I still can't figure out where should I put them in? I tried to edit the ones in my dev account, but it can only be generated automatically. Sorry I'm a total beginner.

Collapse
randomjkbn profile image
random Author • Edited on

On a side note, I made my first twitter bot with the help of this video.
youtube.com/watch?v=W0wWwglE1Vc

However, this will tweet directly into your Twitter handle which is a developer account. If u want another Bot account that is different from your Developer account, u have to obtain access_token and access_token_secret keys in the method mentioned in the blog post and change these 2 lines of code.

Collapse
randomjkbn profile image
random Author

To use the Twitter API, u will need to use 4 variables - consumer_key, consumer_secret, access_token, access_token_secret in your bot program(which could be in Python(tweepy library is pretty famous), JavaScript or any language of ur choice).

  • consumer_key and consumer_secret should be the keys from the standalone app which is created from Twitter developer account.
  • access_token and access_token_secret should be the keys you obtain after running the nodejs program and visiting localhost:3456 (while logged into the bot account).

This blog only explains about how to get these 4 keys not about how to create a Twitter bot. (There are plenty of resources available online for creating a Twitter Bot)

Hope this helps!