hacky way to make template work in two places
This commit is contained in:
parent
9b3dd1906f
commit
8bbda6ae7a
2 changed files with 21 additions and 16 deletions
|
@ -36,6 +36,7 @@ class Cron:
|
|||
await asyncio.sleep(wait_seconds)
|
||||
message = self.responder.get_response(tokens)
|
||||
message = self.responder.api_match(message, '')
|
||||
message = self.responder.rndm_tmpl(message, job['chat'])
|
||||
await self.client.send_message(job['chat'], message)
|
||||
return send_message
|
||||
|
||||
|
|
|
@ -90,20 +90,23 @@ class Responder:
|
|||
api_spec = match.groups()
|
||||
return self.api.call(*api_spec, message)
|
||||
|
||||
async def template(self, response: str,
|
||||
async def user_tmpl(self, response: str,
|
||||
event: telethon.events.common.EventBuilder) -> str:
|
||||
result = response
|
||||
if '<username>' in response:
|
||||
marker = '<username>'
|
||||
if marker not in response:
|
||||
return response
|
||||
sender = await event.get_sender()
|
||||
username = self.namegen.get_name(sender)
|
||||
result = result.replace('<username>', username)
|
||||
if '<randomname>' in response:
|
||||
peer_id = event.message.peer_id
|
||||
users = await self.client.get_participants(entity=peer_id)
|
||||
return response.replace(marker, username)
|
||||
|
||||
async def rndm_tmpl(self, response: str, chat_id: str) -> str:
|
||||
marker = '<randomname>'
|
||||
if marker not in response:
|
||||
return response
|
||||
users = await self.client.get_participants(entity=chat_id)
|
||||
victim = random.choice(users)
|
||||
username = self.namegen.get_name(victim)
|
||||
result = result.replace('<randomname>', username)
|
||||
return result
|
||||
return response.replace('<randomname>', username)
|
||||
|
||||
async def respond(self,
|
||||
event: telethon.events.common.EventBuilder) -> None:
|
||||
|
@ -118,5 +121,6 @@ class Responder:
|
|||
if not response:
|
||||
return None
|
||||
response = self.api_match(response, message)
|
||||
response = await self.template(response, event)
|
||||
await self.client.send_message(event.message.peer_id, response)
|
||||
response = await self.user_tmpl(response, event)
|
||||
response = await self.rndm_tmpl(response, chat_id)
|
||||
await self.client.send_message(chat_id, response)
|
||||
|
|
Loading…
Reference in a new issue