commit b359487abf63639eefdede68b18853bcbc77ae8b Author: Rykov7 Date: Thu Jul 7 02:08:04 2022 +0300 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1bee03e --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/books/ +/logs/ +/testbot.py +/.venv/ +/.idea/ diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..0c925f3 --- /dev/null +++ b/bot.py @@ -0,0 +1,128 @@ +import telebot +from me import get_me +from flask import Flask, request, abort +from query_log import logging + +# https://core.telegram.org/bots/api Telegram Bot API + + +LUTZPYBOT = "5598132169:AAFBpUn4Us8m7StkY4yHUIcEnnJg3adPvsQ" + + +app = Flask(__name__) +bot = telebot.TeleBot(LUTZPYBOT) + +print("LutzPyBot is working!") + + +@bot.message_handler(commands=['rules']) +def send_lutz_command(message): + bot.send_message(message.chat.id, + '⁉️ Правила чата', + parse_mode='HTML', + disable_notification=True, + ) + logging(message) + + +@bot.message_handler(commands=['lutz']) +def send_lutz_command(message): + bot.send_document( + message.chat.id, + document='BQACAgQAAxkBAAPBYsWJG9Ml0fPrnbU9UyzTQiQSuHkAAjkDAAIstCxSkuRbXAlcqeQpBA', + caption="""Learning Python, 5th Edition + ├ by Mark Lutz + └ Released June 2013""", + parse_mode='HTML') + logging(message) + + +@bot.message_handler(commands=['start']) +def send_start_notify_admin(message): + bot.send_message( + 280887861, get_me(message), parse_mode='HTML') + logging(message) + + +@bot.message_handler(commands=['log']) +def send_log_file(message): + """Get log.csv""" + if message.from_user.id == 280887861: + logfile = open("logs/logs.csv", "r") + bot.send_document(message.chat.id, logfile) + logfile.close() + + +@bot.inline_handler(lambda query: len(query.query) == 0) +def default_query(inline_query): + + """Inline Aricles""" + # lutz = telebot.types.InlineQueryResultCachedDocument( + # id='1', title='M. Lutz "Learning Python"', + # document_file_id='BQACAgIAAxkBAAMdYsS6lL00w2d7pTERHuDHHUT8qF0AAq0ZAALazClKDVkklqaS0FQpBA', + # description='5th Edition (2013), 14.7 MB', + # caption='M. Lutz "Learning Python"', + # ) + + lutz = telebot.types.InlineQueryResultDocument( + id='1', title='⭐️ Learning Python', + document_url='https://fk7.ru/books/OReilly.Learning.Python.5th.Edition.pdf', + description='by Mark Lutz, 5th Edition', + caption="""Learning Python, 5th Edition + ├ by Mark Lutz + └ Released June 2013""", + parse_mode='HTML', + mime_type='application/pdf', + thumb_url='https://fk7.ru/books/OReilly.Learning.Python.5th.Edition.jpg',) + + matthes = telebot.types.InlineQueryResultDocument( + id='2', title='📕 Python Crash Course', + document_url='https://fk7.ru/books/PythonCrashCourse.pdf', + description='by Eric Matthes, 2th Edition', + caption="""Python Crash Course, 2th Edition + ├ by Eric Matthes + └ Released 2019""", + parse_mode='HTML', + mime_type='application/pdf', + thumb_url='https://fk7.ru/books/PythonCrashCourse.jpg',) + + vincent = telebot.types.InlineQueryResultDocument( + id='3', title='📕 Django for Beginners', + document_url='https://fk7.ru/books/django_for_beginners_4_0.pdf', + description='by William S. Vincent, 4.0', + caption="""Django for Beginners, 4.0 + ├ by William S. Vincent + └ Released March 2022""", + parse_mode='HTML', + mime_type='application/pdf', + thumb_url='https://fk7.ru/books/django_for_beginners_4_0.jpg',) + + bot.answer_inline_query( + inline_query.id, [lutz, vincent, matthes], + cache_time=10) + + +@bot.message_handler(content_types=['document']) +def command_me(message): + """GetMe Informer""" + if message.chat.id == 280887861: + bot.send_message(message.chat.id, message.document) + + +@bot.message_handler(commands=['me']) +def command_me(message): + """GetMe Informer""" + bot.send_message(message.chat.id, get_me(message), parse_mode='HTML') + logging(message) + + +@app.route(f'/bot{LUTZPYBOT}/', methods=['POST']) +def webhook(): + """Parse POST requests from Telegram""" + if request.headers.get('content-type') == 'application/json': + json_string = request.get_data().decode('utf-8') + update = telebot.types.Update.de_json(json_string) + bot.process_new_updates([update]) + return '' + else: + abort(403) diff --git a/me.py b/me.py new file mode 100644 index 0000000..230a7ff --- /dev/null +++ b/me.py @@ -0,0 +1,20 @@ +""" Helper functions for bot.py """ + + +def get_me(message): + # Fields: https://core.telegram.org/bots/api#user + msg = f"Telegram ID: {message.from_user.id}\n\ + ├ Is bot: {message.from_user.is_bot}\n\ + ├ First name: {message.from_user.first_name}\n" + if message.from_user.last_name: + msg += f" ├ Last name: {message.from_user.last_name}\n" + if message.from_user.username: + msg += f" ├ Username: {message.from_user.username}\n" + msg += f" ├ Language code: {message.from_user.language_code}\n" + if message.from_user.is_premium is True: + msg += f" ├ Is Premium: ⭐️\n" + else: + msg += f" ├ Is Premium: No\n" + msg += f" ├ Chat ID: {message.chat.id}" + + return msg diff --git a/query_log.py b/query_log.py new file mode 100644 index 0000000..8950bbf --- /dev/null +++ b/query_log.py @@ -0,0 +1,14 @@ +import csv +import datetime as dt + + +def logging(message): + date_and_time = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + log_file_csv = 'logs/logs.csv' + with open(log_file_csv, 'a') as f: + logs = csv.writer(f) + logs.writerow( + [date_and_time, message.from_user.id, + message.from_user.first_name, message.from_user.username, + message.chat.id, message.from_user.language_code, message.text]) + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a2a41c7 Binary files /dev/null and b/requirements.txt differ diff --git a/wsgi.py b/wsgi.py new file mode 100644 index 0000000..880c019 --- /dev/null +++ b/wsgi.py @@ -0,0 +1,4 @@ +from bot import app + +if __name__ == '__main__': + app.run(debug=True)