简体   繁体   English

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

[英]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 我初始化了持久菜单:设置问候语,设置开始按钮(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'}
)

This is second part. 这是第二部分。 I try to repeat points 2 and 3, reset menu during using bot. 我尝试重复第2点和第3点,并在使用机器人时重置菜单。 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. 如果您通过Messenger Profile API更新菜单,则所有用户的菜单都会更改。

More details: https://developers.facebook.com/docs/messenger-platform/send-messages/persistent-menu 更多详细信息: https : //developers.facebook.com/docs/messenger-platform/send-messages/persistent-menu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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