未验证 提交 0202e761 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓) 提交者: GitHub

Create url link (#1888)

* UrlLink.create(url) with OpenGraph (#1887)

* 0.29.10

* add open graph supported

* 0.29.11
上级 3f7fbbc8
{
"name": "wechaty",
"version": "0.29.9",
"version": "0.29.11",
"description": "Wechaty is a Bot SDK for Wechat Personal Account",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
......@@ -87,6 +87,7 @@
"in-gfw": "^1.2.0",
"memory-card": "^0.6.9",
"npm-programmatic": "0.0.12",
"open-graph": "^0.2.4",
"opencollective": "^1.0.3",
"opencollective-postinstall": "^2.0.2",
"pkg-dir": "^4.0.0",
......@@ -115,6 +116,7 @@
"@types/glob": "^5.0.0p",
"@types/mime": "^2.0.0",
"@types/node": "^12.12.3",
"@types/open-graph": "^0.2.0",
"@types/promise-retry": "^1.1.3",
"@types/qr-image": "^3.2.1",
"@types/raven": "^2.1.0",
......
import og from 'open-graph'
export async function openGraph (url: string): Promise<og.Data> {
return new Promise((resolve, reject) => {
og(url, (err, meta) => {
if (err) {
reject(err)
} else {
resolve(meta)
}
})
})
}
import Url from 'url'
import {
UrlLinkPayload,
} from 'wechaty-puppet'
......@@ -6,6 +8,10 @@ import {
log,
} from '../config'
import {
openGraph,
} from '../helper-functions/impure/open-graph'
export class UrlLink {
/**
......@@ -16,11 +22,51 @@ export class UrlLink {
public static async create (url: string): Promise<UrlLink> {
log.verbose('UrlLink', 'create(%s)', url)
// TODO: get title/description/thumbnailUrl from url automatically
const meta = await openGraph(url)
let description: string | undefined
let imageUrl: string | undefined
let title: string
if (Array.isArray(meta.description)) {
description = meta.description[0]
} else if (meta.description) {
description = meta.description
}
if (meta.image) {
if (typeof meta.image === 'string') {
imageUrl = meta.image
} else if (Array.isArray(meta.image)) {
imageUrl = meta.image[0]
} else {
if (Array.isArray(meta.image.url)) {
imageUrl = meta.image.url[0]
} else if (meta.image.url) {
imageUrl = meta.image.url
}
}
}
if (Array.isArray(meta.title)) {
title = meta.title[0]
} else {
title = meta.title
}
if (!imageUrl || !description) {
throw new Error('imageUrl or description not found!')
}
if (!imageUrl.startsWith('http')) {
const resolvedUrl = new Url.URL(imageUrl, url)
imageUrl = resolvedUrl.toString()
}
const payload: UrlLinkPayload = {
description : 'todo',
thumbnailUrl : 'todo',
title : 'todo',
description,
thumbnailUrl: imageUrl,
title,
url,
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册