|
|
|
@ -1,5 +1,6 @@ |
|
|
|
|
import aiohttp |
|
|
|
|
import asyncio |
|
|
|
|
import emoji |
|
|
|
|
import logging |
|
|
|
|
import re |
|
|
|
|
import settings |
|
|
|
@ -8,8 +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()) |
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
|
|
|
|
|
|
|
|
logging.info('Bridge initialized') |
|
|
|
|
|
|
|
|
|
@bot.on(events.NewMessage(incoming=True)) |
|
|
|
|
async def new_message(event): |
|
|
|
|
text = event.raw_text |
|
|
|
@ -24,6 +28,7 @@ async def new_message(event): |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
name = re.sub(r'\W+', '', sender.first_name) + '(TG)' |
|
|
|
|
name = name.replace('Somebody', 'Applezaus') |
|
|
|
|
|
|
|
|
|
logging.info('[SEND] {}: {}'.format(name, text)) |
|
|
|
|
|
|
|
|
@ -35,6 +40,7 @@ async def main(): |
|
|
|
|
web = aiohttp.ClientSession() |
|
|
|
|
last_time = 0 |
|
|
|
|
last_msg = '' |
|
|
|
|
logging.info('Bridge loaded') |
|
|
|
|
|
|
|
|
|
while True: |
|
|
|
|
async with web.get(api('/up/world/world/' + str(last_time))) as res: |
|
|
|
@ -43,7 +49,10 @@ async def main(): |
|
|
|
|
for update in j['updates']: |
|
|
|
|
if update['type'] != 'chat': continue |
|
|
|
|
if update['playerName'].endswith('(TG)'): continue |
|
|
|
|
message = '<{}> {}'.format(update['playerName'], update['message']) |
|
|
|
|
|
|
|
|
|
name = update['playerName'] |
|
|
|
|
icon = emojis[hash(name) % len(emojis)] |
|
|
|
|
message = '{} <{}> {}'.format(icon, name, update['message']) |
|
|
|
|
if message == last_msg: continue |
|
|
|
|
|
|
|
|
|
logging.info('[RECV] ' + message) |
|
|
|
|