Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

feat:slack connector #47

Merged
merged 6 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/apps/slack/src/services/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class SlackOauth extends OauthService {
};

return axios
.post('https:/slack.com/api/oauth.v2.exchange', body, config)
.post('https:/slack.com/api/oauth.v2.access', body, config)
.then((res) => {
return {
type: res.data.token_type,
Expand Down
47 changes: 26 additions & 21 deletions packages/apps/slack/src/services/slack.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import fs from "fs";
import axios from "axios";
import { Readable } from "stream";
import { OAuthService, Organisation } from "@ocular/ocular";
import { App, OAuthService, Organisation } from "@ocular/ocular";
import {
IndexableDocument,
TransactionBaseService,
Logger,
AppNameDefinitions,
Section,
} from "@ocular/types";
import { ConfigModule } from "@ocular/ocular/src/types";

Expand Down Expand Up @@ -60,37 +61,31 @@ export default class SlackService extends TransactionBaseService {

try {
const slackChannels = await this.fetchSlackChannels(config)

for (const channel of slackChannels) {
const conversations = await this.fetchChannelConversations(channel.id,config);
for(const conversation of conversations){
const doc : IndexableDocument = {
id: conversation.ts,
const thread = await this.fetchThreadForConversation(channel.id,conversation.ts,config)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a bug, .ts does not exist on conversation. Similarly, it's non-existent for the references in this file. Consider changing to conversation.id

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey id will not work . Please refer to the following doc https://api.slack.com/methods/conversations.history

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, refer to the other comment on this file, ts does not exist on the conversation object


const sections: Section[] = thread.map((message, index) => ({
offset: index,
content: message.text,
link: `https://slack.com/api/conversations.replies?channel_id=${channel.id}&ts=${conversation.ts}`
}));
const threadDoc : IndexableDocument = {
id:conversation.ts, // conversation id
organisationId:org.id,
title:conversation.text,
source:AppNameDefinitions.SLACK,
updatedAt: new Date(Date.now()),
metadata:{sentByUser:conversation.user}
title:conversation.text, // the main message which lead to conversation
metadata:{channel_id:channel.id}, // passing channel id just for top down reference
sections:sections, // an array of messages in the specific conversation
updatedAt: new Date(Date.now())
khareyash05 marked this conversation as resolved.
Show resolved Hide resolved
}
documents.push(doc);
documents.push(threadDoc);
if (documents.length >= 100) {
yield documents;
documents = [];
}
}
// add channel names to document(if we want find a context by channel names)
const channelDoc: IndexableDocument = {
id:channel.id,
organisationId:org.id,
source: AppNameDefinitions.SLACK,
title: channel.name,
updatedAt: new Date(channel.updated),
metadata:{
topic: channel.topic.value,
purpose: channel.purpose.value
}
}
documents.push(channelDoc)
}
yield documents;
await this.oauthService_.update(oauth.id, {
Expand Down Expand Up @@ -156,4 +151,14 @@ export default class SlackService extends TransactionBaseService {
throw new Error("Failed to fetch channel conversation.");
}
}

async fetchThreadForConversation(channelID, tsID, config){
khareyash05 marked this conversation as resolved.
Show resolved Hide resolved
try{
const threadsEndpoint = `https://slack.com/api/conversations.replies?channel_id=${channelID}&ts=${tsID}`
const response = await axios.get(threadsEndpoint,config)
return response.data.messages
}catch(error){
throw new Error("Failed to fetch channel conversation.");
}
}
}