Support custom names and emojis

master
Tanner Collin 3 years ago
parent d468c686e7
commit 5df67e3241
  1. 15
      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,8 +29,8 @@ 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))
@ -48,10 +50,11 @@ async def main():
last_time = j['timestamp']
for update in j['updates']:
if update['type'] != 'chat': continue
if update['playerName'].endswith('(TG)'): continue
if update['playerName'].startswith('[TG] '): continue
name = update['playerName']
icon = emojis[hash(name) % len(emojis)]
icon = emojis[md5(name) % len(emojis)]
icon = settings.CUSTOM_EMOJIS.get(name, icon)
message = '{} <{}> {}'.format(icon, name, update['message'])
if message == last_msg: continue

Loading…
Cancel
Save