#!/usr/bin/env python3 import config import logging import requests import re from emoji import demojize from telegram.ext import Updater, CommandHandler, MessageHandler, Filters logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - \ %(message)s', level=logging.INFO) def get_text_or_type(msg, bot, truncate=False): message = "" # Set message contents if msg.text: if msg.from_user.username == bot.username and re.match('<.*?>(.*)', msg.text): x = re.compile('(<.*?>)(.*)') y = x.search(msg.text) z = y.group(2) message += (z if len(z) <= 20 or not truncate else z[0:19] + "...") else: message += (msg.text if len(msg.text) <= 20 or not truncate else msg.text[0:19] + "...") 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) <= 20 or not truncate else msg.caption[0:19] + "...") # Set sender return_message = "" for i in message.strip().split("\n"): if msg.from_user.username != bot.username or not re.match('<.*?>(.*)', msg.text): 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 += ("[> " + get_text_or_type(msg.reply_to_message, bot, True).split("\n")[0] + f"] ") return_message += i + "\n" return return_message.strip() def message_handling(bot, update): if update.message.chat.id in config.groups: message = get_text_or_type(update.message, bot) params = {"message": demojize(message)} requests.post("http://localhost:" + str(config.port) + "/post", data=params) def direct_cmd(bot, update, text): if update.message.chat.id in config.groups: params = {"message": text} requests.post("http://localhost:" + str(config.port) + "/post", data=params) updater = Updater(token=config.api_key) dispatcher = updater.dispatcher dispatcher.add_handler(CommandHandler('list', lambda x, y: direct_cmd(x, y, "list"))) dispatcher.add_handler(CommandHandler('time', lambda x, y: direct_cmd(x, y, "time"))) dispatcher.add_handler(MessageHandler(Filters.all, message_handling)) updater.start_polling()