Simple Telegram Bot To Reply User Queries Using Dialogflow

Here is a simple way to create a Telegram bot using Python that connects to Dialogflow and is capable of replying to user queries to purchase books:

First, make sure you have Python installed on your computer. If you don’t have it, you can download and install it from https://www.python.org/.

Next, install the python-telegram-bot library, which will make it easy to interact with the Telegram API. You can do this by running the following command in your terminal:

pip install python-telegram-bot

Then, create a new Dialogflow agent and give it a name, such as “Bookstore Bot”. This will be the AI that powers your bot and helps it understand and respond to user queries.

Once you have created your Dialogflow agent, you can use the dialogflow Python library to connect to it and send and receive messages. You can install this library by running the following command in your terminal:

pip install dialogflow

With the python-telegram-bot and dialogflow libraries installed, you can now start writing the code for your bot. The basic structure of the bot will be as follows:

from telegram.ext import Updater, CommandHandler
from dialogflow import types

def start(update, context):
    # This function will be called when the user sends the /start command
    # You can use this function to send a welcome message to the user
    pass

def search(update, context):
    # This function will be called when the user sends a query to purchase a book
    # You can use this function to send a request to Dialogflow and get a response
    pass

# Create a new Updater object and attach the start and search functions as handlers
updater = Updater(token="<your-telegram-bot-token>", use_context=True)
start_handler = CommandHandler("start", start)
search_handler = CommandHandler("search", search)
updater.dispatcher.add_handler(start_handler)
updater.dispatcher.add_handler(search_handler)


# Start the bot
updater.start_polling()
  • In the start function, you can send a welcome message to the user by calling the send_message method on the update.message.chat object. For example:
def start(update, context):
    update.message.chat.send_message("Hi there! I'm a book search bot. Use the /search command to find books.")

In the search function, you can use the detect_intent method of the dialogflow.SessionsClient class to send the user’s query to Dialogflow and get a response. This method takes the following arguments: project_id: The ID of your Dialogflow agent session_id: A unique ID for the current session (you can generate a random ID or use the user’s Telegram ID) query_input: The user’s query, represented as a QueryInput object

Here is an example of how you can use the detect_intent method to send a query to Dialogflow and get a response, as follows:

def search(update, context):
    # Get the user's query
    query = update.message.text
    
    # Create a new QueryInput object
    query_input = types.QueryInput(text=types.TextInput(query=query))
    
    # Send the query to Dialogflow and get the response
    response = dialogflow.SessionsClient().detect_intent(
        project_id="<your-dialogflow-project-id>",
        session_id=str(update.message.chat.id),
        query_input=query_input
    )
    
    # Get the response text from the Dialogflow response
    response_text = response.query_result.fulfillment_text
    
    # Send the response text back to the user
    update.message.chat.send_message(response_text)

This code sends the user’s query to Dialogflow using the detect_intent method, and then gets the response text from the query_result.fulfillment_text field of the response. Finally, it sends the response text back to the user using the send_message method.

You can then add additional logic to your search function to handle the different types of responses that Dialogflow can send, such as list of books or error messages if the query is not understood.

For example, you can check the query_result.intent.display_name field of the response to determine which intent was matched by Dialogflow, and then handle the response accordingly.

def search(update, context):
    # Get the user's query
    query = update.message.text
    
    # Create a new QueryInput object
    query_input = types.QueryInput(text=types.TextInput(query=query))
    
    # Send the query to Dialogflow and get the response
    response = dialogflow.SessionsClient().detect_intent(
        project_id="<your-dialogflow-project-id>",
        session_id=str(update.message.chat.id),
        query_input=query_input
    )
    
    # Check the intent name
    if response.query_result.intent.display_name == "purchase_book":
        # Handle the response for the purchase_book intent
        
        # Get the book title from the response parameters
        book_title = response.query_result.parameters["book_title"]
        
        # Send a message to the user with the book title
        update.message.chat.send_message(f"Here is the book you asked for: {book_title}")
    elif response.query_result.intent.display_name == "search_books":
        # Handle the response for the search_books intent
        
        # Get the list of books from the response parameters
        books = response.query_result.parameters["books"]
        
        # Format the list of books as a string
        books_str = "\n".join(books)
        
        # Send a message to the user with the list of books
        update.message.chat.send_message(f"Here are the books you can choose from:\n{books_str}")
    else:
        # Handle other intents or errors
        
        # Get the response text from the Dialogflow response
        response_text = response.query_result.fulfillment_text
        
        # Send the response text back to the user
        update.message.chat.send_message(response_text)

This code checks the intent name in the Dialogflow response and handles the response differently depending on which intent was matched. For the purchase_book intent, it gets the book title and sends the book the user asked for.

There’s also an additional feature to your search function to handle different types of responses from Dialogflow. For example, you can handle the case where Dialogflow returns a list of books where the intent is search_books.

The complete code would look like this.

from telegram.ext import Updater, CommandHandler
from dialogflow import types

def start(update, context):
    update.message.chat.send_message("Hi there! I'm a book search bot. Use the /search command to find books.")

def search(update, context):
    # Get the user's query
    query = update.message.text
    
    # Create a new QueryInput object
    query_input = types.QueryInput(text=types.TextInput(query=query))
    
    # Send the query to Dialogflow and get the response
    response = dialogflow.SessionsClient().detect_intent(
        project_id="<your-dialogflow-project-id>",
        session_id=str(update.message.chat.id),
        query_input=query_input
    )
    
    # Check the intent name
    if response.query_result.intent.display_name == "purchase_book":
        # Handle the response for the purchase_book intent
        
        # Get the book title from the response parameters
        book_title = response.query_result.parameters["book_title"]
        
        # Send a message to the user with the book title
        update.message.chat.send_message(f"Here is the book you asked for: {book_title}")
    elif response.query_result.intent.display_name == "search_books":
        # Handle the response for the search_books intent
        
        # Get the list of books from the response parameters
        books = response.query_result.parameters["books"]
        
        # Format the list of books as a string
        books_str = "\n".join(books)
        
        # Send a message to the user with the list of books
        update.message.chat.send_message(f"Here are the books you can choose from:\n{books_str}")
    else:
        # Handle other intents or errors
        
        # Get the response text from the Dialogflow response
        response_text = response.query_result.fulfillment_text
        
        # Send the response text back to the user
        update.message.chat.send_message(response_text)

# Create a new Updater object and attach the start and search functions as handlers
updater = Updater(token="<your-telegram-bot-token>", use_context=True)
start_handler = CommandHandler("start", start)
search_handler = CommandHandler("search", search)
updater.dispatcher.add_handler(start_handler)
updater.dispatcher.add_handler(search_handler)

# Start the bot
updater.start_polling()

Once deployed, the user can send a query to the bot using the /search command, and the bot will use Dialogflow to understand and respond to the query.

For example, if the user sends the query “I want to buy the book ‘The Alchemist’”, the bot will use Dialogflow to identify the intent and extract the book title from the query. Then, it will send a response back to the user with the book title, like this:

Bot: Hi there! I'm a book search bot. Use the /search command to find books.
User: /search I want to buy the book 'The Alchemist'
Bot: Here is the book you asked for: The Alchemist

Alternatively, if the user sends a query that does not match any of the intents that are defined in Dialogflow, the bot will send a response from the default fallback intent, like this:

Bot: Hi there! I'm a book search bot. Use the /search command to find books.
User: /search Can you recommend some books?
Bot: Sure, here are some books that you might like:
     - The Alchemist
     - The Catcher in the Rye
     - To Kill a Mockingbird
     - 1984
     - Animal Farm

In this example, the user’s query matches the search_books intent in Dialogflow, which returns a list of books as a response. The bot then sends this list of books back to the user as a response.

You can continue to add more functionality to your bot, such as the ability to purchase books directly from the bot, or to search for books by author or genre. You can also train your Dialogflow agent with additional examples and intents to improve its ability to understand and respond to user queries.

Written on December 5, 2022