Add option to send bot messages as m.notice. Fixes #121

This commit is contained in:
Tulir Asokan
2018-04-29 23:48:02 +03:00
parent 8354bf6bb5
commit 73e7b8f635
6 changed files with 41 additions and 6 deletions
@@ -0,0 +1,24 @@
"""Add is_bot field to puppets
Revision ID: 1fa46383a9d3
Revises: 30eca60587f1
Create Date: 2018-04-29 23:44:40.102333
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '1fa46383a9d3'
down_revision = '30eca60587f1'
branch_labels = None
depends_on = None
def upgrade():
op.add_column('puppet', sa.Column('is_bot', sa.Boolean(), nullable=True))
def downgrade():
op.drop_column('puppet', 'is_bot')
+2
View File
@@ -85,6 +85,8 @@ bridge:
highlight_edits: false
# Whether or not Matrix bot messages (type m.notice) should be bridged.
bridge_notices: true
# Whether to bridge Telegram bot messages as m.notices or m.texts.
bot_messages_as_notices: true
# Maximum number of members to sync per portal when starting up. Other members will be
# synced when they send messages. The maximum is 10000, after which the Telegram server
# will not send any more members.
+1
View File
@@ -171,6 +171,7 @@ class Config(DictWithRecursion):
copy("bridge.edits_as_replies")
copy("bridge.highlight_edits")
copy("bridge.bridge_notices")
copy("bridge.bot_messages_as_notices")
copy("bridge.max_initial_member_sync")
copy("bridge.max_telegram_delete")
copy("bridge.allow_matrix_login")
+1
View File
@@ -94,6 +94,7 @@ class Puppet(Base):
displayname = Column(String, nullable=True)
username = Column(String, nullable=True)
photo_id = Column(String, nullable=True)
is_bot = Column(Boolean, nullable=True)
# Fucking Telegram not telling bots what chats they are in 3:<
+6 -3
View File
@@ -1149,12 +1149,14 @@ class Portal:
"m.relates_to": relates_to or None,
}, timestamp=evt.date, external_url=self.get_external_url(evt))
async def handle_telegram_text(self, source, intent, evt):
async def handle_telegram_text(self, source, intent, is_bot, evt):
self.log.debug(f"Sending {evt.message} to {self.mxid} by {intent.mxid}")
text, html, relates_to = await formatter.telegram_to_matrix(evt, source, self.main_intent)
await intent.set_typing(self.mxid, is_typing=False)
msgtype = "m.notice" if is_bot and config["bridge.bot_messages_as_notices"] else "m.text"
return await intent.send_text(self.mxid, text, html=html, relates_to=relates_to,
timestamp=evt.date, external_url=self.get_external_url(evt))
msgtype=msgtype, timestamp=evt.date,
external_url=self.get_external_url(evt))
async def handle_telegram_edit(self, source, sender, evt):
if not self.mxid:
@@ -1229,7 +1231,8 @@ class Portal:
allowed_media) else None
intent = sender.intent if sender else self.main_intent
if not media and evt.message:
response = await self.handle_telegram_text(source, intent, evt)
is_bot = sender.is_bot if sender else False
response = await self.handle_telegram_text(source, intent, is_bot, evt)
elif media:
relates_to = formatter.telegram_reply_to_matrix(evt, source)
if isinstance(media, MessageMediaPhoto):
+7 -3
View File
@@ -36,13 +36,14 @@ class Puppet:
hs_domain = None
cache = {}
def __init__(self, id=None, username=None, displayname=None, photo_id=None, db_instance=None):
def __init__(self, id=None, username=None, displayname=None, photo_id=None, is_bot=None, db_instance=None):
self.id = id
self.mxid = self.get_mxid_from_id(self.id)
self.username = username
self.displayname = displayname
self.photo_id = photo_id
self.is_bot = is_bot
self._db_instance = db_instance
self.intent = self.az.intent.user(self.mxid)
@@ -62,17 +63,18 @@ class Puppet:
def new_db_instance(self):
return DBPuppet(id=self.id, username=self.username, displayname=self.displayname,
photo_id=self.photo_id)
photo_id=self.photo_id, is_bot=self.is_bot)
@classmethod
def from_db(cls, db_puppet):
return Puppet(db_puppet.id, db_puppet.username, db_puppet.displayname, db_puppet.photo_id,
return Puppet(db_puppet.id, db_puppet.username, db_puppet.displayname, db_puppet.photo_id, db_puppet.is_bot,
db_instance=db_puppet)
def save(self):
self.db_instance.username = self.username
self.db_instance.displayname = self.displayname
self.db_instance.photo_id = self.photo_id
self.db_instance.is_bot = self.is_bot
self.db.commit()
def similarity(self, query):
@@ -121,6 +123,8 @@ class Puppet:
if isinstance(info.photo, UserProfilePhoto):
changed = await self.update_avatar(source, info.photo.photo_big) or changed
self.is_bot = info.bot
if changed:
self.save()