M
M
Manuchehr Haidarow2021-04-30 04:47:18
Python
Manuchehr Haidarow, 2021-04-30 04:47:18

How to make a Facebook bot written in Python able to like?

The bot is written in Python using the Flask framework and the pumessenger and request libraries. Now it just randomly responds to any sent text or photo, video, GIF, etc.
Now I need it to be able to like posts. Example of work: I send a link to a post to the bot, and the bot on this link likes the post on this link.

import random
from flask import Flask, request
from pymessenger.bot import Bot

app = Flask(__name__)

ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN'
VERIFY_TOKEN = 'YOUR_VERIFY_TOKEN'

bot = Bot(ACCESS_TOKEN) #We will

receive messages sent by Facebook to our bot in this call terminal
@app.route('/', methods=['GET', 'POST'])
def receive_message():
if request.method == 'GET':
# before allowing people to send anything to the bot, Facebook verifies the token
# verifies that all requests received by the bot come from Facebook
token_sent = request.args['hub.verify_token' ]
return verify_fb_token(token_sent)
# if the request was not a GET, it was a POST request and we process the user's request
else:
# get the message sent by the user to the Facebook bot
output = request.get_json()
for event in output['entry' ]:
messaging = event['messaging']
for message in messaging:
if message.get('message'):
#determine the ID to know where to send the response
recipient_id = message['sender']['id']
if message['message'].get('text'):
response_sent_text = get_message()
send_message(recipient_id, response_sent_text)
#if user sent GIF, photo, video and any non-text object
if message['message'].get('attachments'):
response_sent_nontext = get_message()
send_message(recipient_id, response_sent_nontext)
return "Message Processed"

def verify_fb_token(token_sent):
'''Checks the token sent by facebook with the one you have.
If it matches, it allows the request, otherwise it throws an error.'''
if token_sent == VERIFY_TOKEN:
return request.args['hub.challenge']
else:
return 'Invalid verification token'

def send_message(recipient_id, response):
'' 'Sends a text message to the user according to the response parameter.'''
bot.send_text_message(recipient_id, response)
return 'Success'

def get_message():
'''Sends random messages to the user.'''
sample_responses = ["Awesome!", "I'm proud of you!", "Keep up the good work!", "Best thing I've ever seen!"]
return random.choice(sample_responses)

if __name__ == '__main__' :
app.run()

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Developer, 2021-04-30
@samodum

Need to add some code

H
HemulGM, 2021-04-30
@HemulGM

Learn python, learn how the wrapper works under the Facebook API. Further questions will disappear by themselves.

I
iris_duty_2020, 2021-04-30
@iris_duty_2020

See documentation. If there is a method for delivering a like, study it in more detail and connect it to the code.

L
LixaNasTya, 2021-08-01
@LixaNasTya

Why bother so much when it doesn’t work out?) If you need likes, just buy it! The Internet is full of services that increase counters. Personally, I like doctorsmm.com more (the site is easy to find - type the domain in any search engine: Google, Yandex or Bing). I periodically buy likes for my posts - the prices are low, the quality of services is high, and I have constant activity in the public)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question