0

I use fbchat module to listen to my message and use my response as an input for a captcha. I have everything figured out except the last line when I want to call my class variable. Any ideas ?

This is my code :

from fbchat import Client
from fbchat.models import *
import fbchat
from fbchat import log, Client

# Subclass fbchat.Client and override required methods
class EchoBot(Client):
   def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs):
       self.markAsDelivered(thread_id, message_object.uid)
       self.markAsRead(thread_id)

       log.info("{} from {} in {}".format(message_object, thread_id, thread_type.name))

       # If you're not the author, echo
       if author_id != self.uid:
           self.send(message_object, thread_id="id", thread_type=ThreadType.USER)

       captchaResponse = str(message_object.text) # This is the text it receive 




client = EchoBot("mail", "password")
client.listen()

captchaInput = driver.find_element_by_xpath("//input[@id='captchaResponse']")
captchaImage = driver.find_element_by_id("captchaTag")
captchaImage.screenshot("captcha/captcha.png")
captchaImage = cv2.imread('captcha/captcha.png')
captchaInput.send_keys(captchaResponse, Keys.ENTER) # This is where I'm stuck

EDIT:

So the problem was that I needed to add this line at the end of my function before I could do anything else.

Client.stopListening(self)

1 Answers1

0

You're declaring captchaResponse inside onMessage's function scope, meaning it's not available on the outside.

Declare it before the class, then access the outer captchaResponse with the global keyword to override it from inside the function.

captchaResponse = None

class EchoBot(Client):
    def onMessage(self, author_id, message_object, thread_id, thread_type, **kwargs):
        ...
        global captchaResponse 
        captchaResponse = str(message_object.text) # This is the text it receive 

Which should then make it available to be used in captchaInput.send_keys.

Related thread on using global variables in a function

TARN4T1ON
  • 522
  • 2
  • 12
  • Yeah I tried but for some reason it doesn't work. If I ask for a simple print I have nothing... Weird. –  Dec 21 '20 at 15:45
  • EDIT: I needed to close the listener before doing anything else. –  Dec 21 '20 at 15:53