Python Bot, just for fun ? Instagram ?
Photo by NeONBRAND on Unsplash

Python Bot, just for fun ? Instagram ?

A bot is a type of application that can automate repeatable event or pattern like in case of dropbox https://github.com/dropbox/securitybot which created a securitybot to improve their security incidents monitoring.

So try to find something that You repeat on daily basis maybe we can automate it ? In my case I manage IT projects in JIRA but many of my contractors are hired through Upwork, developers log time both in JIRA and Upwork, if You consider many projects and many people it is too much time consuming to check this each day. Creating a bot that would check if there is logging hours synchronization between 2 applications would be a great improvement for my finances and team management. At the end if there is no synchronization, bot will send a message to developer to update the logged hours.

Software Requirements Specification

Each good and reliable software must start with some definitions and goals, even most simple document will help You to identify the scope of the work.

As You know I like diagrams and for the purpose of this project scope diagram is totally fine. As You see below we have 3 main use cases, authentication, receiving and writing information, our main resources is Instagram.

Brak alternatywnego tekstu dla tego zdjęcia

The scope of the project, authenticate with Instagram account and receive information about comments, if comment will contain specific word send Direct Message to user.

Integrating Instagram

Instagram is owned by Facebook, the relevant documentation is here https://developers.facebook.com/docs/instagram/, on webpage click right top button Get Started to authenticate developers.facebook with Your facebook account.

When You will register on developers.faceboook we need to create a business app which will give us for sure more possibilities like: webhooks integration and direct messages.

Facebook Webhooks

I couldn't find a possibility to register webhook in instagram directly, although instagram data can be accessed with Instagram Graph API https://developers.facebook.com/docs/instagram-api/reference/ig-comment/ and as it is built on Facebook Graph API, I hope that facebook will allow us to listen on instagram comments.

After spending few minutes on facebook developers documentation I found a guide how to register an webhook.

If You want to avoid creating webhooks You can always go on with pulling(executing normal requests in high frequency), it maybe some idea for tests but in production You will for sure be blocked by exceeding api rate limit(number of request per time frame)

Facebook Webhooks and Ngrok

Add Webhooks product to Your facebook app. Your webhook must be verified and in order to do it quickly the best option is to use a proxy that will redirect to Your local machine, for that we use ngrok.

Brak alternatywnego tekstu dla tego zdjęcia

Ngrok

You need to register an account on ngrok website and download the desktop client. When You authenticate desktop client, You can run in terminal:

$ ngrok http 8000

You will see bellow screen and I will use this https://bd938978d450.ngrok.io endpoint as my webhook. If You want to upskill Your cloud computing techniques You can create AWS EC2 instance, select whatever is more appealing for You.

Brak alternatywnego tekstu dla tego zdjęcia

Webhook verification

On the webhook we created, facebook will send a GET request with those query parameters:

GET https://bd938978d450.ngrok.io?
  hub.mode=subscribe&
  hub.challenge=1158201444&
  hub.verify_token=meatyhamhock

hub.verify_token is the value we add during webhook verification, You need to make sure that this is the value You added

hub.challenge must be send back in order to accomplish webhook verification process.

FastAPI

For development we use FastAPI, it is a great and simple python web api framework. Install it:

$ pip install fastapi

$ pip install uvicorn[standard]

Uvicorn is Asynchronous Server Gateway Interface(ASGI), which take care of handling incoming messages and forwarding them to FastAPI instances.

Run below code with this command:

$ uvicorn main:app --reload

save the code in main.py

from fastapi import FastAPI, Request

app = FastAPI()


@app.get("/")
async def main(request: Request):
    
    pass

if You are using pycharm debugger, You can easily check the request body in real time.

Brak alternatywnego tekstu dla tego zdjęcia
Brak alternatywnego tekstu dla tego zdjęcia

Don't forget to return this value from Your function view:

from fastapi import FastAPI, Request

app = FastAPI()


@app.get("/")
async def main(request: Request):
    return int(request.query_params['hub.challenge'])

If all is ok You will see this screen, we will subscribe to comments.

Brak alternatywnego tekstu dla tego zdjęcia

Summary

Integration with Instagram and Facebook took me more time than expected :) and I don't want to make this article too long. So what we achieved, our bot endpoint was registered and is now ready to receive real time information from Instagram.

Secondly we know that ngrok is very useful tool during development as without it we would need to have development server and this is not needed for proof of concept like tasks.

Maybe You are interested about something or have better idea what our Instagram bot could do ? I plan to send Direct Message on Instagram when somebody will comment my content with defined keyword.

Remember to spend enough time, on checking how to integrate Your project resources, don't wait with it until development !




To view or add a comment, sign in

More articles by Igor Miazek

Explore content categories