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 telegram import Update
8from telegram.ext import CommandHandler, ContextTypes
9
10from besser.bot.core.bot import Bot
11from besser.bot.core.session import Session
12
13# Configure the logging module
14logging.basicConfig(level=logging.INFO, format='{levelname} - {asctime}: {message}', style='{')
15
16bot = Bot('telegram_bot')
17# Load bot properties stored in a dedicated file
18bot.load_properties('config.ini')
19# Define the platform your chatbot will use
20telegram_platform = bot.use_telegram_platform()
21
22
23# Adding a custom handler for the Telegram Application: command /help
24async def help(update: Update, context: ContextTypes.DEFAULT_TYPE):
25 session = bot.get_or_create_session(str(update.effective_chat.id), telegram_platform)
26 session.reply('I am a bot, tell me something!')
27help_handler = CommandHandler('help', help)
28telegram_platform.add_handler(help_handler)
29
30
31# STATES
32
33s0 = bot.new_state('s0', initial=True)
34hello_state = bot.new_state('hello_state')
35bye_state = bot.new_state('bye_state')
36howareyou_state = bot.new_state('howareyou_state')
37
38# INTENTS
39
40hello_intent = bot.new_intent('hello_intent', [
41 'hello',
42 'hi'
43])
44
45bye_intent = bot.new_intent('bye_intent', [
46 'bye',
47 'goodbye',
48 'see you'
49])
50
51howareyou_intent = bot.new_intent('howareyou_intent', [
52 'how are you?',
53 'how r u',
54])
55
56# GLOBAL VARIABLES
57
58count_hello = 0
59count_bye = 0
60
61# STATES BODIES' DEFINITION + TRANSITIONS
62
63
64def global_fallback_body(session: Session):
65 telegram_platform.reply(session, 'Greetings from global fallback')
66
67
68# Assigned to all bot states (overriding all currently assigned fallback bodies).
69bot.set_global_fallback_body(global_fallback_body)
70
71
72def s0_body(session: Session):
73 telegram_platform.reply(session, 'Waiting...')
74
75
76s0.set_body(s0_body)
77s0.when_intent_matched_go_to(hello_intent, hello_state)
78s0.when_intent_matched_go_to(howareyou_intent, howareyou_state)
79
80
81def hello_body(session: Session):
82 global count_hello
83 count_hello = count_hello + 1
84 telegram_platform.reply(session, f'You said hello {count_hello} times')
85
86
87# Custom fallback for hello_state
88def hello_fallback_body(session: Session):
89 telegram_platform.reply(session, 'Greetings from hello fallback')
90
91
92hello_state.set_body(hello_body)
93hello_state.set_fallback_body(hello_fallback_body)
94hello_state.when_intent_matched_go_to(bye_intent, bye_state)
95
96
97def bye_body(session: Session):
98 global count_bye
99 count_bye = count_bye + 1
100 telegram_platform.reply(session, f'You said bye {count_bye} times')
101
102
103bye_state.set_body(bye_body)
104bye_state.go_to(s0)
105
106
107def howareyou_body(session: Session):
108 telegram_platform.reply(session, f'I am fine, thanks!')
109
110
111howareyou_state.set_body(howareyou_body)
112howareyou_state.go_to(s0)
113
114# RUN APPLICATION
115
116if __name__ == '__main__':
117 bot.run()