2019-01-22 11:17:37 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import config
|
2019-01-22 12:24:57 +01:00
|
|
|
import logging
|
|
|
|
import requests
|
2019-01-28 12:02:31 +01:00
|
|
|
import re
|
2019-01-22 11:17:37 +01:00
|
|
|
from telegram.ext import Updater, MessageHandler, Filters
|
|
|
|
|
2019-01-22 12:24:57 +01:00
|
|
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - \
|
|
|
|
%(message)s', level=logging.INFO)
|
|
|
|
|
2019-01-22 11:17:37 +01:00
|
|
|
|
2019-01-28 12:02:31 +01:00
|
|
|
def get_text_or_type(msg, bot, truncate=False):
|
|
|
|
message = ""
|
|
|
|
|
|
|
|
# Set message contents
|
|
|
|
if msg.text:
|
|
|
|
if msg.from_user.username == bot.username:
|
|
|
|
x = re.compile('(<.*?>)(.*)')
|
|
|
|
y = x.search(msg.text)
|
|
|
|
z = y.group(2)
|
|
|
|
message += (z if len(z) <= 10 or not truncate else z[0:9] + "...")
|
|
|
|
else:
|
|
|
|
message += (msg.text if len(msg.text) <= 10 or not truncate else
|
|
|
|
msg.text[0:9] + "...")
|
|
|
|
elif msg.audio:
|
|
|
|
message += "[Audio]"
|
|
|
|
elif msg.document:
|
|
|
|
message += "[Document]"
|
|
|
|
elif msg.animation:
|
|
|
|
message += "[GIF]"
|
|
|
|
elif msg.game:
|
|
|
|
message += "[Game]"
|
|
|
|
elif msg.photo:
|
|
|
|
message += "[Photo]"
|
|
|
|
elif msg.sticker:
|
|
|
|
message += "[Sticker]"
|
|
|
|
elif msg.video:
|
|
|
|
message += "[Video]"
|
|
|
|
elif msg.voice:
|
|
|
|
message += "[Voice Note]"
|
|
|
|
elif msg.video_note:
|
|
|
|
message += "[Video Note]"
|
|
|
|
elif msg.contact:
|
|
|
|
message += "[Contact]"
|
|
|
|
elif msg.location:
|
|
|
|
message += "[Location]"
|
|
|
|
elif msg.venue:
|
|
|
|
message += "[Venue]"
|
|
|
|
else:
|
|
|
|
message += "[Unknown type]"
|
|
|
|
|
|
|
|
# If caption exists
|
|
|
|
if msg.caption:
|
|
|
|
message += (msg.caption if len(msg.caption) <= 10 or not truncate else
|
|
|
|
msg.caption[0:9] + "...")
|
|
|
|
|
|
|
|
# Set sender
|
|
|
|
return_message = ""
|
|
|
|
for i in message.strip().split("\n"):
|
|
|
|
if msg.from_user.username != bot.username:
|
|
|
|
if msg.from_user.username:
|
|
|
|
return_message += f"@{msg.from_user.username}: "
|
|
|
|
else:
|
|
|
|
return_message += f"{msg.from_user.first_name}: "
|
|
|
|
else:
|
|
|
|
x = re.compile('(<.*?>)(.*)')
|
|
|
|
y = x.search(msg.text)
|
|
|
|
return_message = y.group(1) + ": " + i + "\n"
|
|
|
|
# Set the replied to message's contents
|
|
|
|
if msg.reply_to_message:
|
|
|
|
return_message += ("[Reply to " +
|
|
|
|
get_text_or_type(msg.reply_to_message, bot,
|
|
|
|
True).split("\n")[0] +
|
|
|
|
f"] ")
|
|
|
|
return_message += i + "\n"
|
|
|
|
|
|
|
|
return return_message.strip()
|
|
|
|
|
|
|
|
|
2019-01-22 11:17:37 +01:00
|
|
|
def message_handling(bot, update):
|
2019-01-22 12:24:57 +01:00
|
|
|
if update.message.chat.id in config.groups:
|
2019-01-28 12:02:31 +01:00
|
|
|
message = get_text_or_type(update.message, bot)
|
|
|
|
params = {"message": message}
|
|
|
|
requests.post("http://localhost:" + str(config.port) + "/post",
|
|
|
|
data=params)
|
2019-01-22 11:17:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
updater = Updater(token=config.api_key)
|
|
|
|
dispatcher = updater.dispatcher
|
2019-01-28 12:02:31 +01:00
|
|
|
dispatcher.add_handler(MessageHandler(Filters.all, message_handling))
|
2019-01-22 12:24:57 +01:00
|
|
|
updater.start_polling()
|