Last active
November 17, 2020 15:36
-
-
Save Romakita/64bb9d5046dd4c9dd93ff72847fed005 to your computer and use it in GitHub Desktop.
Oracle Bot with Ts.ED
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {useDecorators} from "@tsed/core"; | |
import {registerBotComponent} from "../registries/BotComponentsRegistry.ts"; | |
export function BotComponent(): ClassDecorator { | |
return useDecorators(registerBotComponent); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {GlobalProviders, Provider, TypedProvidersRegistry} from "@tsed/di"; | |
export const PROVIDER_TYPE_BOT_COMPONENTS = "botComponents"; | |
export const botComponentsRegistry: TypedProvidersRegistry = GlobalProviders.createRegistry(PROVIDER_TYPE_BOT_COMPONENTS, Provider, { | |
injectable: true | |
}); | |
export const registerBotComponent = GlobalProviders.createRegisterFn(PROVIDER_TYPE_BOT_COMPONENTS); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const OracleBot = require('@oracle/bots-node-sdk'); | |
const app = express(); | |
OracleBot.init(app); | |
// implement custom component api | |
OracleBot.Middleware.customComponent(app, { | |
baseUrl: '/components', | |
cwd: __dirname, | |
register: [ | |
'./path/to/a/component', | |
'./path/to/other/components', | |
'./path/to/a/directory', | |
] | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from "@tsed/di"; | |
import {BotComponent} from "../decorators/botComponent"; | |
import {OracleBotClient} from "../services/OracleBotClient" | |
@BotComponent() | |
export class MessageComponent implements Lib.IComponent { | |
@Inject() | |
private client: OracleBotClient; | |
public metadata(): Lib.IComponentMetadata { | |
return { | |
name: 'cy2.message.component', | |
supportedActions: ['chatContinue', 'chatFinished'] | |
} | |
} | |
public invoke(conversation: Lib.Conversation, done: () => void): void { | |
this.sendMessage(); | |
conversation.transition(); | |
done(); | |
} | |
sendMessage(){ | |
this.client.hello(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable, ProviderScope, ProviderType} from "@tsed/di"; | |
import {PlatformApplication} from "@tsed/common"; | |
import {messageModelUtil} from "@oracle/bots-node-sdk/util"; | |
import {OracleBotClient} from "./services/OracleBotClient"; | |
import {PROVIDER_TYPE_BOT_COMPONENTS} from "./registries/BotComponentsRegistry"; | |
@Injectable() | |
export class OrableBotModule { | |
webhook: WebhookClient; | |
bot:any = OracleBot; | |
@Inject() | |
private app: PlatformApplication; | |
@Inject() | |
private injector: InjectorService; | |
@Inject() | |
private client: OracleBotClient | |
@Constant("orableBot.webhook", {}) | |
oracleBotWebhook: any; | |
$beforeRoutesInit() { // or $afterInit | |
this.injector.logger.info("OrableBotModule init..."); | |
this.client.bot.init(this.app.raw, { | |
logger: this.injector.logger | |
}); | |
this.client.bot.Middleware.customComponent(this.app.raw, { | |
baseUrl: '/components', | |
cwd: __dirname, | |
register: this.getBotComponents() | |
}); | |
const {WebhookClient} = this.bot.Middleware; | |
this.client.webhook = new WebhookClient(oracleBotWebhook); | |
} | |
protected getBotComponents() { | |
returnn [...this.injector.getProviders(PROVIDER_TYPE_BOT_COMPONENTS)].map((provider) => { | |
return this.injector.get(provider.token) | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from "@tsed/di"; | |
import {WebhookClient} from "@oracle/bots-node-sdk/middleware"; | |
const OracleBot = require('@oracle/bots-node-sdk'); | |
@Injectable() | |
export class OracleBotClient { | |
webhook: WebhookClient; | |
bot: any = OracleBot; | |
hello() { | |
console.log('hello'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Configuration} "@tsed/di"; | |
import "@tsed/platform-express"; | |
import "./oracle-bot/OracleBotModule"; // discover new module | |
const rootDir = __dirname; | |
@Configuration({ | |
orableBot: { | |
webhook: { | |
channel: { | |
url: process.env.ODA_URL, | |
secret: process.env.ODA_SECRET | |
} | |
} | |
}, | |
componentsScan: [ | |
`${rootDir}/oracle-bot/components/**/*.ts | |
] | |
}) | |
export class Server { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment