Support custom names and emojis

This commit is contained in:
Tanner Collin 2021-03-04 23:43:36 +00:00
parent d468c686e7
commit 5df67e3241

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,8 +29,8 @@ 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))
@ -48,10 +50,11 @@ async def main():
last_time = j['timestamp'] last_time = j['timestamp']
for update in j['updates']: for update in j['updates']:
if update['type'] != 'chat': continue if update['type'] != 'chat': continue
if update['playerName'].endswith('(TG)'): 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)]
icon = settings.CUSTOM_EMOJIS.get(name, icon)
message = '{} <{}> {}'.format(icon, name, update['message']) message = '{} <{}> {}'.format(icon, name, update['message'])
if message == last_msg: continue if message == last_msg: continue