BotFramework-WebChat icon indicating copy to clipboard operation
BotFramework-WebChat copied to clipboard

how to get user current location in web based bot using direct line c#

Open UsamaAslam opened this issue 8 years ago • 3 comments

hi, i am working on a bot in which i have to display offers to the user according to their current location. i use this method to get user IP address :

String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(ip)) { ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; }

but user IP that code Fetch is totally wrong ..

Is their any correct method to get user current location in bot framework C#.

UsamaAslam avatar Jul 19 '18 06:07 UsamaAslam

How about sending the IP/location to bot through channel data or an event type activity?

Kartik1607 avatar Jul 19 '18 07:07 Kartik1607

@Kartik1607 thanks for the idea. Now able to get the location using geolocation and pass the value to bot through channel data.

UsamaAslam avatar Jul 19 '18 10:07 UsamaAslam

i am using below code to pass the value to bot ActivityTypes.ConversationUpdate but i am not able to get any value in bot under ActivityTypes.ConversationUpdate. Can you help me ? Don't able to pass the value. Full address is local variable on html page that have value like in this i-e if i want to pass London to bot and in conversation start bot display London .

<script type="text/javascript">
        // for location of user....
        var fulladdress ='landon';
 
 alert(fulladdress);
         // for Bot  .......
        const speechOptions = {
            speechRecognizer: new CognitiveServices.SpeechRecognizer({ subscriptionKey: 'key' }),
            speechSynthesizer: new CognitiveServices.SpeechSynthesizer({
                gender: CognitiveServices.SynthesisGender.Female,
                subscriptionKey: 'key',
                voiceName: 'Microsoft Server Speech Text to Speech Voice (en-US, JessaRUS)'
            })
        };
      
        var model = {
            "userId": '_' + Math.random().toString(36).substr(2, 9),
            "userName": "User Name",
            "botId": "Chatbot Main Tree",
            "botIconUrl": "//bot-framework.azureedge.net/bot-icons-v1/bot-framework-default-8.png",
            "botName": "Chatbot Main Tree",
            "secret": "Direct Line Secret",
            "iconUrl": "//bot-framework.azureedge.net/bot-icons-v1/bot-framework-default-8.png",
            "directLineUrl": "https://webchat.botframework.com/v3/directline",
            "webSocketEnabled": "True"
        };

        var dl = new BotChat.DirectLine(
            {
                  secret: model.secret,
                  token: model.token,
                  domain: model.directLineUrl,
                  webSocket: true,
                  sendTyping: true
            });

        BotChat.App({
            botConnection: {
                botConnection: dl,
                postActivity: activity => dl.postActivity({
                    from: { id: 'userid', name: 'username' },
                    name: 'postLocationData',
                    text: 'I said my location is ' + fulladdress,
                    type: 'ConversationUpdate',
                    value: fulladdress,
                    channelData: fulladdress
                })
                    .subscribe(function (id) {
                        console.log(fulladdress);
                    })
            },
            sendTyping: true,
            user: { id: model.userId, name: model.userName },
            bot: { id: model.botId, name: model.botName },
            speechOptions: speechOptions,
            resize: 'window',
        }, document.getElementById("BotChatElement"));
</script>

UsamaAslam avatar Jul 24 '18 07:07 UsamaAslam