Creating a Bot/Python

## Twitter Bots

Visit this page!

Templates

## Facebook Bots

If you don't know the basics of using facebook-sdk, refer to this guide: How to make a facebook bot (basics)
If you know python, and are okay with reading a bit of api docs, you may skip the basics.
Also you would need:

* Python 3.x
* IDE (preferably VS Code/Sublime Text/Atom)
* Loads of free time
* VPS/ Host (Anything is fine here, even your PC is okay if you can keep it on 24/7, otherwise if you can use Amazon's AWS, Google Cloud, Microsoft Azure, etc)

Here, i will be walking you through the process of writing the code, getting the access token, and just a sneak peak of scheduling your bot!
Now time for some coding!
Lets say you want to make a **Lottery Ticket Bot** (classic example).
Firstly we would need to generate lottery tickets, which are essentially random. So lets rev up our text editors. In my case, I'm using Sublime Text 3. If you are using Sublime Text as well, it should look like this to you (considering you're in a folder)

Great! Now lets do some coding.

Considering python is basically just spicy English, it shouldn't be too difficult.

So after writing literally 3 lines of code, we come up with this

But this doesn't quite look like a lottery ticket number, does it?

That fix should be easy, just a little for loop to insert a space within out number...

And overall, tweaking the code a bit to make it look better,

Basically i put everything into functions to make the code a bit more organized.
This gives us the output

*the first output is the originally generated number and the second is the better looking version*

That means, we are now ready to make our bot live on Facebook!

I am using my test page for this purpose, but it should be more or less the same for you people.
After creating the page, head over to this site and get an access token for your bot!

**[UPDATE 2021]** It is now possible *yet again* to make your own token from scratch. Although keep in mind, this **will** limit some advanced features for your bot.You should do this only if the token tool website is down for some reason and you want to test your bot anyways.

I'm not going to dive deep on how to generate the token as Paintmin, the site owner and our Lord at The Bot Appreciation Society has a pretty detailed description of that on his site.

Now, store the token in your program in a variable with whatever name you want.

After that we need a function that can post the Ticket on Facebook.

Here's the code for the function:

If you're wondering why **put\_photo** and not **put\_object,**that's because Facebook blocks text posts from time to time and marks it as spam. So, we attach a 400\*1 white image to make it seem like a regular text post.
You can download the image from here

**[UPDATE 2021]** It is now possible to make text posts without attaching any image!

```
graph.put\_object(parent\_object = "me", connection\_name = "feed",message = message)

```

Overall, this is how it looks:

Now its time to run it...
and viola, It worked!

Lets see if it actually posted anything or not.

Yes it did!

Now back to work...

We have to make our bot post the ticket to our page.
For that, we make a minor adjustment to the end of the code:

Lets see if it worked...

Yay! It worked perfectly!

Now we need the bot to do this every 30 minutes lets say,
For that, we can use this god sent module called schedule. (use pip install schedule to get the module)

After that lets import schedule & time and shove everything in the main function to another function called job.

Now All you need to do is let the code run, and it will keep posting lottery numbers.
Of course, posting lottery numbers wont make a bot fun. Try coming up with bot ideas, and integrate them with the code we just wrote. I'm positive you can code a brilliant bot as well!

-Semolini

(Admin of pages like Every Tom and Jerry Frame in Order, Blessed Bot 6009, Unmanned Cringe Bot etc...)

**[UPDATE 2021]** Semolini from 2021 here! I just wanted you guys to know that this is not a good way to generate random lottery numbers. Although it serves the purpose here for demonstrating how the Facebook api works pretty well, if you're into generating big random numbers *(for some reason)* , this is not the way to go. This script in the long run might hog a chunk of your processing power. This article gives you a pretty solid overview of what not to do and *why* not to do. That being said, best of luck on your bot adventures from all of us at The Bot Appreciation Society!