Compare commits

...

3 Commits

Author SHA1 Message Date
Derek 20914fcfd9
Fix underscore in chat name causes api error bug
When we do parse_markdown, telegram api will punt us out
if we give it bad markdown. Literal underscores have to be
escaped as they are special characters in markdown
2018-06-28 17:11:35 -07:00
Derek 72be1eb741
Fix channel behavior
from_user is not avalible in channels,
so instead we lookup the creator of the channel
and send to them instead.

This should be configurable in the future
2018-06-28 17:10:32 -07:00
Derek ec10b59a7e
Better logging
Log traceback even when exception is handled
2018-06-28 17:08:35 -07:00
1 changed files with 18 additions and 7 deletions

25
main.py
View File

@ -4,6 +4,7 @@ import click
from telebot.apihelper import ApiException
import json
import time
import traceback
from models import db, Chat
@ -27,10 +28,18 @@ 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'])
@ -41,18 +50,19 @@ def start(message):
@bot.message_handler(commands=['register'])
def register(message):
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:
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, 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')
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.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)), 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')
except ApiException as e:
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.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=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:
bot.send_message(message.chat.id, "Can't seem to find any tokens you own for this chat.")
return
@ -134,7 +145,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)), 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:'))
def manage_show_token(callback):
@ -147,7 +158,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), 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:'))
def manage_revoke_token(callback):