Compare commits

...

2 Commits

  1. 46
      teledynmap.py

@ -1,6 +1,6 @@
import aiohttp
import asyncio
import emoji
import hashlib
import logging
import re
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)
web = None
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.info('Loaded {} emojis'.format(len(emojis)))
logging.info('Bridge initialized')
@bot.on(events.NewMessage(incoming=True))
@ -27,13 +29,17 @@ async def new_message(event):
logging.info('No text found')
return
name = re.sub(r'\W+', '', sender.first_name) + '(TG)'
name = name.replace('Somebody', 'Applezaus')
name = re.sub(r'\W+', '', sender.first_name)
name = '[TG] ' + settings.CUSTOM_NAMES.get(name, name)
logging.info('[SEND] {}: {}'.format(name, 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():
global web
@ -43,21 +49,25 @@ async def main():
logging.info('Bridge loaded')
while True:
async with web.get(api('/up/world/world/' + str(last_time))) as res:
j = await res.json(content_type='text/plain;charset=utf-8')
last_time = j['timestamp']
for update in j['updates']:
if update['type'] != 'chat': continue
if update['playerName'].endswith('(TG)'): continue
try:
async with web.get(api('/up/world/world/' + str(last_time))) as res:
j = await res.json(content_type='text/plain;charset=utf-8')
last_time = j['timestamp']
for update in j['updates']:
if update['type'] != 'chat': continue
if update['playerName'].startswith('[TG] '): continue
name = update['playerName']
icon = emojis[hash(name) % len(emojis)]
message = '{} <{}> {}'.format(icon, name, update['message'])
if message == last_msg: continue
name = update['playerName']
icon = emojis[md5(name) % len(emojis)]
icon = settings.CUSTOM_EMOJIS.get(name, icon)
message = '{} <{}> {}'.format(icon, name, update['message'])
if message == last_msg: continue
logging.info('[RECV] ' + message)
await bot.send_message(settings.CHAT_ID, message)
last_msg = message
logging.info('[RECV] ' + message)
await bot.send_message(settings.CHAT_ID, message)
last_msg = message
except aiohttp.ClientError:
logging.exception('Problem getting message from dynmap:')
await asyncio.sleep(1)

Loading…
Cancel
Save