Skip to content

Commit

Permalink
support send image in user contact.
Browse files Browse the repository at this point in the history
  • Loading branch information
林宇靖 committed Jul 24, 2021
1 parent e6683fa commit ab7c9e1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/appservice-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,11 @@ export class AppserviceManager extends Manager {
return this.bridge.getIntent(userId).uploadContent(content, opts)
}

public async mxcUrlToHttp (
mxcUrl: string,
): Promise<string> {
// also can use getHttpUriForMxc(this.baseUrl, mxcUrl, width, height, resizeMethod, allowDirectLinks);
return this.bridge.getIntent().client.mxcUrlToHttp(mxcUrl)
}

}
26 changes: 23 additions & 3 deletions src/matrix-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { DialogManager } from './dialog-manager'
import { MiddleManager } from './middle-manager'
import { WechatyManager } from './wechaty-manager'
import { UserManager } from './user-manager'
import { Contact, FileBox, Message, MiniProgram, UrlLink } from 'wechaty'

export class MatrixHandler {

Expand Down Expand Up @@ -326,9 +327,28 @@ export class MatrixHandler {
const contact = await this.middleManager.wechatyUser(service)
// const contact = await this.wechatyManager
// .wechatyContact(service, user)

const text = superEvent.event.content!.body
await contact.say(text + '')
const content = superEvent.event.content!
let mxcUrl = ''
let httpsUrl = ''
const body = superEvent.text()
let message : string|number|Message|Contact|FileBox|MiniProgram|UrlLink
= body
switch (content.msgtype) {
case 'm.text':
break
case 'm.image': case 'm.file':
mxcUrl = content.url as string
httpsUrl = await this.appserviceManager.mxcUrlToHttp(mxcUrl)
// XXX can't show Animation well in wechat.
message = FileBox.fromUrl(
httpsUrl,
body.indexOf('.') > -1 || content.msgtype !== 'm.image'
? body
: `${mxcUrl.split('/').pop()}.gif`,
)
}
// XXX removing the overload declarations may be a better choice.
await contact.say(message as any)
}

}
8 changes: 2 additions & 6 deletions src/middle-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,14 +479,8 @@ export class MiddleManager extends Manager {

/**
* Send message from service bot to the bridge consumer
*/
public async directMessageToMatrixConsumer (message: string, from: Wechaty): Promise<void>
/**
* Send message from user to the bridge consumer
*/
public async directMessageToMatrixConsumer (message: string, from: WechatyUser): Promise<void>
public async directMessageToMatrixConsumer (message: Message, from: WechatyUser): Promise<void>

public async directMessageToMatrixConsumer (
message: string | Message,
from: WechatyUser | Wechaty,
Expand All @@ -501,11 +495,13 @@ export class MiddleManager extends Manager {
let matrixUser

if (from instanceof WechatyUser) {
// receive message (in user side)

matrixRoom = await this.matrixRoom(from)
matrixUser = await this.matrixUser(from)

} else if (from instanceof Wechaty) {
// send message (in user side)

const consumerId = this.wechatyManager.matrixConsumerId(from)
matrixRoom = await this.adminRoom(consumerId)
Expand Down

0 comments on commit ab7c9e1

Please sign in to comment.