Skip to content

Instantly share code, notes, and snippets.

@kaecy
Last active June 29, 2022 21:46
Show Gist options
  • Save kaecy/55d3769e36c7630d41722893ef80994f to your computer and use it in GitHub Desktop.
Save kaecy/55d3769e36c7630d41722893ef80994f to your computer and use it in GitHub Desktop.
Chat Relay Module
# Uses a single Telegram node to relay messages from one to many.
# Receivers receive messages and can reply to senders. Senders never see who the receivers are
# and receive anonymous messages from receivers.
# - Setup to begin receiving messages -
# 1. Use `/receiver admin` to set a receiver and you're setup
import raey.core.module
from telethon.utils import get_display_name
moduleInfo = raey.core.module.ModuleInfo("relay")
relay = {}
admin_pass = "admin" # Change this!
receivers = []
sender = {}
async def messages(event, params):
global sender
if receivers:
if event.chat in receivers:
if event.is_reply:
replyMsg = await event.get_reply_message()
if replyMsg.id in relay[event.sender_id]:
sender[event.sender.id] = relay[event.sender_id][replyMsg.id]
print("Relay:sender set to %s" % sender[event.sender_id].id)
await event.respond("Messages set to be sent to %s." % get_display_name(sender[event.sender_id]))
print("Relay:from %s to sender %s" % (event.sender_id, sender[event.sender_id].id))
await event.client.send_message(relay[event.sender_id][replyMsg.id], event.message)
elif sender.get(event.sender_id, None):
print("Relay:from %s to sender %s" % (event.sender_id, sender[event.sender_id].id))
await event.client.send_message(sender[event.sender_id], event.message)
return
if event.is_private:
for receiver in receivers:
msg = await event.forward_to(receiver)
for item in list(filter(lambda e: e[1] == event.chat, relay[receiver.id].items())):
del relay[receiver.id][item[0]]
relay[receiver.id][msg.id] = event.chat
print("Relay:from %s to receiver %s" % (event.sender.id, receiver.id))
async def receiver(event, params):
"Set the receiver of the relay messages."
global receivers
if len(params) == 1:
if params[0] == admin_pass:
receivers.append(event.sender)
relay[event.sender_id] = {}
await event.respond("Added you to receivers.")
moduleInfo.add_command(receiver)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment