在Facebook Messenger上将持久性菜单更改为其他语言

问题描述 投票:0回答:1

我制作了多种语言(俄语,英语,希伯来语)的机器人,并希望任何用户都可以将持久性菜单切换到其他语言。他能这样吗?

这是第一部分。我初始化了持久性菜单:设置问候语,设置开始按钮(GET_STARTED_PAYLOAD)和设置菜单

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'}
)

这是第二部分。我尝试重复第2点和第3点,在使用bot时重置菜单。菜单没有任何反应,机器人不响应其他命令

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'}
    )
python-3.x facebook messenger
1个回答
0
投票

持久性菜单存储在页面级别上,并且不能为每个单独的用户定制。如果您通过Messenger Profile API更新菜单,则会为所有用户进行更改。

更多细节:https://developers.facebook.com/docs/messenger-platform/send-messages/persistent-menu

© www.soinside.com 2019 - 2024. All rights reserved.