wechaty / matrix-appservice

Wechaty [Matrix] Application Services Bridge
https://matrix.org/docs/projects/bridge/matrix-appservice-wechaty
Apache License 2.0
132 stars 16 forks source link

The best practice for bridge direct message from Wechaty to Matrix users #2

Open huan opened 5 years ago

huan commented 5 years ago

Recently I'm keeping thinking about what's the best practice for our app service to help matrix users to receive wechat message from wechat individual user.

The scenario is:

Background

  1. Eric install matrix-appservice-wechaty in his home server pulsar.im
  2. Eric set up the app service bot name as @wechaty:pulsar.im, also reserved all the namespace that matching the regex ^[#!@]wechaty_.*$ to be controlled by this app service.
  3. Eric enabled/registered the matrix-appservice-wechaty to his home server
  4. Eric adds the wechaty bot @wechaty:pulsar.im as his friend, and talk to the bot, get the Wechat login QR Code, then scan it to let the bot login the Wechat.

After the above steps, the matrix-appservice-wechaty had logged in on behalf of Eric and will be ready to receive/send Wechat messages for Eric.

The Problem

Let's say, Bob is a friend of Eric on Wechat

Step 1. Bob send a Wechat message to Eric

  1. matrix-appservice-wechaty received a message from Bob to Eric
  2. app service build a ghost user @wechaty_bob_wechat_id:pulsar.im
  3. app service create a new room and invite @eric:pulsar.im and wechaty_bob_wechat_id:pulsar.im to that room
  4. app service send message to the new room, and set the sender to be wechaty_bob_wechat_id:pulsar.im
Step 2. Bob send another Wechat message to Eric
  1. all the same as step 1

If so, we will create a new room for each new message in a conversation and end up with lots of new rooms with only one message in it.

One of the solutions would be: we save the room id when we create it for the first time, and then use it when there's any new message comes in the future.

The Question

I can use a store to map the wechat id to matrix room id, and whenever a wechat user sends a message to Eric, I'll try to find an existing matrix room id first. If it exists, I'll send the message via that room.

  1. Does that the standard implementation for doing this?
  2. Does there have other ways to do this?
  3. What is the best practice to solve this problem?
tulir commented 5 years ago

You'll need to store the mapping somewhere. The most common solution is to have some kind of database (NeDB, SQLite, Postgres, etc) to store all the rooms used by the bridge:

huan commented 5 years ago

@tulir Thank you so much for answer my question in such a detail reply, appreciate it!