minetest-telegram-bridge/bot/bot.py

103 lines
3.3 KiB
Python
Raw Normal View History

2019-01-22 11:17:37 +01:00
#!/usr/bin/env python3
import config
import logging
import requests
2019-01-28 12:02:31 +01:00
import re
2019-01-28 12:48:15 +01:00
from emoji import demojize
2019-01-28 21:35:39 +01:00
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
2019-01-22 11:17:37 +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)
2019-01-28 12:48:15 +01:00
message += (z if len(z) <= 20 or not truncate else z[0:19] + "...")
2019-01-28 12:02:31 +01:00
else:
2019-01-28 12:48:15 +01:00
message += (msg.text if len(msg.text) <= 20 or not truncate else
msg.text[0:19] + "...")
2019-01-28 12:02:31 +01:00
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:
2019-01-28 12:48:15 +01:00
message += (msg.caption if len(msg.caption) <= 20 or not truncate else
msg.caption[0:19] + "...")
2019-01-28 12:02:31 +01:00
# 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:
2019-01-28 12:48:15 +01:00
return_message += ("[> " +
2019-01-28 12:02:31 +01:00
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):
if update.message.chat.id in config.groups:
2019-01-28 12:02:31 +01:00
message = get_text_or_type(update.message, bot)
2019-01-28 12:48:15 +01:00
params = {"message": demojize(message)}
2019-01-28 12:02:31 +01:00
requests.post("http://localhost:" + str(config.port) + "/post",
data=params)
2019-01-22 11:17:37 +01:00
2019-01-28 22:27:31 +01:00
def direct_cmd(bot, update, text):
2019-01-28 21:35:39 +01:00
if update.message.chat.id in config.groups:
2019-01-28 22:27:31 +01:00
params = {"message": text}
2019-01-28 21:35:39 +01:00
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 22:27:31 +01:00
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")))
2019-01-28 12:02:31 +01:00
dispatcher.add_handler(MessageHandler(Filters.all, message_handling))
updater.start_polling()