Conversation Design Using Local Time and Weather
# 📖tutorials
f
Here in Britain we love to moan about the weather in most daily conversations we have with each other, its just what we do. haha! So i thought why not build that into the bot to make conversations feel more natural. I guess you could also use this for say a bot that tells the weather or I am sure someone will think of some more use cases. You can completely change what you want the bot to say to make it personalised for your use case.
Here's a breakdown of what each part of the code does: 1. API Key and Location Information: - An API key (
apiKey
) is provided for accessing the OpenWeatherMap API. - The city (
city
) and country (
country
) are specified as "Moscow" and "Russia," respectively. 2. Current Time: - The Luxon library is used to get the current time in the local timezone. - The
currentTime
variable holds the current time in a simple time format (e.g., "9:30 AM"). 3. Fetching Weather Data: - The weather data is fetched from the OpenWeatherMap API using Axios. - The
weatherUrl
is constructed with the city, country, API key, and units as metric. - The
weatherResponse
holds the response from the API. - The current temperature is extracted from the API response using Lodash's
_.get
function. 4. Getting Weather Conditions and Cloud Coverage: - The weather conditions (e.g., "Clouds," "Rain") are extracted from the API response. - The cloud coverage percentage is also extracted from the API response.
5. Time-based Greetings: - Based on the current hour, a suitable greeting message is assigned to the
greeting
variable. - Different greetings are chosen for different time ranges (morning, afternoon, evening, etc.). 6. Constructing Welcome Message: - The
welcomeMessage
is constructed by combining the
greeting
with other messages. 7. Checking Cloud Conditions: - The cloud coverage percentage is used to add information about the sky conditions to the
welcomeMessage
. - Different cloud coverage ranges trigger different messages. 8. Checking for Rain: - If the weather conditions include "Rain," the message is appended to indicate rain. 9. Checking Temperature Ranges: - Depending on the temperature range, the message about the temperature is added. - Different temperature ranges lead to different comments. 10. Finalizing Welcome Message: - The temperature information and city name are added to the
welcomeMessage
. - The
welcomeMessage
is then stored in a variable called
workflow.welcome1
.
to use the weather function you will need an API key with www.openweathermap.org - you can get 1000 free api calls per day. here is the code below: I had to upload a file as it ran over my character limit per message. I have marked out where to change the code with capital letters.
c
Oh wowwwww 💖 !!!!!! Amazing work. Thanks for sharing with the community @freezing-carpenter-9818 !
l
I have problem with getting user's location and local time to use for some process, but I always get the Botpress information ( Auburn Virginia). I want to use https://ipapi.co/json/ to get the location.
c
@early-train-33247 can you please help here!
e
@lemon-rocket-12038 your bot's code always run on Botpress Servers, so we don't have access to your user IP.
The workarounds: - Creating a website and adding a script that makes a request to ipapi and then passes that information to the Webchat which will make it available for your bot in the Studio. Check out this docs https://botpress.com/docs/cloud/channels/webchat/#send-user-data-from-your-website-to-botpress and this tutorial

https://www.youtube.com/watch?v=FKH3b9NlyaI

- Asking your user for their timezone and language. Consider that sometimes the IP doesn't match the user location 😉
Hope that helps
c
Appreciate it Gui. Thanks
l
Thank you @early-train-33247 .
2 Views