pack to package
This commit is contained in:
+153
@@ -0,0 +1,153 @@
|
||||
import telebot
|
||||
from telebot import util
|
||||
from flask import Flask, request, abort
|
||||
from datetime import datetime as dt
|
||||
from os.path import exists
|
||||
import csv
|
||||
|
||||
from .query_log import logging
|
||||
from .me import get_me
|
||||
from .config import bot, ADMIN_ID, TOKEN
|
||||
from . import reminder
|
||||
|
||||
# https://core.telegram.org/bots/api Telegram Bot API
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
print(">>> LutzBot is running! <<<")
|
||||
|
||||
|
||||
@bot.message_handler(commands=['rules', 'faq'])
|
||||
def send_lutz_command(message):
|
||||
bot.send_message(message.chat.id,
|
||||
'<b>🟡 <u><a href="https://telegra.ph/pythonchatru-07-07">Правила чата</a></u></b>',
|
||||
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="""<i><b>Learning Python</b>, 5th Edition</i>
|
||||
├ 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(
|
||||
ADMIN_ID, get_me(message), parse_mode='HTML')
|
||||
logging(message)
|
||||
|
||||
|
||||
@bot.message_handler(commands=['log'])
|
||||
def send_log(message):
|
||||
"""Send log from log.csv"""
|
||||
if message.from_user.id == ADMIN_ID:
|
||||
file_path = f"logs/log_{dt.now().strftime('%Y-%m')}.csv"
|
||||
if exists(file_path):
|
||||
with open(file_path, "r", newline='', encoding='utf-8') as f:
|
||||
all_rows = list(csv.reader(f))
|
||||
text = ''
|
||||
for row in all_rows[-min(len(all_rows), 5):]:
|
||||
text += str(row) + '\n'
|
||||
bot.send_message(ADMIN_ID, f'<code>{text}</code>', parse_mode='html')
|
||||
|
||||
|
||||
@bot.inline_handler(lambda query: len(query.query) == 0)
|
||||
def default_query(inline_query):
|
||||
"""Inline Aricles"""
|
||||
lutz_rus = telebot.types.InlineQueryResultCachedDocument(
|
||||
id='44', title='📕 Изучаем Python 🇷🇺',
|
||||
document_file_id='BQACAgIAAxkBAAIBcGLHE2ryQewEP1ddXOd_jF3OOHUfAAISIAACaOk4SissgGKstQbqKQQ',
|
||||
description='Марк Лутц, 5-е издание, Том 1',
|
||||
caption="""<i><b>Изучаем Python</b>, 5-е издание</i>
|
||||
├ Автор: Марк Лутц
|
||||
└ Год: 2019""",
|
||||
parse_mode='HTML',
|
||||
)
|
||||
|
||||
matthes_rus = telebot.types.InlineQueryResultCachedDocument(
|
||||
id='45', title='📕 Изучаем Python 🇷🇺',
|
||||
document_file_id='BQACAgIAAxkBAAIBdmLHF0InnLNlQuKzi1fZOYWOdu7eAAIrIAACaOk4StPj0vlMzwe2KQQ',
|
||||
description='Эрик Мэтиз , 3-е издание',
|
||||
caption="""<i><b>Изучаем Python</b>, 3-е издание</i>
|
||||
├ Автор: Эрик Мэтиз
|
||||
└ Год: 2020""",
|
||||
parse_mode='HTML',
|
||||
)
|
||||
|
||||
lutz = telebot.types.InlineQueryResultDocument(
|
||||
id='1', title='📕 Learning Python ⭐️',
|
||||
document_url='https://fk7.ru/books/OReilly.Learning.Python.5th.Edition.pdf',
|
||||
description='Mark Lutz, 5th Edition',
|
||||
caption="""<i><b>Learning Python</b>, 5th Edition</i>
|
||||
├ 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='Eric Matthes, 2th Edition',
|
||||
caption="""<i><b>Python Crash Course</b>, 2th Edition</i>
|
||||
├ by Eric Matthes
|
||||
└ Released 2019""",
|
||||
parse_mode='HTML',
|
||||
mime_type='application/pdf',
|
||||
thumb_url='https://fk7.ru/books/PythonCrashCourse.jpg', )
|
||||
|
||||
bot.answer_inline_query(
|
||||
inline_query.id, [lutz, matthes, lutz_rus, matthes_rus],
|
||||
cache_time=10)
|
||||
|
||||
|
||||
@bot.message_handler(content_types=['document'])
|
||||
def command_me(message):
|
||||
""" GetMe Informer. """
|
||||
if message.chat.id == ADMIN_ID:
|
||||
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)
|
||||
|
||||
|
||||
@bot.message_handler(commands=['remind'])
|
||||
def remind_manually():
|
||||
"""Remind manually"""
|
||||
reminder.remind()
|
||||
|
||||
|
||||
@bot.message_handler(commands=['jobs'])
|
||||
def list_jobs(message):
|
||||
"""List all jobs"""
|
||||
if message.chat.id == ADMIN_ID:
|
||||
text = reminder.get_jobs()
|
||||
bot.send_message(message.chat.id, text, parse_mode='HTML')
|
||||
|
||||
|
||||
@app.route(f"/bot{TOKEN}/", methods=['POST'])
|
||||
def webhook():
|
||||
""" Set webhook. """
|
||||
bot.set_webhook(f'https://fk7.ru/bot{TOKEN}/', allowed_updates=util.update_types)
|
||||
""" 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)
|
||||
@@ -0,0 +1,20 @@
|
||||
""" Helper functions for bot.py """
|
||||
|
||||
|
||||
def get_me(message):
|
||||
# Fields tip: https://core.telegram.org/bots/api#user
|
||||
msg = f"<b>Telegram ID:</b> <code>{message.from_user.id}</code>\n\
|
||||
├ <b>Is bot:</b> {message.from_user.is_bot}\n\
|
||||
├ <b>First name:</b> {message.from_user.first_name}\n"
|
||||
if message.from_user.last_name:
|
||||
msg += f" ├ <b>Last name:</b> {message.from_user.last_name}\n"
|
||||
if message.from_user.username:
|
||||
msg += f" ├ <b>Username:</b> {message.from_user.username}\n"
|
||||
msg += f" ├ <b>Language code:</b> {message.from_user.language_code}\n"
|
||||
if message.from_user.is_premium is True:
|
||||
msg += f" ├ <b>Is Premium</b>: ⭐️\n"
|
||||
else:
|
||||
msg += f" ├ <b>Is Premium</b>: No\n"
|
||||
msg += f" ├ <b>Chat ID:</b> {message.chat.id}"
|
||||
|
||||
return msg
|
||||
@@ -0,0 +1,19 @@
|
||||
import csv
|
||||
from os.path import exists
|
||||
import datetime as dt
|
||||
|
||||
|
||||
def logging(message):
|
||||
log_file_csv = f'logs/log_{dt.datetime.now().strftime("%Y-%m")}.csv'
|
||||
if not exists(log_file_csv):
|
||||
with open(log_file_csv, 'a') as f:
|
||||
logs = csv.writer(f)
|
||||
logs.writerow(['DATE AND TIME', 'ID', 'FIRST NAME', 'USERNAME',
|
||||
'CHAT', 'LANG', 'PREMIUM', 'QUERY'])
|
||||
|
||||
with open(log_file_csv, 'a') as f:
|
||||
logs = csv.writer(f)
|
||||
logs.writerow(
|
||||
[dt.datetime.now().strftime('%Y-%m-%d %H:%H'), message.from_user.id,
|
||||
message.from_user.first_name, message.from_user.username,
|
||||
message.chat.id, message.from_user.language_code, message.from_user.is_premium, message.text])
|
||||
@@ -0,0 +1,44 @@
|
||||
import schedule
|
||||
import time
|
||||
from threading import Thread
|
||||
from datetime import datetime as dt
|
||||
import csv
|
||||
|
||||
from .config import PYTHONCHATRU, bot
|
||||
|
||||
|
||||
# https://core.telegram.org/bots/api Telegram Bot API
|
||||
|
||||
|
||||
def scheduler():
|
||||
while True:
|
||||
schedule.run_pending()
|
||||
time.sleep(20)
|
||||
|
||||
|
||||
def remind():
|
||||
""" Remind holiday. """
|
||||
with open('holidays.csv', newline='', encoding='utf-8') as holidays_file:
|
||||
holidays = tuple(csv.reader(holidays_file))
|
||||
today = dt.today()
|
||||
for entry in holidays[1:]:
|
||||
date, holiday, description = entry
|
||||
if today.month == dt.strptime(date, "%Y-%m-%d").month and today.day == dt.strptime(date, "%Y-%m-%d").day:
|
||||
notification = f'🎉💻 Сегодня {dt.strptime(date, "%Y-%m-%d"):%d.%m}, <b><u>{holiday.upper()}</u></b>!\
|
||||
\n\n{description}.'
|
||||
if dt.strptime(date, "%Y-%m-%d").year != 1:
|
||||
age = today.year - dt.strptime(date, "%Y-%m-%d").year
|
||||
notification += f'\n<i>{age} годовщина</i>'
|
||||
bot.send_message(PYTHONCHATRU, notification, parse_mode='HTML') # PYTHONCHATRU
|
||||
|
||||
|
||||
def get_jobs():
|
||||
all_jobs = schedule.get_jobs()
|
||||
text = f"├ <b>Jobs:</b> {str(len(all_jobs))}\
|
||||
\n├ {dt.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
|
||||
text += '<i>' + str(all_jobs) + '</i>'
|
||||
return text
|
||||
|
||||
|
||||
schedule.every().day.at('06:00').do(remind)
|
||||
Thread(target=scheduler).start()
|
||||
Reference in New Issue
Block a user