Skip to content

Commit

Permalink
PlatformAPI.getLinkPreview
Browse files Browse the repository at this point in the history
  • Loading branch information
KishanBagaria committed Sep 3, 2022
1 parent 3b57d17 commit 9d7d989
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
33 changes: 33 additions & 0 deletions fixtures/unfurlLink.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"ok": true,
"attachments": {
"https://texts.com/": {
"from_url": "https://texts.com/",
"title": "Texts",
"title_link": "https://texts.com/",
"text": "The ultimate messaging app",
"image_url": "https://texts.com/ogcover.png",
"image_width": 500,
"image_height": 250,
"image_bytes": 139283,
"fallback": "Texts",
"service_icon": "https://texts.com/favicon.ico",
"service_name": "texts.com",
"id": 1,
"original_url": "https://texts.com/"
}
},
"response_metadata": {
"scopes": [
"identify",
"read",
"post",
"client",
"apps",
"admin"
],
"acceptedScopes": [
"read"
]
}
}
10 changes: 8 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InboxName, PaginationArg, Paginated, Thread, Message, PlatformAPI, OnServerEventCallback, LoginResult, ReAuthError, ActivityType, MessageContent, AccountInfo, CustomEmojiMap, ServerEventType, LoginCreds, texts, NotificationsInfo } from '@textshq/platform-sdk'
import { InboxName, PaginationArg, Paginated, Thread, Message, PlatformAPI, OnServerEventCallback, LoginResult, ReAuthError, ActivityType, MessageContent, AccountInfo, CustomEmojiMap, ServerEventType, LoginCreds, texts, NotificationsInfo, MessageLink } from '@textshq/platform-sdk'
import { CookieJar } from 'tough-cookie'
import { mapCurrentUser, mapThreads, mapMessage, mapParticipant } from './mappers'
import { mapCurrentUser, mapThreads, mapMessage, mapParticipant, mapLinkAttachment } from './mappers'
import { MESSAGE_REPLY_THREAD_PREFIX } from './constants'
import { textsTime } from './util'

Expand Down Expand Up @@ -217,6 +217,12 @@ export default class Slack implements PlatformAPI {
}
}

getLinkPreview = async (link: string): Promise<MessageLink> => {
const res = await this.api.unfurlLink(link)
const att = res.attachments[link]
if (att) return mapLinkAttachment(att)
}

sendReadReceipt = (threadID: string, messageID: string) =>
this.api.sendReadReceipt(threadID, messageID)

Expand Down
3 changes: 3 additions & 0 deletions src/lib/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,9 @@ export default class SlackAPI {
await this.webClient.conversations.mark({ channel: threadID, ts: messageID })
}

unfurlLink = (link: string) =>
this.webClient.apiCall('chat.unfurlLink', { url: link })

addReaction = async (channel: string, timestamp: string, reactionKey: string) => {
const name = emojiToShortcode(reactionKey) || reactionKey
await this.webClient.reactions.add({ name, channel, timestamp })
Expand Down
2 changes: 1 addition & 1 deletion src/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ const ACTION_MESSAGE_TYPES = new Set([
'joiner_notification_for_inviter',
])

const mapLinkAttachment = ({
export const mapLinkAttachment = ({
title,
title_link,
text,
Expand Down

0 comments on commit 9d7d989

Please sign in to comment.