简体   繁体   中英

Change persistent menu to other language on facebook messenger

I make a multiple languages (Russian, English, Hebrew) bot and want that any user can switch persistent menu to other lang. Does he can this?

This is first part. I initialized persistent menu: set greeting, set start button (GET_STARTED_PAYLOAD) and set menu

def init_facebook() -> None:
"""
Init configuration for the facebook bot.
"""
# Set greeting for the bot
requests.post(
    url=API_URL,
    params={'access_token': PAT},
    data=json.dumps({
        'greeting': [
            {"locale": "default",
             "text": 'Welcome to the'
                     ' Jew Calendar Bot!\nFor start,'
                     ' press the \"Get Started\" button and send your'
                     ' location.\nFor help press'
                     ' \"Contact\" button.'},
            {"locale": "en_US",
             "text": 'Welcome to the'
                     ' Jew Calendar Bot!\nFor start,'
                     ' press the \"Get Started\" button and send your'
                     ' location.\nFor help press'
                     ' \"Contact\" button.'
             },
            {"locale": "ru_RU",
             "text": 'Добро пожаловать'
                     ' в Jew Calendar Bot!\nЧтобы начать,'
                     ' нажмите кнопку \"Начать\" и'
                     ' отправьте вашу геолокацию.\nДля справки нажмите'
                     ' кнопку \"Связаться\".'
             }
        ]

    }),
    headers={'Content-type': 'application/json'}
)
# Set start button for the bot
requests.post(
    url=API_URL,
    params={'access_token': PAT},
    data=json.dumps({
        'get_started': {
            'payload': 'GET_STARTED_PAYLOAD'
        }
    }),
    headers={'Content-type': 'application/json'}
)

# Set persistent menu for the bot
requests.post(
    url=API_URL,
    params={'access_token': PAT},
    data=json.dumps({
        'persistent_menu': [
            {
                'locale': 'default',
                'composer_input_disabled': True,
                'call_to_actions': [
                    {
                        'title': 'Change Language',
                        'type': 'nested',
                        'call_to_actions': [
                            {
                                'title': 'English',
                                'type': 'postback',
                                'payload': 'ENGLISH_PAYLOAD'
                            },
                            {
                                'title': 'Русский',
                                'type': 'postback',
                                'payload': 'RUSSIAN_PAYLOAD'
                            },
                            {
                                'title': 'עִברִית',
                                'type': 'postback',
                                'payload': 'HEBREW_PAYLOAD'
                            }
                        ]
                    },
                    {
                        'title': 'Update location',
                        'type': 'postback',
                        'payload': 'UPDATE_LOCATION_PAYLOAD_EN'
                    },
                    {
                        'title': 'Contact',
                        'type': 'web_url',
                        'url': 'http://telegra.ph/'
                               'Hebrew-Calendar-Bot-FAQ-05-10',

                    }
                ]
            },
            {
                'locale': 'en_US',
                'composer_input_disabled': True,
                'call_to_actions': [
                    {
                        'title': 'Change Language',
                        'type': 'nested',
                        'call_to_actions': [
                            {
                                'title': 'English',
                                'type': 'postback',
                                'payload': 'ENGLISH_PAYLOAD'
                            },
                            {
                                'title': 'Русский',
                                'type': 'postback',
                                'payload': 'RUSSIAN_PAYLOAD'
                            },
                            {
                                'title': 'עִברִית',
                                'type': 'postback',
                                'payload': 'HEBREW_PAYLOAD'
                            }
                        ]
                    },
                    {
                        'title': 'Update location',
                        'type': 'postback',
                        'payload': 'UPDATE_LOCATION_PAYLOAD_EN'
                    },
                    {
                        'title': 'Contact',
                        'type': 'web_url',
                        'url': 'http://telegra.ph/'
                               'Hebrew-Calendar-Bot-FAQ-05-10',

                    }
                ]
            },
            {
                'locale': 'ru_RU',
                'composer_input_disabled': True,
                'call_to_actions': [
                    {
                        'title': 'Сменить Язык',
                        'type': 'nested',
                        'call_to_actions': [
                            {
                                'title': 'English',
                                'type': 'postback',
                                'payload': 'ENGLISH_PAYLOAD'
                            },
                            {
                                'title': 'Русский',
                                'type': 'postback',
                                'payload': 'RUSSIAN_PAYLOAD'
                            },
                            {
                                'title': 'עִברִית',
                                'type': 'postback',
                                'payload': 'HEBREW_PAYLOAD'
                            }
                        ]
                    },
                    {
                        'title': 'Обновить местоположение',
                        'type': 'postback',
                        'payload': 'UPDATE_LOCATION_PAYLOAD_RU'
                    },
                    {
                        'title': 'Связаться',
                        'type': 'web_url',
                        'url': 'http://telegra.ph/'
                               'Hebrew-Calendar-Bot-FAQ-05-10',

                    }
                ]
            },
            {
                'locale': 'he_IL',
                'composer_input_disabled': True,
                'call_to_actions': [
                    {
                        'title': 'לשנות את השפה',
                        'type': 'nested',
                        'call_to_actions': [
                            {
                                'title': 'English',
                                'type': 'postback',
                                'payload': 'ENGLISH_PAYLOAD'
                            },
                            {
                                'title': 'Русский',
                                'type': 'postback',
                                'payload': 'RUSSIAN_PAYLOAD'
                            },
                            {
                                'title': 'עִברִית',
                                'type': 'postback',
                                'payload': 'HEBREW_PAYLOAD'
                            }
                        ]
                    },
                    {
                        'title': 'עדכון מיקום',
                        'type': 'postback',
                        'payload': 'UPDATE_LOCATION_PAYLOAD_IL'
                    },
                    {
                        'title': 'צור קשר',
                        'type': 'web_url',
                        'url': 'http://telegra.ph/'
                               'Hebrew-Calendar-Bot-FAQ-05-10',

                    }
                ]
            }
        ]
    }),
    headers={'Content-type': 'application/json'}
)

This is second part. I try to repeat points 2 and 3, reset menu during using bot. Nothing happens with menu and bot doesn't respond to other commands

def send_message(token: str, recipient: str, message: dict,
             message_type: str) -> None:
"""
    Send the message text to recipient with id recipient.
"""
if message_type is 'postback':
    r = requests.post(
        url=API_URL_PROFILE,
        params={'access_token': token},
        data=json.dumps({
            'get_started': {
                'payload': 'GET_STARTED_PAYLOAD'
            }
        }),
        headers={'Content-type': 'application/json'}
    )
    print(f'Set get_started result: status {r.status_code},'
          f' response {r.text}')

    requests.post(
        url=API_URL_PROFILE,
        params={'access_token': token},
        data=json.dumps(message),
        headers={'Content-type': 'application/json'})
else:
    requests.post(
        API_URL_MESSAGE,
        params={'access_token': token},
        data=json.dumps({
            "messaging_type": "RESPONSE",
            'recipient': {'id': recipient},
            'message': message
        }),
        headers={'Content-type': 'application/json'}
    )

The persistent menu is stored on the page level and cannot be customized for each individual user as things stand. If you update the menu via the Messenger Profile API, it will change for all of your users.

More details: https://developers.facebook.com/docs/messenger-platform/send-messages/persistent-menu

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM