Skip to content

Commit

Permalink
New Components - chatfly (#11931)
Browse files Browse the repository at this point in the history
* chatfly init

* [Components] chatfly #11730
Actions
 - SendMessage

* pnpm update

* Update components/chatfly/chatfly.app.mjs

* fix description

---------

Co-authored-by: Leo Vu <[email protected]>
  • Loading branch information
luancazarine and vunguyenhung committed May 15, 2024
1 parent 85482fc commit e0d423e
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 8 deletions.
45 changes: 45 additions & 0 deletions components/chatfly/actions/send-message/send-message.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import chatfly from "../../chatfly.app.mjs";

export default {
key: "chatfly-send-message",
name: "Send Message",
description: "Dispatches a text message to a specified group or individual in Chatfly.",
version: "0.0.1",
type: "action",
props: {
chatfly,
botId: {
propDefinition: [
chatfly,
"botId",
],
},
sessionId: {
propDefinition: [
chatfly,
"sessionId",
({ botId }) => ({
botId,
}),
],
},
message: {
propDefinition: [
chatfly,
"message",
],
},
},
async run({ $ }) {
const response = await this.chatfly.dispatchMessage({
$,
data: {
bot_id: this.botId,
message: this.message,
session_id: this.sessionId,
},
});
$.export("$summary", "Message dispatched successfully");
return response;
},
};
85 changes: 80 additions & 5 deletions components/chatfly/chatfly.app.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,86 @@
import { axios } from "@pipedream/platform";
import { prepareSessionLabel } from "./common/utils.mjs";

export default {
type: "app",
app: "chatfly",
propDefinitions: {},
propDefinitions: {
botId: {
type: "string",
label: "Bot ID",
description: "The ID of the bot to send the message to.",
async options() {
const data = await this.listBots();

return data.map(({
id: value, bot_name: label,
}) => ({
label,
value,
}));
},
},
message: {
type: "string",
label: "Message",
description: "The message to send.",
},
sessionId: {
type: "string",
label: "Session ID",
description: "The session ID for the message. To initiate a new session, use a unique string in a Custom Expression, for example: `8g12h`",
async options({ botId }) {
const data = await this.listSessions({
params: {
bot_id: botId,
},
});

return data.map(({
session_id: value, chat_history_response,
}) => ({
label: prepareSessionLabel(chat_history_response),
value,
}));
},
},
},
methods: {
// this.$auth contains connected account data
authKeys() {
console.log(Object.keys(this.$auth));
_baseUrl() {
return "https://backend.chatfly.co/api";
},
_headers() {
return {
"CHATFLY-API-KEY": `${this.$auth.api_key}`,
};
},
_makeRequest({
$ = this, path = "/", ...opts
}) {
return axios($, {
url: this._baseUrl() + path,
headers: this._headers(),
...opts,
});
},
dispatchMessage(opts = {}) {
return this._makeRequest({
method: "POST",
path: "/chat/get-streaming-response",
...opts,
});
},
listBots(opts = {}) {
return this._makeRequest({
path: "/bot",
...opts,
});
},
listSessions(opts = {}) {
return this._makeRequest({
path: "/chat/history",
...opts,
});
},
},
};
};
5 changes: 5 additions & 0 deletions components/chatfly/common/utils.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const prepareSessionLabel = (messages) => {
return `${messages[messages.length - 1].content.substring(0, 60)} ${(messages[messages.length - 1].content.length > 60)
? "..."
: ""}`;
};
7 changes: 5 additions & 2 deletions components/chatfly/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/chatfly",
"version": "0.0.1",
"version": "0.1.0",
"description": "Pipedream ChatFly Components",
"main": "chatfly.app.mjs",
"keywords": [
Expand All @@ -11,5 +11,8 @@
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@pipedream/platform": "^1.6.5"
}
}
}
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e0d423e

Please sign in to comment.