how to get user current location in web based bot using direct line c#
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#.
How about sending the IP/location to bot through channel data or an event type activity?
@Kartik1607 thanks for the idea. Now able to get the location using geolocation and pass the value to bot through channel data.
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>