Greetings 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
6
7from besser.bot.core.bot import Bot
8from besser.bot.core.session import Session
9
10# Configure the logging module
11logging.basicConfig(level=logging.INFO, format='{levelname} - {asctime}: {message}', style='{')
12
13# Create the bot
14bot = Bot('greetings_bot')
15# Load bot properties stored in a dedicated file
16bot.load_properties('config.ini')
17# Define the platform your chatbot will use
18websocket_platform = bot.use_websocket_platform(use_ui=True)
19
20# STATES
21
22initial_state = bot.new_state('initial_state', initial=True)
23hello_state = bot.new_state('hello_state')
24good_state = bot.new_state('good_state')
25bad_state = bot.new_state('bad_state')
26
27# INTENTS
28
29hello_intent = bot.new_intent('hello_intent', [
30 'hello',
31 'hi',
32])
33
34good_intent = bot.new_intent('good_intent', [
35 'good',
36 'fine',
37])
38
39bad_intent = bot.new_intent('bad_intent', [
40 'bad',
41 'awful',
42])
43
44
45# STATES BODIES' DEFINITION + TRANSITIONS
46
47
48initial_state.when_intent_matched_go_to(hello_intent, hello_state)
49
50
51def hello_body(session: Session):
52 session.reply('Hi! How are you?')
53
54
55hello_state.set_body(hello_body)
56hello_state.when_intent_matched_go_to(good_intent, good_state)
57hello_state.when_intent_matched_go_to(bad_intent, bad_state)
58
59
60def good_body(session: Session):
61 session.reply('I am glad to hear that!')
62
63
64good_state.set_body(good_body)
65good_state.go_to(initial_state)
66
67
68def bad_body(session: Session):
69 session.reply('I am sorry to hear that...')
70
71
72bad_state.set_body(bad_body)
73bad_state.go_to(initial_state)
74
75
76# RUN APPLICATION
77
78if __name__ == '__main__':
79 bot.run()