Telegram agent#

  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/agentic-framework") # Replace with your directory path
  4
  5import logging
  6
  7from telegram import Update
  8from telegram.ext import CommandHandler, ContextTypes
  9
 10from besser.agent.core.agent import Agent
 11from besser.agent.core.session import Session
 12from besser.agent.exceptions.logger import logger
 13
 14# Configure the logging module (optional)
 15logger.setLevel(logging.INFO)
 16
 17agent = Agent('telegram_agent')
 18# Load agent properties stored in a dedicated file
 19agent.load_properties('config.ini')
 20# Define the platform your agent will use
 21telegram_platform = agent.use_telegram_platform()
 22
 23
 24# Adding a custom handler for the Telegram Application: command /help
 25async def help(update: Update, context: ContextTypes.DEFAULT_TYPE):
 26    session = agent.get_or_create_session(str(update.effective_chat.id), telegram_platform)
 27    session.reply('I am an agent, tell me something!')
 28help_handler = CommandHandler('help', help)
 29telegram_platform.add_handler(help_handler)
 30
 31
 32# STATES
 33
 34s0 = agent.new_state('s0', initial=True)
 35hello_state = agent.new_state('hello_state')
 36bye_state = agent.new_state('bye_state')
 37howareyou_state = agent.new_state('howareyou_state')
 38
 39# INTENTS
 40
 41hello_intent = agent.new_intent('hello_intent', [
 42    'hello',
 43    'hi'
 44])
 45
 46bye_intent = agent.new_intent('bye_intent', [
 47    'bye',
 48    'goodbye',
 49    'see you'
 50])
 51
 52howareyou_intent = agent.new_intent('howareyou_intent', [
 53    'how are you?',
 54    'how r u',
 55])
 56
 57# GLOBAL VARIABLES
 58
 59count_hello = 0
 60count_bye = 0
 61
 62# STATES BODIES' DEFINITION + TRANSITIONS
 63
 64
 65def global_fallback_body(session: Session):
 66    telegram_platform.reply(session, 'Greetings from global fallback')
 67
 68
 69# Assigned to all agent states (overriding all currently assigned fallback bodies).
 70agent.set_global_fallback_body(global_fallback_body)
 71
 72
 73def s0_body(session: Session):
 74    telegram_platform.reply(session, 'Waiting...')
 75
 76
 77s0.set_body(s0_body)
 78s0.when_intent_matched(hello_intent).go_to(hello_state)
 79s0.when_intent_matched(howareyou_intent).go_to(howareyou_state)
 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(bye_intent).go_to(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    agent.run()