2

I'm trying to set my chatbot to display a typing bubble before sending the user the response so that it's more human like. I followed the documentation but am still getting instant responses from the bot with no typing bubble. As per the documentation I am using this sender action:

{"recipient":{"id":recipient_id}, "sender_action":"typing_on"}

And to put it in context, here is how I'm using it in the flask app:

@app.route('/', methods=['GET', 'POST'])
def receive_message():
    global tag, latest_message
    if request.method == 'GET':
        # Before allowing people to message your bot Facebook has implemented a verify token
        # that confirms all requests that your bot receives came from Facebook.
        token_sent = request.args.get("hub.verify_token")
        return verify_fb_token(token_sent)
    # If the request was not GET, it  must be POSTand we can just proceed with sending a message
    # back to user
    else:
            # get whatever message a user sent the bot
        output = request.get_json()
        for event in output['entry']:
            messaging = event['messaging']
            for message in messaging:
                if message.get('message'):
                    # Facebook Messenger ID for user so we know where to send response back to
                    recipient_id = message['sender']['id']
                    if message['message'].get('text'):
                        response_sent_text = send(message['message'].get('text'))
                        send_message(recipient_id, response_sent_text)
                        latest_message = response_sent_text
    return "Message Processed"

def send_message(recipient_id, response):
    # sends user the text message provided via input response parameter
    typing_payload = {"recipient":{"id":recipient_id}, "sender_action":"typing_on"}
    bot.send_raw(typing_payload)
    print(bot.send_raw(typing_payload))
    bot.send_text_message(recipient_id, response)
    return "success"

Any ideas would be much appreciated!

Jamerson2
  • 151
  • 5

1 Answers1

1

You should pass a string representation of the payload (not a dictionary).

try doing something like this:

import json

...


def send_message(recipient_id, response):
    # sends user the text message provided via input response parameter
    typing_payload = json.dumps({"recipient":{"id":recipient_id}, "sender_action":"typing_on"})
    bot.send_raw(typing_payload)
    print(bot.send_raw(typing_payload))
    bot.send_text_message(recipient_id, response)
    return "success"

Also it's best practice to send these indicators upon receiving message (to show the typing indicator while processing), as opposed to directly before sending message.

documentation

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
  • Thanks! I tried this but still not seeing a typing bubble. I also tried sending the indictor upon receiving the message. – Jamerson2 Aug 24 '20 at 21:19
  • Seems like this works fine for other sender actions such as "mark_seen", but yields no result for "typing_on" for some reason. Perhaps an issue on the facebook side? – Jamerson2 Aug 25 '20 at 02:20