Weather bot#

 1# You may need to add your working directory to the Python path. To do so, uncomment the following lines of code
 2# import sys
 3# sys.path.append("/Path/to/directory/bot-framework") # Replace with your directory path
 4
 5import logging
 6import random
 7
 8from besser.bot.core.bot import Bot
 9from besser.bot.core.session import Session
10from besser.bot.nlp.intent_classifier.intent_classifier_prediction import IntentClassifierPrediction
11
12# Configure the logging module
13logging.basicConfig(level=logging.INFO, format='{levelname} - {asctime}: {message}', style='{')
14
15bot = Bot('weather_bot')
16# Load bot properties stored in a dedicated file
17bot.load_properties('config.ini')
18# Define the platform your chatbot will use
19websocket_platform = bot.use_websocket_platform(use_ui=True)
20
21# STATES
22
23s0 = bot.new_state('s0', initial=True)
24weather_state = bot.new_state('weather_state')
25
26# ENTITIES
27
28city_entity = bot.new_entity('city_entity', entries={
29    'Barcelona': ['BCN', 'barna'],
30    'Madrid': [],
31    'Luxembourg': ['LUX']
32})
33
34# INTENTS
35
36weather_intent = bot.new_intent('weather_intent', [
37    'what is the weather in CITY?',
38    'weather in CITY',
39])
40weather_intent.parameter('city1', 'CITY', city_entity)
41
42# STATES BODIES' DEFINITION + TRANSITIONS
43
44
45def s0_body(session: Session):
46    session.reply('Waiting...')
47
48
49s0.set_body(s0_body)
50s0.when_intent_matched_go_to(weather_intent, weather_state)
51
52
53def weather_body(session: Session):
54    predicted_intent: IntentClassifierPrediction = session.predicted_intent
55    city = predicted_intent.get_parameter('city1')
56    temperature = round(random.uniform(0, 30), 2)
57    if city.value is None:
58        session.reply("Sorry, I didn't get the city")
59    else:
60        session.reply(f"The weather in {city.value} is {temperature}°C")
61        if temperature < 15:
62            session.reply('🥶')
63        else:
64            session.reply('🥵')
65
66
67weather_state.set_body(weather_body)
68weather_state.go_to(s0)
69
70# RUN APPLICATION
71
72if __name__ == '__main__':
73    bot.run()