Search code examples
botsbotframeworkmicrosoft-teamsngrok

MS Teams Botframework - getting the "from.name" field


I have a bot running from a desktop <> ngrok <> azure <> MS Teams as a webchat. It is written in Python. Is there a way I can read the field from.name via Botframework/Python (the string USERNAME in the example below)?

The documentation says to use recipient.name, but my recipient.name is the bot's name as it is being run as a webchat (not installed in MS Teams).

I see the following POST API info via ngrok.

{    "text": "hello...",    
"textFormat": "plain",    
"type": "message",    
"timestamp": "2022-09-25T05:26:29.629655Z",    
"localTimestamp": "2022-09-25T13:26:29.629655+08:00",   
"id": "XXXXXX",    
"channelId": "msteams",    
"serviceUrl": "https://smba.trafficmanager.net/XXXX",    
"from": {        "id": "XXXXX",        "name": "USERNAME",        "aadObjectId": "XXXX"    },    
"conversation": {        "conversationType": "personal",        "tenantId": "XXXX",        "id": "XXXXXX"    },    
"recipient": {        "id": "XXXXXXX",        "name": "BOTNAME"    },    
"entities": [        {            "locale": "en-US",            "country": "XX",            "platform": "iOS",            "timezone": "XX/XX",            "type": "clientInfo"        }    ],    "channelData": {"tenant": {"id": "XXXXXX"}},"locale": "en-US","localTimezone": "XX/XX"}

Solution

  • If you want to read the field from.name via Botframework/Python.You can use the below code-

      name = turn_context.activity.from_property.name
                await turn_context.send_activity(
                    f"It is a good practice to welcome the user and provide personal greeting. For example: Welcome {name}"
                )
    

    Reference Sample-https://github.com/microsoft/BotBuilder-Samples/blob/main/samples/python/03.welcome-user/bots/welcome_user_bot.py#L100