Create a Telegram Bot to work with hotels!
External Libraries
- pyTelegramBotAPI
- python-dotenv
- requests
pip install -r requirements.txt
Firstly, create an instance of Bot Class, passing your token as an argument
bot = Bot(TOKEN)Then, create a function, that has to take message as argument and wrap it with a decorator bot.message_handler()
You need to pass a content_types argument to a decorator. Pass ['text'] to make bot listen to all messages
@bot.message_handler(content_types=['text'])
def reply(message) -> None:
passAfter than, everything is built with conditional statements, message variable, and bot's methods Generally speaking, if got this message - call that method
@bot.message_handler(content_types=['text'])
def reply(message) -> None:
if message.text == '/your_command':
your_method()Finally, start your bot with this command:
bot.polling(none_stop=True, interval=0)Done! Now the bot will answer your messages!
Important Note:
Due to the release of new API version, some previous features are currently not working. The whole Request System was rebuilt, and most features could be kept, while other cannot be implemented with new API structure (at least for now). Therefore, it was decided to temporally turn off some features, mostly related to the /bestdeal command
In this section, the detailed information about classes, their methods nad functions will be provided
Args:
token (str): Bot token
Attributes:
requests (HotelRequests): Instance of the class, executing requests to hotels API
database (DataBase): Instance of the class that controls and manages the requests history database
info (Optional[Dict[str, Optional[str, int]]]): Dictionary of user's request criteria
Method clearing the criteria of the request
:return: None
Method that sends the list of commands to the user
:param chat_id: Chat id in which the message needs to be sent
:type chat_id: int
:return: None
Method that send the list of hotels to the user
:param chat_id: Chat id in which the message needs to be sent
:type chat_id: int
:param hotels: List of hotels
:type hotels: List[handlers.Hotel]
:return: None
Method greeting the user
:param user: The user, bot has to greet
:return: None
Method starting a branch to find low price hotels
:param chat_id: Chat id in which the message needs to be sent
:type chat_id: int
:return: None
Method starting a branch to find high price hotels
:param chat_id: Chat id in which the message needs to be sent
:type chat_id: int
:return: None
Currently is not being used
Method starting a branch to find best price hotels based on the cost and distance from the center
:param chat_id: Chat id in which the message needs to be sent
:type chat_id: int
:return: None
Method sending the history of requested hotels to the user
:param chat_id: Chat id in which the message needs to be sent
:type chat_id: int
:return: None
Args:
hotel_id (str): Hotel's ID
name (str): Hotel's name
address (str): Hotel's address
price (str): Hotel's price
rating (str): Hotel's rating
images (Optional[List[str]]): Hotel's pictures
distance (str): Hotel's distance from center
Class executing the requests to Hotels API
Args:
__x_rapidapi_key (str): the personal API key
__headers (Dict[str: str]): settings for API requests
Methods that makes a request to the API to get the rating and address from the hotel
:param hotelId: Hotel ID
:type hotelId: str
:return: Tuple[Union[str, int], str]
Currently, some of the arguments are not processed and do not affect the final result (images_num, cost_range, distance_range)
Final method that gets the hotels based on all criteria
:param destination_id: City ID
:type destination_id: str
:param number: Number of hotels
:type number: str
:param sort: Sorting method
Can be one of 3 values: "PRICE", "PRICE_HIGHEST_FIRST", or "DISTANCE_FROM_LANDMARK"
:type sort: str
:param images_num: Number of pictures for each hotel
:type images_num: int
:param cost_range: Tuple that contains the range of possible prices.
1st value - minimal price, 2nd - maximum price
:type cost_range: Optional[Tuple[str]]
:param distance_range: Tuple that contains the range of the possible distance from the center.
1st value - minimal distance, 2nd - maximum distance
:type distance_range: Optional[Tuple[str]]
:return: hotels_list
:rtype: List[Hotel]
Method getting the City ID based on its name.
if the city is not found returns the string 'CITY_NOT_FOUND'
:param city: City name
:type city: str
:return: destination_id
:rtype: str
:return: 'CITY_NOT_FOUND'
:rtype: str
Currently is not being used
Method getting the pictures of hotels
:param hotel_id: Hotel ID
:type hotel_id: str
:param num: Number of pictures for each hotel
:type num: Union[str, int]
:returns: (result, None)
:rtype: Optional[List[str]]
Class that describes the user request
Args:
request_id (int): Request ID
command (str): command the user used to get hotels
date (str): date and time of the request
hotels (List[Hotel]): the list of hotels from this request
Class that controls and manages the database
Args:
filename (str): the filename of database
Attributes:
conn: connection to a database
cursor: connection cursor
Method that starts the database
:return: None
Method that closes the database
:return: None
Methods that clears the database
:return: None
Method that generates all tables in database
:return: None
Method that inserts the hotel to the database
:param hotel: the instance of the hotel class that needs to be inserted
:type hotel: Hotel
:return: None
Method that inserts the user request to the database
:param user_id: User ID who requested hotels
:type user_id: int
:param command: command of the request
:type command: str
:param city: city of the request
:type city: str
:param hotels: the list of hotels produced by the request
:type hotels: List[Hotel]
:return: None
Method that gets the hotels from database based on its ID
:param hotel_id: Hotel ID
:type hotel_id: str
:return: Hotel
Method that gets the request from teh user based on their ID
:param user_id: User ID
:type user_id: int
:return: final
:rtype: List[Request]
Functions used to process messages and redirect the user to another branch of dialog
Function that gets the City ID and redirects to the branch of choosing number of hotels
:param message: User message that contains the city name
:param bot: Instance of Bot class
:return: None
Currently is not being used
Function that gets the price range from the user and redirects to the branch of choosing the range of the possible distance from center
:param message: User message that contains the range of prices
:param bot: Instance of Bot class
:return: None
Currently is not being used
Function that gets the range of possible distance and redirects to the branch of choosing the number of hotels
:param message: User message that contains the range of distances
:param bot: Instance of Bot class
:return: None
Function that gets the number of hotels from user and send the list of hotels to user
:param message: User message that contains the number of hotels
:param bot: Instance of Bot class
:return: None
Currently is not being used
Function that gets the information whether or not the user needs images and redirects tot the branch of choosing the number of images or send hotels to the user
:param message: User message that contains the answer (yes / no)
:param bot: Instance of Bot class
:return: None
Currently is not being used
Function that gets the number of images and send hotels to the user
:param message: User message that contains the number of images
:param bot: Instance of Bot class
:return: None
