Hi, you can start by creating a bot on telegram, search for @botfather first. Create your bot and copy the key.
Then open a python file in vscode and start implementing. You can install dependencies like
“pip install pytelegrambotapi” , “pip install openai” and get started.
import telebot
import openai
import re
from os import getenv
import dotenv
Configuring credentials and API key
dotenv.load_dotenv()
API_KEY = getenv(‘API_KEY’)
OPENAI_API_KEY = getenv(‘API_OPENAI’)
Telegram bot initialization
bot = telebot.TeleBot(API_KEY)
OpenAI API initialization
openai.api_key = OPENAI_API_KEY
Storage of message state for each user
user_messages = {}
appointment_date = {}
OpenAI response function
def get_openai_response(messages):
response = openai.ChatCompletion.create(
model=“gpt-3.5-turbo”,
messages=messages
)
return response.choices[0].message[‘content’]
/start command handler
@bot.message_handler(commands=[‘start’])
def send_welcome(message):
chat_id = message.chat.id
user_first_name = message.from_user.first_name
bot.send_message(chat_id, f"Hello! {user_first_name}, how are you?
Welcome to office X")
Start the conversation with system messages
user_messages[chat_id] = [{‘role’: ‘system’, ‘content’: “”"
First, greet the user and inform them that you are doctor Marcus Júnior’s secretary, and that you are available to schedule your appointment.
You are Jana, a virtual assistant and secretary to Doctor Marcus Junior. You are here to help schedule doctor appointments.
You are not available to answer general questions, but only help schedule appointments at Doctor Marcus Junior’s medical office.
Ask for the client’s full name and reason for the consultation.
Ask the client for a preferred date and time to see if it is available on the doctor’s schedule.
Check the doctor’s availability and suggest three possible times if the requested time is not available.
After confirming the date and time of the appointment, ask if the appointment will be private or through a health plan.
After the customer provides plan details or confirms they will pay out of pocket,
assure the client that the appointment was successfully scheduled and provide a summary of the appointment details.
Consultation details must be sent to the patient at the end of the appointment.
Be polite, professional and attentive throughout the interaction.
Emphasize that you are available to help with any questions or rescheduling needs.
Expect new interactions, always ready to serve the customer in an efficient and friendly manner.
“”"}]
appointment_data[chat_id] = {}
Text message handler
@bot.message_handler(func=lambda message: True)
def handle_message(message):
chat_id = message.chat.id
user_message = message.text
Retrieve user’s message history
if chat_id not in user_messages:
user_messages[chat_id] = []
user_messages[chat_id].append({‘role’: ‘user’, ‘content’: user_message})
Call the OpenAI API to get the response
assistant_message = get_openai_response(user_messages[chat_id])
user_messages[chat_id].append({‘role’: ‘assistant’, ‘content’: assistant_message})
bot.send_message(chat_id, assistant_message)
Start the bot
bot.polling()