Compare commits
3 Commits
0e014fbca2
...
20914fcfd9
Author | SHA1 | Date |
---|---|---|
Derek | 20914fcfd9 | |
Derek | 72be1eb741 | |
Derek | ec10b59a7e |
25
main.py
25
main.py
|
@ -4,6 +4,7 @@ import click
|
||||||
from telebot.apihelper import ApiException
|
from telebot.apihelper import ApiException
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
import traceback
|
||||||
|
|
||||||
from models import db, Chat
|
from models import db, Chat
|
||||||
|
|
||||||
|
@ -27,10 +28,18 @@ def channel_commands(message, commands):
|
||||||
def oops(message, e):
|
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, "Looks like something went wrong, send @skehmatics the following but note it may contain confidential information:")
|
||||||
bot.send_message(message.chat.id, str(e))
|
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):
|
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']
|
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.channel_post_handler(func=lambda msg: channel_commands(msg, ['start']))
|
||||||
@bot.message_handler(commands=['start'])
|
@bot.message_handler(commands=['start'])
|
||||||
|
@ -41,18 +50,19 @@ def start(message):
|
||||||
@bot.message_handler(commands=['register'])
|
@bot.message_handler(commands=['register'])
|
||||||
def register(message):
|
def register(message):
|
||||||
try:
|
try:
|
||||||
old_chat = Chat.query.filter_by(chat_id=message.chat.id, user_id=message.from_user.id).first()
|
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()
|
||||||
if old_chat:
|
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')
|
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
|
return
|
||||||
if not is_admin(message.chat.id, message.from_user.id):
|
if not is_admin(message.chat.id, sender_or_creator_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')
|
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
|
return
|
||||||
chat = Chat(message.chat.id, message.from_user.id)
|
chat = Chat(message.chat.id, sender_or_creator_id)
|
||||||
db.session.add(chat)
|
db.session.add(chat)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
try:
|
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)), 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).replace('_', '\_'),), parse_mode='Markdown')
|
||||||
bot.send_message(message.chat.id, "Badabing, badaboom. Token PM'ed to you!", parse_mode='Markdown')
|
bot.send_message(message.chat.id, "Badabing, badaboom. Token PM'ed to you!", parse_mode='Markdown')
|
||||||
except ApiException as e:
|
except ApiException as e:
|
||||||
if e.result.status_code == 403:
|
if e.result.status_code == 403:
|
||||||
|
@ -66,8 +76,9 @@ def register(message):
|
||||||
@bot.channel_post_handler(func=lambda msg: channel_commands(msg, ['revoke']))
|
@bot.channel_post_handler(func=lambda msg: channel_commands(msg, ['revoke']))
|
||||||
@bot.message_handler(commands=['revoke'])
|
@bot.message_handler(commands=['revoke'])
|
||||||
def revoke(message, chat=None):
|
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:
|
if chat is None:
|
||||||
chat = Chat.query.filter_by(chat_id=message.chat.id, user_id=message.from_user.id).first()
|
chat = Chat.query.filter_by(chat_id=message.chat.id, user_id=sender_or_creator_id).first()
|
||||||
if chat is None:
|
if chat is None:
|
||||||
bot.send_message(message.chat.id, "Can't seem to find any tokens you own for this chat.")
|
bot.send_message(message.chat.id, "Can't seem to find any tokens you own for this chat.")
|
||||||
return
|
return
|
||||||
|
@ -134,7 +145,7 @@ def manage_show_settings(callback):
|
||||||
back_button = telebot.types.InlineKeyboardButton("\u00ab Back", callback_data='managestart:')
|
back_button = telebot.types.InlineKeyboardButton("\u00ab Back", callback_data='managestart:')
|
||||||
markup.add(show_button, revoke_button, back_button)
|
markup.add(show_button, revoke_button, back_button)
|
||||||
|
|
||||||
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.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.callback_query_handler(func=lambda call: call.data.startswith('manageshow:'))
|
@bot.callback_query_handler(func=lambda call: call.data.startswith('manageshow:'))
|
||||||
def manage_show_token(callback):
|
def manage_show_token(callback):
|
||||||
|
@ -147,7 +158,7 @@ def manage_show_token(callback):
|
||||||
back_button = telebot.types.InlineKeyboardButton("\u00ab Back", callback_data='managestart:')
|
back_button = telebot.types.InlineKeyboardButton("\u00ab Back", callback_data='managestart:')
|
||||||
markup.add(show_button, revoke_button, back_button)
|
markup.add(show_button, revoke_button, back_button)
|
||||||
|
|
||||||
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.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.callback_query_handler(func=lambda call: call.data.startswith('managerevoke:'))
|
@bot.callback_query_handler(func=lambda call: call.data.startswith('managerevoke:'))
|
||||||
def manage_revoke_token(callback):
|
def manage_revoke_token(callback):
|
||||||
|
|
Loading…
Reference in New Issue