GuessThePrice/source/bot.py

79 lines
2.0 KiB
Python
Raw Normal View History

2022-05-02 15:04:54 +00:00
"""
script for telegram bot and its functions
"""
__author__ = "Florian Kellermann, Linus Eickhoff, Florian Kaiser"
__date__ = "02.05.2022"
2022-05-02 15:22:50 +00:00
__version__ = "0.0.1"
2022-05-02 15:04:54 +00:00
__license__ = "None"
# main bot at http://t.me/guess_the_price_bot
# debug bot at http://t.me/amazondebug_bot
2022-05-02 15:07:28 +00:00
import logging
2022-05-02 15:04:54 +00:00
import os
import sys
2022-05-02 15:22:50 +00:00
import telebot
from dotenv import load_dotenv
from telebot import types
2022-05-02 15:58:56 +00:00
# from apscheduler.schedulers.background import BackgroundScheduler
2022-05-02 15:22:50 +00:00
2022-05-02 16:03:13 +00:00
# from db import User, session
2022-05-02 15:22:50 +00:00
load_dotenv(dotenv_path='.env') # load environment variables
2022-05-02 15:04:54 +00:00
2022-05-02 15:46:04 +00:00
BOT_VERSION = "0.0.1" # version of bot
2022-05-02 15:04:54 +00:00
bot = telebot.TeleBot(os.getenv('BOT_API_KEY'))
2022-05-02 15:22:50 +00:00
@bot.message_handler(commands=['start', 'Start'])
2022-05-02 15:04:54 +00:00
def send_start(message):
""" Sending welcome message to new user
:type message: message object bot
:param message: message that was reacted to, in this case always containing '/start'
:raises: none
:rtype: none
"""
2022-05-02 15:07:28 +00:00
bot.reply_to(message, "Welcome to this amazon prices guesser bot")
2022-05-02 15:22:50 +00:00
2022-05-02 15:07:28 +00:00
telebot.logger.setLevel(logging.DEBUG)
2022-05-02 15:22:50 +00:00
@bot.inline_handler(lambda query: query.query == 'text') # inline prints for debugging
2022-05-02 15:04:54 +00:00
def query_text(inline_query):
""" Output in the console about current user actions and status of bot
2022-05-02 16:03:13 +00:00
:type inline_query:
2022-05-02 15:04:54 +00:00
:param inline_query:
:raises: none
:rtype: none
"""
try:
2022-05-02 16:03:13 +00:00
r = types.InlineQueryResultArticle('1', 'Result1', types.InputTextMessageContent('hi')) # pylint: disable=invalid-name
r2 = types.InlineQueryResultArticle('2', 'Result2', types.InputTextMessageContent('hi')) # pylint: disable=invalid-name
2022-05-02 15:04:54 +00:00
bot.answer_inline_query(inline_query.id, [r, r2])
2022-05-02 16:03:13 +00:00
except Exception as e: # pylint: disable=broad-except, invalid-name
2022-05-02 15:04:54 +00:00
print(e)
2022-05-02 15:22:50 +00:00
2022-05-02 15:04:54 +00:00
def main_loop():
""" Start bot
:raises: none
:rtype: none
"""
bot.infinity_polling()
2022-05-02 15:22:50 +00:00
2022-05-02 15:04:54 +00:00
if __name__ == '__main__':
try:
main_loop()
except KeyboardInterrupt:
print('\nExiting by user request.\n')
2022-05-02 15:22:50 +00:00
sys.exit(0)