Telegram 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 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
 68def s0_body(session: Session):
 69    telegram_platform.reply(session, 'Waiting...')
 70
 71
 72s0.set_body(s0_body)
 73s0.when_intent_matched_go_to(hello_intent, hello_state)
 74s0.when_intent_matched_go_to(howareyou_intent, howareyou_state)
 75
 76# Assigned to all bot states (overriding all currently assigned fallback bodies).
 77# In this case, it replaces besser.bot.library.state.StateLibrary.default_fallback_body only on s0, since it is the only
 78# defined state at this point of the code
 79bot.set_global_fallback_body(global_fallback_body)
 80
 81
 82def hello_body(session: Session):
 83    global count_hello
 84    count_hello = count_hello + 1
 85    telegram_platform.reply(session, f'You said hello {count_hello} times')
 86
 87
 88# Custom fallback for hello_state
 89def hello_fallback_body(session: Session):
 90    telegram_platform.reply(session, 'Greetings from hello fallback')
 91
 92
 93hello_state.set_body(hello_body)
 94hello_state.set_fallback_body(hello_fallback_body)
 95hello_state.when_intent_matched_go_to(bye_intent, bye_state)
 96
 97
 98def bye_body(session: Session):
 99    global count_bye
100    count_bye = count_bye + 1
101    telegram_platform.reply(session, f'You said bye {count_bye} times')
102
103
104bye_state.set_body(bye_body)
105bye_state.go_to(s0)
106
107
108def howareyou_body(session: Session):
109    telegram_platform.reply(session, f'I am fine, thanks!')
110
111
112howareyou_state.set_body(howareyou_body)
113howareyou_state.go_to(s0)
114
115# RUN APPLICATION
116
117if __name__ == '__main__':
118    bot.run()