Skip to content

Commit

Permalink
init telegram plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmyn5600 committed Jan 12, 2024
1 parent b32f56c commit c93ef12
Show file tree
Hide file tree
Showing 18 changed files with 618 additions and 2 deletions.
6 changes: 5 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"recommendations": ["ms-playwright.playwright"]
"recommendations": [
"ms-playwright.playwright",
"esbenp.prettier-vscode",
"firsttris.vscode-jest-runner"
]
}
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"search.exclude": {
"**/node_modules": true,
"**/build": true
}
},
"eslint.validate": ["json"]
}
1 change: 1 addition & 0 deletions plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './core/src/index'
export * from './telegram/src/index'
32 changes: 32 additions & 0 deletions plugins/telegram/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
},
{
"files": ["./package.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/nx-plugin-checks": "error"
}
}
]
}
11 changes: 11 additions & 0 deletions plugins/telegram/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# telegram

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build telegram` to build the library.

## Running unit tests

Run `nx test telegram` to execute the unit tests via [Jest](https://jestjs.io).
10 changes: 10 additions & 0 deletions plugins/telegram/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable */
export default {
displayName: 'telegram',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/plugins/telegram',
}
10 changes: 10 additions & 0 deletions plugins/telegram/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "telegram",
"version": "0.0.1",
"dependencies": {
"tslib": "^2.3.0"
},
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts"
}
58 changes: 58 additions & 0 deletions plugins/telegram/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "telegram",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "plugins/telegram/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/plugins/telegram",
"main": "plugins/telegram/src/index.ts",
"tsConfig": "plugins/telegram/tsconfig.lib.json",
"assets": [
"plugins/telegram/*.md",
{
"input": "./plugins/telegram/src",
"glob": "**/!(*.ts)",
"output": "./src"
},
{
"input": "./plugins/telegram/src",
"glob": "**/*.d.ts",
"output": "./src"
},
{
"input": "./plugins/telegram",
"glob": "generators.json",
"output": "."
},
{
"input": "./plugins/telegram",
"glob": "executors.json",
"output": "."
}
]
}
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"plugins/telegram/**/*.ts",
"plugins/telegram/package.json"
]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "plugins/telegram/jest.config.ts"
}
}
},
"tags": []
}
1 change: 1 addition & 0 deletions plugins/telegram/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/telegramPlugin'
36 changes: 36 additions & 0 deletions plugins/telegram/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { PluginCredential } from 'server/credentials'

export const pluginName = 'Telegram'

export const pluginCredentials: PluginCredential[] = [
{
name: 'telegram-bot',
serviceType: 'telegram',
credentialType: 'plugin',
initials: 'TBT',
},
]

export const TELEGRAM_EVENTS = {
MESSAGE: 'message',
EDITED_MESSAGE: 'edited_message',
CHANNEL_POST: 'channel_post',
EDITED_CHANNEL_POST: 'edited_channel_post',
INLINE_QUERY: 'inline_query',
CHOSEN_INLINE_RESULT: 'chosen_inline_result',
CALLBACK_QUERY: 'callback_query',
SHIPPING_QUERY: 'shipping_query',
PRE_CHECKOUT_QUERY: 'pre_checkout_query',
POLL: 'poll',
POLL_ANSWER: 'poll_answer',
}

export const TELEGRAM_ACTIONS = {
SEND_MESSAGE: 'sendMessage',
SEND_IMAGE: 'sendImage',
GENERATE_IMAGE: 'generateImage',
}

export const TELEGRAM_KEY = 'telegramClient'

export const TELEGRAM_DEVELOPER_MODE = false
26 changes: 26 additions & 0 deletions plugins/telegram/src/lib/nodes/actions/sendTelegramMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { TelegramClient } from '../../services/telegram'
import { EventPayload } from 'packages/server/plugin/src'
import { TelegramEventPayload } from '../../types'
import { SocketDefinition } from '@magickml/behave-graph'
import { TELEGRAM_KEY } from '../../constants'
import { IEventStore } from 'server/grimoire'

type Inputs = {

Check failure on line 8 in plugins/telegram/src/lib/nodes/actions/sendTelegramMessage.ts

View workflow job for this annotation

GitHub Actions / ESLint

'Inputs' is defined but never used

Check failure on line 8 in plugins/telegram/src/lib/nodes/actions/sendTelegramMessage.ts

View workflow job for this annotation

GitHub Actions / ESLint

'Inputs' is defined but never used
flow: SocketDefinition
content: SocketDefinition
}

type Outputs = {

Check failure on line 13 in plugins/telegram/src/lib/nodes/actions/sendTelegramMessage.ts

View workflow job for this annotation

GitHub Actions / ESLint

'Outputs' is defined but never used

Check failure on line 13 in plugins/telegram/src/lib/nodes/actions/sendTelegramMessage.ts

View workflow job for this annotation

GitHub Actions / ESLint

'Outputs' is defined but never used
flow: SocketDefinition
}

export const sendTelegramMessage = async (
dependencies: { [TELEGRAM_KEY]: TelegramClient; IEventStore: IEventStore },
inputs: { content: string },
event: EventPayload<TelegramEventPayload>
): Promise<void> => {
await dependencies[TELEGRAM_KEY].getClient().sendMessage({
text: inputs.content,
chat_id: event.chat.id,
})
}
78 changes: 78 additions & 0 deletions plugins/telegram/src/lib/nodes/events/telegramMessageEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
Assert,
NodeCategory,
makeEventNodeDefinition,
} from '@magickml/behave-graph'
import { TELEGRAM_EVENTS } from '../../constants'
import { EventPayload } from 'packages/server/plugin/src'

type State = {
onStartEvent?: ((event: EventPayload) => void) | undefined
}

const makeInitialState = (): State => ({
onStartEvent: undefined,
})

export const telegramMessageEvent = makeEventNodeDefinition({
typeName: 'telegram/onMessage',
label: 'On Telegram Message',
category: NodeCategory.Event,
configuration: {
numInputs: {
valueType: 'number',
defaultValue: 3,
},
},
in: {},
out: {
flow: 'flow',
content: 'string',
event: 'object',
},
initialState: makeInitialState(),
init: args => {
const {
write,
commit,
node,
engine,
graph: { getDependency },
} = args
const onStartEvent = (event: EventPayload) => {
write('event', event)
write('content', event.content)

console.log('event', event)
console.log('content', event.content)

commit('flow')

if (!node || !engine) return

engine.onNodeExecutionEnd.emit(node)
}

const telegramEventEmitter = getDependency('Telegram')

telegramEventEmitter?.on('message', onStartEvent)

return {
onStartEvent,
}
},
dispose: ({ state: { onStartEvent }, graph: { getDependency } }) => {
Assert.mustBeTrue(onStartEvent !== undefined)

const telegramEventEmitter = getDependency('Telegram')

if (onStartEvent)
telegramEventEmitter?.removeListener(
TELEGRAM_EVENTS.message,
onStartEvent
)

return {}
},
})

Loading

0 comments on commit c93ef12

Please sign in to comment.