Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #28 from andrew4ever/dev
Browse files Browse the repository at this point in the history
Patches
  • Loading branch information
andrewyazura authored Mar 20, 2020
2 parents eda9d36 + dba13fd commit 5689839
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 57 deletions.
7 changes: 7 additions & 0 deletions .dependabot/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 1
update_configs:
- package_manager: "python"
directory: "/"
update_schedule: "daily"
default_labels:
- "dependencies"
6 changes: 3 additions & 3 deletions config_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
'BOT': {
'TOKEN': 'your_token',
'START_MESSAGE': 'Hello world',
'ASK_MESSAGE': 'How are you?',
'ASK_MESSAGE': 'Do you want to order?',

'ORDER_TRUE': 'Orderd!',
'ORDER_FALSE': 'Not ordered',

'ACCEPTED': 'Great!',
'ADDED': 'Admin added you!',

'SUCCESS': 'Everything is fine!',
Expand All @@ -36,6 +35,7 @@
'NO_USER': 'No such user',

'TROUBLESHOOTING': 'Developer will be notified!',
'NOT_REGISTERED': 'You\'re not registered! Use /start first and wait until admins submit you',
'GARBAGE_RESPONSE': 'Stop sending this!',

'KEYBOARDS': {
Expand All @@ -49,7 +49,7 @@
/report <message> - report bug in bot
''',
'ADMIN_COMMANDS': '''Commands:
'ADMIN_COMMANDS': r'''Commands:
/users - all users
Expand Down
81 changes: 53 additions & 28 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import tinydb
from telebot import types

from services import *
import services
from config import config

if not os.path.exists(config['DB_PATH']):
Expand All @@ -28,36 +28,41 @@
level=logging.DEBUG)

get_data_process = multiprocessing.Process(
target=execute_at, args=(config['ASK_TIME'], get_food_orders, True, (bot, db, config, True)))
target=services.execute_at, args=(config['ASK_TIME'], services.get_food_orders, True, (bot, db, config, True)))
send_data_process = multiprocessing.Process(
target=execute_at, args=(config['SEND_TIME'], send_food_orders, True, (bot, db, config)))
target=services.execute_at, args=(config['SEND_TIME'], services.send_food_orders, True, (bot, db, config)))


@bot.message_handler(commands=['start'])
def start_menu(message: types.Message):
bot.reply_to(message, config['BOT']['START_MESSAGE'])
u = message.chat
user_str = generate_user_str(u)
user_str = services.generate_user_str(u)

logging.info('/start from %s:%s', u.id, user_str)

if not is_user(u.id, db) and not is_admin(u.id, config):
if not services.is_user(u.id, db):
add_user_keyboard = types.InlineKeyboardMarkup(row_width=1)
add_user_keyboard.add(
types.InlineKeyboardButton(
text=config['BOT']['KEYBOARDS']['ADD_TO_DB'],
callback_data=f'{u.id}:{user_str}'
))

send_to_admins(bot, config,
config['BOT']['NEW_USER'] +
f' {u.id}, {u.username}, {user_str}',
kwargs={'reply_markup': add_user_keyboard})
services.send_to_admins(bot, config,
config['BOT']['NEW_USER'] +
f' {u.id}, {u.username}, {user_str}',
kwargs={'reply_markup': add_user_keyboard})


@bot.message_handler(commands=['help'])
def help_menu(message: types.Message):
u = message.chat

if not services.is_user(u.id, db):
bot.reply_to(message, config['BOT']['NOT_REGISTERED'])
return

bot.reply_to(message, config['BOT']['HELP_MESSAGE'])
logging.info('/help from %s:%s', u.id, u.first_name)

Expand All @@ -66,36 +71,50 @@ def help_menu(message: types.Message):
def reorder(message: types.Message):
u = message.chat

get_order(bot, config, {
if not services.is_user(u.id, db):
bot.reply_to(message, config['BOT']['NOT_REGISTERED'])
return

services.get_order(bot, config, {
'telegram_id': u.id,
'name': generate_user_str(u)
'name': services.generate_user_str(u)
})

logging.info('/change_order or /ask_me from %s:%s', u.id, u.first_name)


@bot.message_handler(commands=['report', 'troubleshoot'])
def troubleshoot(message: types.Message):
command_args = extract_args(message.text, (' ', 1))
command_args = services.extract_args(message.text, (' ', 1))
chat = message.chat

if not services.is_user(chat.id, db):
bot.reply_to(message, config['BOT']['NOT_REGISTERED'])
return

if len(command_args) != 1:
bot.send_message(chat.id, config['BOT']['INVALID_SYNTAX'])
return

bot.send_message(chat.id, config['BOT']['TROUBLESHOOTING'])

send_to_developers(bot, config, command_args[0] +
'\n{0}:{1}'.format(chat.id, chat.username))
services.send_to_developers(bot, config, command_args[0] +
'\n{0}:{1}'.format(chat.id, chat.username))

logging.info('/report or /troubleshoot from %s:%s', message.chat.id,
message.chat.first_name)


@bot.message_handler(commands=['commands'])
def admin_menu(message: types.Message):
u_id = message.chat.id

if not services.is_admin(u_id, config):
bot.send_message(u_id, config['BOT']['NO_PERMISSION'])
return

bot.send_message(
message.chat.id, config['BOT']['ADMIN_COMMANDS'], parse_mode='markdown')
u_id, config['BOT']['ADMIN_COMMANDS'], parse_mode='markdown')

logging.info('/commands from %s:%s', message.chat.id,
message.chat.first_name)
Expand All @@ -105,7 +124,7 @@ def admin_menu(message: types.Message):
def send_logs(message: types.Message):
u_id = message.chat.id

if not is_admin(u_id, config):
if not services.is_admin(u_id, config):
bot.send_message(u_id, config['BOT']['NO_PERMISSION'])
return

Expand All @@ -121,18 +140,18 @@ def send_logs(message: types.Message):
@bot.message_handler(commands=['ask_now'])
def request_orders(message: types.Message):
u_id = message.chat.id
args = extract_args(message.text)
args = services.extract_args(message.text)
clear = args[0] if args else '0'

if not is_admin(u_id, config):
if not services.is_admin(u_id, config):
bot.send_message(u_id, config['BOT']['NO_PERMISSION'])
return

if not clear.isdigit():
bot.send_message(u_id, config['BOT']['INVALID_SYNTAX'])
return

get_food_orders(bot, db, config, int(clear))
services.get_food_orders(bot, db, config, int(clear))
bot.send_message(u_id, config['BOT']['SUCCESS'])

logging.info('/ask_now from %s:%s', message.chat.id,
Expand All @@ -143,22 +162,28 @@ def request_orders(message: types.Message):
def send_orders(message: types.Message):
u_id = message.chat.id

if not is_admin(u_id, config):
if not services.is_admin(u_id, config):
bot.send_message(u_id, config['BOT']['NO_PERMISSION'])
return

bot.send_message(u_id, config['BOT']['USERS_LIST_TITLE'] +
generate_users_str(db, config, with_orders=True))
services.generate_users_str(db, user_query, config, with_orders=True))

logging.info('/orders from %s:%s', message.chat.id,
message.chat.first_name)


@bot.message_handler(commands=['clear_orders'])
def clear(message: types.Message):
clear_orders(db)
u_id = message.chat.id

bot.send_message(message.chat.id, config['BOT']['SUCCESS'])
if not services.is_admin(u_id, config):
bot.send_message(u_id, config['BOT']['NO_PERMISSION'])
return

services.clear_orders(db)

bot.send_message(u_id, config['BOT']['SUCCESS'])

logging.info('/clear_orders from %s:%s', message.chat.id,
message.chat.first_name)
Expand All @@ -168,31 +193,31 @@ def clear(message: types.Message):
def manage_users(message: types.Message):
u_id = message.chat.id

if not is_admin(u_id, config):
if not services.is_admin(u_id, config):
bot.send_message(u_id, config['BOT']['NO_PERMISSION'])
return

bot.send_message(u_id, config['BOT']['USERS_LIST_TITLE'] +
generate_users_str(db, config, with_ids=True))
services.generate_users_str(db, user_query, config, with_ids=True))

logging.info('/users from %s:%s', message.chat.id,
message.chat.first_name)


@bot.message_handler(commands=['del_user'])
def delete_user(message: types.Message):
command_args = extract_args(message.text)
command_args = services.extract_args(message.text)
u_id = message.chat.id

if not is_admin(u_id, config):
if not services.is_admin(u_id, config):
bot.send_message(u_id, config['BOT']['NO_PERMISSION'])
return

if len(command_args) != 1:
bot.send_message(u_id, config['BOT']['INVALID_SYNTAX'])
return

if is_user(command_args[0], db):
if services.is_user(command_args[0], db):
db.remove(tinydb.where('telegram_id') == command_args[0])
bot.send_message(u_id, config['BOT']['SUCCESS'])

Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
tinydb
pytelegrambotapi
tinydb==3.15.2
pyTelegramBotAPI==3.6.7
1 change: 0 additions & 1 deletion services/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from services.get_food_orders import get_food_orders
from services.get_order import get_order
from services.is_admin import is_admin
from services.is_business_day import is_business_day
from services.is_user import is_user
from services.send_food_orders import send_food_orders
from services.send_to_admins import send_to_admins
Expand Down
22 changes: 12 additions & 10 deletions services/execute_at.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import datetime
from datetime import datetime, timedelta
import logging
import time

from services.is_business_day import is_business_day

from pprint import pprint
def is_business_day(date):
return date.weekday() < 5


def execute_at(wake_time: datetime.time, callback, only_business, args=(), kwargs={}):
def execute_at(wake_time: datetime.time, callback, only_business: bool,
args=(), kwargs={}, interval=timedelta(days=1)):
wake_time = datetime.combine(datetime.now(), wake_time)

while True:
time.sleep(0.9)
now = datetime.datetime.now()
time.sleep(1)
now = datetime.now()

if only_business and not is_business_day(now):
continue

if now.hour == wake_time.hour \
and now.minute == wake_time.minute \
and now.second == wake_time.second:
time.sleep(1) # without delay it triggers many times a second
if now >= wake_time:
callback(*args, **kwargs)
wake_time += interval

logging.info('Executing %s at %r', callback.__name__, wake_time)
27 changes: 16 additions & 11 deletions services/generate_users_str.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from collections import Counter
def generate_users_str(db, config, with_orders=False, with_ids=False):
base = ''

for user in db:
base += user['telegram_id'] + ' - ' if with_ids else ''
base += user['name']
base += ' - ' + str(user.get('order_food',
config['DEFAULT_ORDER'])) if with_orders else ''
base += '\n'

def generate_users_str(db, config, with_orders=False, with_ids=False):
p_list = '\n'.join([((user['telegram_id'] + ' - ') if with_ids else '')
+ user['name'] +
((' - ' + str(user.get('order_food',
config['DEFAULT_ORDER']))) if with_orders else '')
for user in db])
if not len(db):
return config['BOT']['EMPTY']

total_orders = base.count('True')

total_orders = Counter(p_list.split())['True']
base += '\n' + config['BOT']['TOTAL_USERS'] + str(len(db))
base += '\n' + config['BOT']['TOTAL_ORDERS'] + \
str(total_orders) if with_orders else ''

return (p_list + '\n\n' + config['BOT']['TOTAL_USERS'] + str(len(db)) +
(('\n' + config['BOT']['TOTAL_ORDERS'] + str(total_orders)) if with_orders else '')) \
if len(p_list) else config['BOT']['EMPTY']
return base
2 changes: 0 additions & 2 deletions services/is_business_day.py

This file was deleted.

0 comments on commit 5689839

Please sign in to comment.