lono/main.py

104 lines
4.1 KiB
Python
Raw Normal View History

2018-11-20 19:51:15 +00:00
from html import escape
from random import random
from time import sleep
from telegram.error import Unauthorized, TelegramError
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram import Message, Update
2018-11-20 20:06:07 +00:00
import config
2018-11-20 19:51:15 +00:00
from models import Subscriber
queue = []
def go_away(bot, update: Update):
2018-11-20 20:06:07 +00:00
# Subscriber(user_id=str(update.message.chat_id))
# update.message.reply_text('Вы были добавлены')
update.message.reply_text('Пожалуйста, обратитесь к @lono_contactbot')
2018-11-20 19:51:15 +00:00
def unsubscribe(bot, update: Update):
Subscriber.deleteBy(user_id=str(update.message.chat_id))
update.message.reply_text('Вы были отписаны от бота. Обратитесь к @lono_contactbot за добавлением обратно.')
def msg(bot, update: Update):
queue.append(update.message)
def task_queue(bot, job):
if not queue:
return
m = queue.pop(0)
current_chat = str(m.chat_id)
uids = set(s.user_id for s in Subscriber.select())
if current_chat not in uids:
2018-11-20 20:06:07 +00:00
return m.reply_text('Пожалуйста, обратитесь к @lono_contactbot')
# Subscriber(user_id=current_chat)
# m.reply_text('Вы были добавлены')
2018-11-20 19:51:15 +00:00
try:
uids.remove(current_chat)
except KeyError:
pass
for uid in uids:
sleep(.05)
try:
if m.forward_from or m.forward_from_chat or m.forward_from_message_id or m.forward_signature:
m.forward(f'{uid}')
elif hasattr(m, 'audio') and m.audio:
a = m.audio
bot.send_audio(f'{uid}', a.file_id, a.duration, a.performer, a.title, m.caption_html, parse_mode='html')
elif hasattr(m, 'document') and m.document:
d = m.document
bot.send_document(f'{uid}', d.file_id, d.file_name, m.caption_html, parse_mode='html')
elif hasattr(m, 'photo') and m.photo:
p = m.photo
bot.send_photo(f'{uid}', p[-1].file_id, m.caption_html, parse_mode='html')
elif hasattr(m, 'sticker') and m.sticker:
s = m.sticker
bot.send_sticker(f'{uid}', s.file_id)
elif hasattr(m, 'video') and m.video:
v = m.video
bot.send_video(f'{uid}', v.file_id, v.duration, m.caption_html, parse_mode='html')
elif hasattr(m, 'voice') and m.voice:
v = m.voice
bot.send_voice(f'{uid}', v.file_id, v.duration, m.caption_html, parse_mode='html')
elif hasattr(m, 'video_note') and m.video_note:
vn = m.video_note
bot.send_video_note(f'{uid}', vn.file_id, vn.duration, vn.length)
elif hasattr(m, 'contact') and m.contact:
c = m.contact
bot.send_contact(f'{uid}', c.phone_number, c.first_name, c.last_name)
elif hasattr(m, 'location') and m.location:
l = m.location
bot.send_location(f'{uid}', l.latitude, l.longitude)
elif hasattr(m, 'venue') and m.venue:
v = m.venue
bot.send_venue(f'{uid}', v.location.latitude, v.location.longitude, v.title, v.address, v.foursquare_id)
elif hasattr(m, 'text') and m.text:
txt = m.text_html
if txt.startswith('!sign') or txt.startswith('/sign'):
txt = txt[5:] + f'\n\n____________\n' \
f'by <a href="tg://user?id={m.from_user.id}">{escape(m.from_user.full_name)}</a>'
bot.send_message(f'{uid}', txt, 'html')
except Unauthorized:
Subscriber.deleteBy(user_id=uid)
except TelegramError:
pass
2018-11-20 20:06:07 +00:00
updater = Updater(config.BOT_TOKEN, workers=4)
2018-11-20 19:51:15 +00:00
updater.job_queue.run_repeating(task_queue, .04)
updater.dispatcher.add_handler(CommandHandler('start', go_away))
updater.dispatcher.add_handler(CommandHandler('stop', unsubscribe))
updater.dispatcher.add_handler(MessageHandler(Filters.all, msg))
if __name__ == '__main__':
updater.start_polling()
updater.idle()