def returnOutputMessage(): try: output: dict = request.get_json() message: dict = output['entry'][0]['messaging'][0] if message.get('message'): recipient_id: str = message['sender']['id'] if message['message'].get('text'): text: str = message['message'].get('text') output_msg = text bot = Bot(ACCESS_TOKEN) bot.send_text_message(recipient_id, output_msg) return 'réalisation avec succès' except: return 'Nous rencontrons un problème à recevoir votre message'
class FacebookHandler(object): verify_token: str = None bot: Bot = None ggly: Ggly = None work_dir: str = None def __init__(self, ggly: Ggly, access_token: str, verify_token: str): super().__init__() if access_token is None: logging.warning( "Facebook access token not set. You will not be able to use messenger bot." ) self.verify_token = verify_token self.bot = Bot(access_token) self.ggly = ggly self.work_dir = get_work_dir() def handle_http_request(self, request): if request.method == 'GET': logging.debug('FB verify token request') return verify_fb_token(request, self.verify_token) else: payload = request.get_json() logging.info('FB request: %s', payload) for event in payload['entry']: for msg in event['messaging']: msg_recipient_id = msg['sender']['id'] if msg.get('message'): msg_img_attachments = filter_attachments( msg['message'].get('attachments', [])) if len(msg_img_attachments) == 0: self.__send_text( msg_recipient_id, "Send me a picture of a human face") else: upload_as_json(WORKER_GCS_BUCKET, generate_basename() + '.json', msg) else: self.__send_text(msg_recipient_id, "Send me a picture of a human face") return Response('OK', status=200) def handle_gcs_event(self, data, context): logging.info('FB event: %s %s', data, context) gcs_file_path = format(data['name']) msg, metadata = download_json(WORKER_GCS_BUCKET, gcs_file_path) if metadata.get(KEY_STATUS, BLOB_STATUS_PENDING) != BLOB_STATUS_PENDING: logging.warning('Ignoring blob %s because of its status', gcs_file_path) return update_metadata(WORKER_GCS_BUCKET, gcs_file_path, {KEY_STATUS: STATUS_PROCESSING}) msg_recipient_id = msg['sender']['id'] try: self.__send_text(msg_recipient_id, "Let's see what we've got here") msg_img_attachments = filter_attachments(msg['message'].get( 'attachments', [])) for idx, attachment in enumerate(msg_img_attachments): file_basename = gcs_file_path.split('/')[-1].replace( '.json', '_' + str(idx)) self.__process_url(msg_recipient_id, file_basename, attachment) update_metadata(WORKER_GCS_BUCKET, gcs_file_path, {KEY_STATUS: BLOB_STATUS_DONE}) except Exception as e: logging.error(e) if msg_recipient_id: self.__send_text(msg_recipient_id, "I couldn't make it") update_metadata(WORKER_GCS_BUCKET, gcs_file_path, {KEY_STATUS: BLOB_STATUS_ERROR}) def __process_url(self, msg_recipient_id, file_basename, attachment): src_path = '%s/%s.jpg' % (self.work_dir, file_basename) dest_path = '%s/%s_ggly.jpg' % (self.work_dir, file_basename) try: urllib.request.urlretrieve(attachment['payload']['url'], src_path) upload(IMG_GCS_BUCKET, src_path) output, face_count = self.ggly.go(cv2.imread(src_path)) if face_count > 0: cv2.imwrite(dest_path, output) dest_url = upload(IMG_GCS_BUCKET, dest_path, True) self.__send_image_url(msg_recipient_id, dest_url) else: self.__send_text(msg_recipient_id, "I can't recognize any faces on the picture") return face_count finally: delete_if_exists(src_path) delete_if_exists(dest_path) def __send_text(self, recipient_id, text): logging.debug("Sending text message `%s` to %s", text, recipient_id) self.bot.send_text_message(recipient_id, text) def __send_image_url(self, recipient_id, image_url): logging.debug("Sending image URL `%s` to %s", image_url, recipient_id) self.bot.send_image_url(recipient_id, image_url)
response = "Are you facing any sort of work pressure?" buttons = [ { 'type':'postback', 'title':'yes', 'payload':'q13_yes' }, { 'type':'postback', 'title':'no', 'payload':'q13_no' } ] if payload_name == "q12_yes": sol="Consult a doctor and take proper medications!" bot.send_text_message(sender_id, sol) bot.send_button_message(sender_id,response,buttons) elif payload_name == "q13_yes" or payload_name == "q13_no": response = "Are you addicted to say, social media?" buttons = [ { 'type':'postback', 'title':'yes', 'payload':'q14_yes' }, { 'type':'postback', 'title':'no', 'payload':'q14_no' } ]
index_of_dollar_sign = text.index('$') # => int # Retrieving the price from the page. price = text[index_of_dollar_sign:index_of_dollar_sign + 5] # => str return price # Send a text to the page sender_id = '1530178270396594' recipient_id = '682020518574190' app_id = '137799076998371' while True: # coffee price from the predefined function price = coffee_price(120) # => str if price < "$4.74": message = "Hi, Mr. CEO of StarBuzz.\nDropped price of Coffee: %s" % price # => str # Sending a direct message to a facebook page account bot.send_text_message(sender_id, message) # => NoneType else: message = "Hi, Mr. CEO of StarBuzz.\nEmergency price of Coffee: %s" % price #=> str # Sending a direct message to a facebook page account bot.send_text_message(sender_id, message)