Compare commits

...

2 Commits

Author SHA1 Message Date
9d41cafb3e Handle errors better 2021-03-26 21:06:58 +00:00
5df67e3241 Support custom names and emojis 2021-03-04 23:43:36 +00:00

View File

@ -1,6 +1,6 @@
import aiohttp import aiohttp
import asyncio import asyncio
import emoji import hashlib
import logging import logging
import re import re
import settings import settings
@ -9,9 +9,11 @@ from telethon import TelegramClient, events
bot = TelegramClient('bot', settings.API_ID, settings.API_HASH).start(bot_token=settings.API_TOKEN) bot = TelegramClient('bot', settings.API_ID, settings.API_HASH).start(bot_token=settings.API_TOKEN)
web = None web = None
api = lambda route: settings.DYNMAP_ADDRESS + route api = lambda route: settings.DYNMAP_ADDRESS + route
emojis = list(emoji.unicode_codes.UNICODE_EMOJI_ENGLISH.keys()) md5 = lambda name: int(hashlib.md5(name.encode()).hexdigest(), 16)
emojis = list('🌹🏵🌻🌼🍀🏞🌊⛄️❄️🏔🌡🔥🏜☀️💧☔️⚡️🌈⭐️🌍🌙🪐🐵🦁🐯🐱🐶🐺🐻🐼🐹🐰🦊🦝🦓🦄🐸🐢🐕🐆🐏🦥🐘🦏🦒🦍🐪🦦🐔🐤🦉🦜🦢🦩🐧🐬🐠🐡🦐🐙🕷🐌🐞🦋🍓🍒🍎🍉🍍🍌🍋🍐🥝🍇🥥🍅🌶🍄🥕🧅🌽🥦🥑🍞🥞🧇🧀🥓🍔🌭🥪🥨🍟🍕🌮🥙🍜🍣🍭🍩🍪🍯🍿🍺🍷🛹🚀🛸🛩🎉🎈🎀🎁⚾️🏀🏐🏈🎯🎹🎷🎺🎸💵💰💡💎🟥🟧🟨🟩🟦🟪')
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
logging.info('Loaded {} emojis'.format(len(emojis)))
logging.info('Bridge initialized') logging.info('Bridge initialized')
@bot.on(events.NewMessage(incoming=True)) @bot.on(events.NewMessage(incoming=True))
@ -27,13 +29,17 @@ async def new_message(event):
logging.info('No text found') logging.info('No text found')
return return
name = re.sub(r'\W+', '', sender.first_name) + '(TG)' name = re.sub(r'\W+', '', sender.first_name)
name = name.replace('Somebody', 'Applezaus') name = '[TG] ' + settings.CUSTOM_NAMES.get(name, name)
logging.info('[SEND] {}: {}'.format(name, text)) logging.info('[SEND] {}: {}'.format(name, text))
data = dict(name=name, message=text) data = dict(name=name, message=text)
await web.post(api('/up/sendmessage'), json=data) try:
await web.post(api('/up/sendmessage'), json=data)
except aiohttp.ClientError:
logging.exception('Problem sending message to dynmap:')
async def main(): async def main():
global web global web
@ -43,21 +49,25 @@ async def main():
logging.info('Bridge loaded') logging.info('Bridge loaded')
while True: while True:
async with web.get(api('/up/world/world/' + str(last_time))) as res: try:
j = await res.json(content_type='text/plain;charset=utf-8') async with web.get(api('/up/world/world/' + str(last_time))) as res:
last_time = j['timestamp'] j = await res.json(content_type='text/plain;charset=utf-8')
for update in j['updates']: last_time = j['timestamp']
if update['type'] != 'chat': continue for update in j['updates']:
if update['playerName'].endswith('(TG)'): continue if update['type'] != 'chat': continue
if update['playerName'].startswith('[TG] '): continue
name = update['playerName'] name = update['playerName']
icon = emojis[hash(name) % len(emojis)] icon = emojis[md5(name) % len(emojis)]
message = '{} <{}> {}'.format(icon, name, update['message']) icon = settings.CUSTOM_EMOJIS.get(name, icon)
if message == last_msg: continue message = '{} <{}> {}'.format(icon, name, update['message'])
if message == last_msg: continue
logging.info('[RECV] ' + message) logging.info('[RECV] ' + message)
await bot.send_message(settings.CHAT_ID, message) await bot.send_message(settings.CHAT_ID, message)
last_msg = message last_msg = message
except aiohttp.ClientError:
logging.exception('Problem getting message from dynmap:')
await asyncio.sleep(1) await asyncio.sleep(1)