Compare commits

..

No commits in common. "20914fcfd963be72883bab65273912b445c051ff" and "0e014fbca2486cefa4a94874b2cf010fd675ef11" have entirely different histories.

1 changed files with 7 additions and 18 deletions

25
main.py
View File

@ -4,7 +4,6 @@ import click
from telebot.apihelper import ApiException
import json
import time
import traceback
from models import db, Chat
@ -28,18 +27,10 @@ def channel_commands(message, commands):
def oops(message, e):
bot.send_message(message.chat.id, "Looks like something went wrong, send @skehmatics the following but note it may contain confidential information:")
bot.send_message(message.chat.id, str(e))
user = message.from_user.username if message.from_user is not None else get_chat_creator(message.chat.id).username
app.logger.error("encountered by {user} - {e}".format(user=user, e=str(e)))
app.logger.error(traceback.format_exc())
def is_admin(chat, user):
return bot.get_chat(chat).type not in ['group', 'supergroup'] or bot.get_chat_member(chat, user).status in ['creator', 'administrator']
def get_chat_creator(chat_id):
privileged_members = bot.get_chat_administrators(chat_id)
creator = next(member.user for member in privileged_members if member.status == 'creator')
return creator
@bot.channel_post_handler(func=lambda msg: channel_commands(msg, ['start']))
@bot.message_handler(commands=['start'])
@ -50,19 +41,18 @@ def start(message):
@bot.message_handler(commands=['register'])
def register(message):
try:
sender_or_creator_id = message.from_user.id if message.from_user is not None else get_chat_creator(message.chat.id).id
old_chat = Chat.query.filter_by(chat_id=message.chat.id, user_id=sender_or_creator_id).first()
old_chat = Chat.query.filter_by(chat_id=message.chat.id, user_id=message.from_user.id).first()
if old_chat:
bot.send_message(message.chat.id, "It looks like you already have this bot activated. If you want to revoke the token, use /revoke instead.", parse_mode='Markdown')
return
if not is_admin(message.chat.id, sender_or_creator_id):
if not is_admin(message.chat.id, message.from_user.id):
bot.send_message(message.chat.id, "It looks like you're not an admin here. Go bug one of them instead.", parse_mode='Markdown')
return
chat = Chat(message.chat.id, sender_or_creator_id)
chat = Chat(message.chat.id, message.from_user.id)
db.session.add(chat)
db.session.commit()
try:
bot.send_message(chat.user_id, "Here's your shiny new token for {name}: `{token}`".format(token=chat.token, name=chat.get_friendly_name(bot).replace('_', '\_'),), parse_mode='Markdown')
bot.send_message(chat.user_id, "Here's your shiny new token for {name}: `{token}`".format(token=chat.token, name=chat.get_friendly_name(bot)), parse_mode='Markdown')
bot.send_message(message.chat.id, "Badabing, badaboom. Token PM'ed to you!", parse_mode='Markdown')
except ApiException as e:
if e.result.status_code == 403:
@ -76,9 +66,8 @@ def register(message):
@bot.channel_post_handler(func=lambda msg: channel_commands(msg, ['revoke']))
@bot.message_handler(commands=['revoke'])
def revoke(message, chat=None):
sender_or_creator_id = message.from_user.id if message.from_user is not None else get_chat_creator(message.chat.id).id
if chat is None:
chat = Chat.query.filter_by(chat_id=message.chat.id, user_id=sender_or_creator_id).first()
chat = Chat.query.filter_by(chat_id=message.chat.id, user_id=message.from_user.id).first()
if chat is None:
bot.send_message(message.chat.id, "Can't seem to find any tokens you own for this chat.")
return
@ -145,7 +134,7 @@ def manage_show_settings(callback):
back_button = telebot.types.InlineKeyboardButton("\u00ab Back", callback_data='managestart:')
markup.add(show_button, revoke_button, back_button)
bot.edit_message_text("Chat \"{name}\"\n token: `<hidden>`".format(name=chat.get_friendly_name(bot).replace('_', '\_')), reply_markup=markup, parse_mode='Markdown', chat_id=callback.message.chat.id, message_id=callback.message.message_id)
bot.edit_message_text("Chat \"{name}\"\n token: `<hidden>`".format(name=chat.get_friendly_name(bot)), reply_markup=markup, parse_mode='Markdown', chat_id=callback.message.chat.id, message_id=callback.message.message_id)
@bot.callback_query_handler(func=lambda call: call.data.startswith('manageshow:'))
def manage_show_token(callback):
@ -158,7 +147,7 @@ def manage_show_token(callback):
back_button = telebot.types.InlineKeyboardButton("\u00ab Back", callback_data='managestart:')
markup.add(show_button, revoke_button, back_button)
bot.edit_message_text("Chat \"{name}\"\n token: `{token}`".format(name=chat.get_friendly_name(bot).replace('_', '\_'),, token=chat.token), reply_markup=markup, parse_mode='Markdown', chat_id=callback.message.chat.id, message_id=callback.message.message_id)
bot.edit_message_text("Chat \"{name}\"\n token: `{token}`".format(name=chat.get_friendly_name(bot), token=chat.token), reply_markup=markup, parse_mode='Markdown', chat_id=callback.message.chat.id, message_id=callback.message.message_id)
@bot.callback_query_handler(func=lambda call: call.data.startswith('managerevoke:'))
def manage_revoke_token(callback):