Skip to content

Commit 3b98922

Browse files
authored
Merge pull request #48 from Dialogue-Bot/DIAL-35-Setup-socket-for-server
DIAL-35-Setup-socket-for-server
2 parents ef22b2f + 14387b6 commit 3b98922

18 files changed

+224
-49
lines changed

client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"@tiptap/extension-text-align": "^2.2.4",
5555
"@tiptap/extension-text-style": "^2.2.4",
5656
"@tiptap/extension-underline": "^2.2.4",
57+
"@tiptap/pm": "^2.2.4",
5758
"@tiptap/react": "^2.2.4",
5859
"@tiptap/starter-kit": "^2.2.4",
5960
"@xyflow/react": "^12.0.0-next.11",

server/model.nlp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"settings":{"languages":["en"],"forceNER":true,"tag":"nlp","threshold":0.5,"autoLoad":true,"autoSave":true,"modelFileName":"model.nlp","executeActionsBeforeAnswers":false,"calculateSentiment":true},"nluManager":{"settings":{"tag":"nlu-manager"},"locales":["en"],"languageNames":{},"domainManagers":{"en":{"settings":{"locale":"en","trainByDomain":false,"tag":"domain-manager-en","nluByDomain":{"default":{"className":"NeuralNlu","settings":{}}},"useStemDict":true},"stemDict":{"hello":{"intent":"Hello","domain":"default"}},"intentDict":{"Hello":"default"},"sentences":[{"domain":"default","utterance":"hello","intent":"Hello"}],"domains":{"master_domain":{"settings":{"locale":"en","tag":"nlu-en","keepStopwords":true,"nonefeatureValue":1,"nonedeltaMultiplier":1.2,"spellCheck":false,"spellCheckDistance":1,"filterZeros":true,"log":true},"features":{"hello":1},"intents":{"Hello":1},"intentFeatures":{"Hello":{"hello":1}},"featuresToIntent":{"hello":["Hello"]},"neuralNetwork":{"settings":{"locale":"en","tag":"nlu-en","keepStopwords":true,"nonefeatureValue":1,"nonedeltaMultiplier":1.2,"spellCheck":false,"spellCheckDistance":1,"filterZeros":true,"log":true},"features":["hello"],"intents":["Hello"],"perceptrons":[[9.46274471282959,4.736227717439353]]}}}}},"intentDomains":{},"extraSentences":[["en","hello"]]},"ner":{"settings":{"tag":"ner","entityPreffix":"%","entitySuffix":"%"},"rules":{}},"nlgManager":{"settings":{"tag":"nlg-manager"},"responses":{"en":{"Hello":[{"answer":"hi"}]}}},"actionManager":{"settings":{"tag":"action-manager"},"actions":{}},"slotManager":{}}
1+
{"settings":{"languages":["en"],"forceNER":true,"tag":"nlp","threshold":0.5,"autoLoad":true,"autoSave":true,"modelFileName":"model.nlp","executeActionsBeforeAnswers":false,"calculateSentiment":true},"nluManager":{"settings":{"tag":"nlu-manager"},"locales":["en"],"languageNames":{},"domainManagers":{"en":{"settings":{"locale":"en","trainByDomain":false,"tag":"domain-manager-en","nluByDomain":{"default":{"className":"NeuralNlu","settings":{}}},"useStemDict":true},"stemDict":{"for,javascript,syntax,type,typescript,with":{"intent":"typescript","domain":"default"},"-,introduct,typescript,w3school":{"intent":"typescript","domain":"default"},"is,new,stack,the,typescript,what,|":{"intent":"typescript","domain":"default"},"-,typescript,wikipedia":{"intent":"typescript","domain":"default"},"is,typescript,what":{"intent":"typescript","domain":"default"},"-,document,for,javascript,programm,typescript":{"intent":"typescript","domain":"default"},"#program,-,30,in,is,second,typescript,what,youtub,‍":{"intent":"typescript","domain":"default"},"-,a,comprehens,guid,is,kinsta®,typescript,what":{"intent":"typescript","domain":"default"},"-,geeksforgeek,introduct,to,typescript":{"intent":"typescript","domain":"default"},"-,overview,tutorialspoint,typescript":{"intent":"typescript","domain":"default"},"altern,javascript":{"intent":"typescript","domain":"default"},"for,is,typescript,use,what":{"intent":"typescript","domain":"default"},"angular,in,is,typescript,what":{"intent":"typescript","domain":"default"},"in,is,react,typescript,what":{"intent":"typescript","domain":"default"},"exampl,is,typescript,what":{"intent":"typescript","domain":"default"},"in,is,program,typescript,what":{"intent":"typescript","domain":"default"},"in,is,javascript,typescript,what":{"intent":"typescript","domain":"default"},"tutori,typescript":{"intent":"typescript","domain":"default"},"is,languag,typescript,what":{"intent":"typescript","domain":"default"},"and,is,it,typescript,use,what,whi":{"intent":"typescript","domain":"default"},"is,javascript,typescript,vs,what":{"intent":"typescript","domain":"default"},"are,main,of,the,typescript,use,what":{"intent":"typescript","domain":"default"},"a,is,languag,typescript":{"intent":"typescript","domain":"default"}},"intentDict":{"typescript":"default"},"sentences":[{"domain":"default","utterance":"TypeScript: JavaScript With Syntax For Types.","intent":"typescript"},{"domain":"default","utterance":"TypeScript Introduction - W3Schools","intent":"typescript"},{"domain":"default","utterance":"What Is Typescript? | The New Stack","intent":"typescript"},{"domain":"default","utterance":"TypeScript - Wikipedia","intent":"typescript"},{"domain":"default","utterance":"What is TypeScript?","intent":"typescript"},{"domain":"default","utterance":"Documentation - TypeScript for JavaScript Programmers","intent":"typescript"},{"domain":"default","utterance":"What is TypeScript!? ‍ (in 30 Seconds) #programming ... - YouTube","intent":"typescript"},{"domain":"default","utterance":"What is TypeScript? A Comprehensive Guide - Kinsta®","intent":"typescript"},{"domain":"default","utterance":"Introduction to TypeScript - GeeksforGeeks","intent":"typescript"},{"domain":"default","utterance":"TypeScript - Overview - Tutorialspoint","intent":"typescript"},{"domain":"default","utterance":"JavaScript alternative","intent":"typescript"},{"domain":"default","utterance":"What is typescript used for","intent":"typescript"},{"domain":"default","utterance":"What is TypeScript in Angular","intent":"typescript"},{"domain":"default","utterance":"What is TypeScript in React","intent":"typescript"},{"domain":"default","utterance":"What is typescript example","intent":"typescript"},{"domain":"default","utterance":"What is typescript in programming","intent":"typescript"},{"domain":"default","utterance":"What is typescript in javascript","intent":"typescript"},{"domain":"default","utterance":"TypeScript Tutorial","intent":"typescript"},{"domain":"default","utterance":"What is TypeScript language","intent":"typescript"},{"domain":"default","utterance":"What is TypeScript and why use it?","intent":"typescript"},{"domain":"default","utterance":"What is TypeScript vs JavaScript?","intent":"typescript"},{"domain":"default","utterance":"What are the main uses of TypeScript?","intent":"typescript"},{"domain":"default","utterance":"Is TypeScript is a language?","intent":"typescript"}],"domains":{"master_domain":{"settings":{"locale":"en","tag":"nlu-en","keepStopwords":true,"nonefeatureValue":1,"nonedeltaMultiplier":1.2,"spellCheck":false,"spellCheckDistance":1,"filterZeros":true,"log":true},"features":{"30":1,"typescript":1,"javascript":1,"with":1,"syntax":1,"for":1,"type":1,"introduct":1,"-":1,"w3school":1,"what":1,"is":1,"|":1,"the":1,"new":1,"stack":1,"wikipedia":1,"document":1,"programm":1,"‍":1,"in":1,"second":1,"#program":1,"youtub":1,"a":1,"comprehens":1,"guid":1,"kinsta®":1,"to":1,"geeksforgeek":1,"overview":1,"tutorialspoint":1,"altern":1,"use":1,"angular":1,"react":1,"exampl":1,"program":1,"tutori":1,"languag":1,"and":1,"whi":1,"it":1,"vs":1,"are":1,"main":1,"of":1},"intents":{"typescript":1},"intentFeatures":{"typescript":{"30":1,"typescript":1,"javascript":1,"with":1,"syntax":1,"for":1,"type":1,"introduct":1,"-":1,"w3school":1,"what":1,"is":1,"|":1,"the":1,"new":1,"stack":1,"wikipedia":1,"document":1,"programm":1,"‍":1,"in":1,"second":1,"#program":1,"youtub":1,"a":1,"comprehens":1,"guid":1,"kinsta®":1,"to":1,"geeksforgeek":1,"overview":1,"tutorialspoint":1,"altern":1,"use":1,"angular":1,"react":1,"exampl":1,"program":1,"tutori":1,"languag":1,"and":1,"whi":1,"it":1,"vs":1,"are":1,"main":1,"of":1}},"featuresToIntent":{"30":["typescript"],"typescript":["typescript"],"javascript":["typescript"],"with":["typescript"],"syntax":["typescript"],"for":["typescript"],"type":["typescript"],"introduct":["typescript"],"-":["typescript"],"w3school":["typescript"],"what":["typescript"],"is":["typescript"],"|":["typescript"],"the":["typescript"],"new":["typescript"],"stack":["typescript"],"wikipedia":["typescript"],"document":["typescript"],"programm":["typescript"],"‍":["typescript"],"in":["typescript"],"second":["typescript"],"#program":["typescript"],"youtub":["typescript"],"a":["typescript"],"comprehens":["typescript"],"guid":["typescript"],"kinsta®":["typescript"],"to":["typescript"],"geeksforgeek":["typescript"],"overview":["typescript"],"tutorialspoint":["typescript"],"altern":["typescript"],"use":["typescript"],"angular":["typescript"],"react":["typescript"],"exampl":["typescript"],"program":["typescript"],"tutori":["typescript"],"languag":["typescript"],"and":["typescript"],"whi":["typescript"],"it":["typescript"],"vs":["typescript"],"are":["typescript"],"main":["typescript"],"of":["typescript"]},"neuralNetwork":{"settings":{"locale":"en","tag":"nlu-en","keepStopwords":true,"nonefeatureValue":1,"nonedeltaMultiplier":1.2,"spellCheck":false,"spellCheckDistance":1,"filterZeros":true,"log":true},"features":["typescript","javascript","with","syntax","for","type","introduct","-","w3school","what","is","|","the","new","stack","wikipedia","document","programm","30","‍","in","second","#program","youtub","a","comprehens","guid","kinsta®","to","geeksforgeek","overview","tutorialspoint","altern","use","angular","react","exampl","program","tutori","languag","and","whi","it","vs","are","main","of"],"intents":["typescript"],"perceptrons":[[5.864444255828857,1.3793507814407349,0.2651691138744354,0.2651691138744354,-0.000009804296496440656,0.2651691138744354,0.5669187903404236,1.2452651262283325,0.3928775191307068,0.5992182493209839,1.2922813892364502,-0.00007762110908515751,0.32274720072746277,-0.00007762110908515751,-0.00007762110908515751,0.9856331944465637,-0.24609969556331635,-0.24609969556331635,-0.019618354737758636,-0.019618354737758636,-0.9037643074989319,-0.019618354737758636,-0.019618354737758636,-0.019618354737758636,0.5133158564567566,-0.5026148557662964,-0.5026148557662964,-0.5026148557662964,0.1747691035270691,0.1747691035270691,0.46149536967277527,0.46149536967277527,6.579104423522949,0.29652366042137146,1.090837836265564,1.0781846046447754,0.27076593041419983,1.049940586090088,2.2173736095428467,0.3632107377052307,-0.00157655100338161,-0.00157655100338161,-0.00157655100338161,-1.1090513467788696,0.32372617721557617,0.32372617721557617,0.32372617721557617,6.226292117274352]]}}}}},"intentDomains":{},"extraSentences":[["en","TypeScript: JavaScript With Syntax For Types."],["en","TypeScript Introduction - W3Schools"],["en","What Is Typescript? | The New Stack"],["en","TypeScript - Wikipedia"],["en","What is TypeScript?"],["en","Documentation - TypeScript for JavaScript Programmers"],["en","What is TypeScript!? ‍ (in 30 Seconds) #programming ... - YouTube"],["en","What is TypeScript? A Comprehensive Guide - Kinsta®"],["en","Introduction to TypeScript - GeeksforGeeks"],["en","TypeScript - Overview - Tutorialspoint"],["en","JavaScript alternative"],["en","What is typescript used for"],["en","What is TypeScript in Angular"],["en","What is TypeScript in React"],["en","What is typescript example"],["en","What is typescript in programming"],["en","What is typescript in javascript"],["en","TypeScript Tutorial"],["en","What is TypeScript language"],["en","What is TypeScript and why use it?"],["en","What is TypeScript vs JavaScript?"],["en","What are the main uses of TypeScript?"],["en","Is TypeScript is a language?"]]},"ner":{"settings":{"tag":"ner","entityPreffix":"%","entitySuffix":"%"},"rules":{}},"nlgManager":{"settings":{"tag":"nlg-manager"},"responses":{"en":{"typescript":[{"answer":"TypeScript is JavaScript with syntax for types. TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at ..."},{"answer":"TypeScript is a syntactic superset of JavaScript which adds static typing. This basically means that TypeScript adds syntax on top of JavaScript, allowing ..."},{"answer":"TypeScript is a strongly typed, object-oriented, compiled programming language that builds on JavaScript. It is a superset of the JavaScript ..."},{"answer":"TypeScript is a free and open-source high-level programming language developed by Microsoft that adds static typing with optional type annotations to ..."},{"answer":"TypeScript adds type to the JavaScript and helps you avoid potential bugs that occur at runtime. TypeScript also implements the future features of JavaScript."},{"answer":"TypeScript stands in an unusual relationship to JavaScript. TypeScript offers all of JavaScript's features, and an additional layer on top of these: TypeScript ..."},{"answer":"Share your videos with friends, family, and the world."},{"answer":"TypeScript is a superset of JavaScript that adds optional static typing and advanced features to JavaScript. It was developed by Microsoft and ..."},{"answer":"TypeScript is an open-source, object-oriented programming language developed and maintained by Microsoft Corporation."},{"answer":"TypeScript is a strongly typed, object oriented, compiled language. It was designed by Anders Hejlsberg (designer of C#) at Microsoft. TypeScript is both a ..."},{"answer":"TypeScript allows specifying the types of data being passed around within the code, and has the ability to report errors when the types don't match. For example, TypeScript will report an error when passing a string into a function that expects a number. JavaScript will not. TypeScript uses compile time type checking."},{"answer":"TypeScript is an object-oriented programming language developed by Microsoft Corporation, whereas JavaScript is the programming language for the web. TypeScript comes with many functionalities like code editors and IDEs, whereas JavaScript has limited built-in tooling functionality."},{"answer":"JavaScript is a common query among developers. TypeScript offers many benefits over JavaScript, but the primary one is the static typings it adds to the language. This addition gives us more certainty over the code we write and helps reduce the number of bugs and issues that are created."},{"answer":"TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale."}]}}},"actionManager":{"settings":{"tag":"action-manager"},"actions":{}},"slotManager":{}}

server/package-lock.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"react": "^18.2.0",
6363
"reflect-metadata": "^0.1.13",
6464
"slugify": "^1.6.6",
65+
"socket.io": "^4.7.5",
6566
"swagger-jsdoc": "^6.2.1",
6667
"swagger-ui-express": "^4.5.0",
6768
"typedi": "^0.10.0",

server/src/app.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ import { LocaleService } from './i18n/ctx'
1818
import { getPreferredLocale } from './i18n/get-preferred-locale'
1919
import { loadAllLocales } from './i18n/i18n-util.sync'
2020

21+
// socket.io
22+
import { createServer } from 'http'
23+
import { Server } from 'socket.io'
24+
import { SocketController } from './controllers/socket.controller'
25+
2126
Container.set(LOCALE_KEY, new LocaleService('en'))
2227

2328
export class App {
2429
public app: express.Application
2530
public env: string
2631
public port: string | number
32+
public static io: Server
33+
2734

2835
constructor(routes: Routes[]) {
2936
this.app = express()
@@ -37,12 +44,26 @@ export class App {
3744
}
3845

3946
public listen() {
40-
this.app.listen(this.port, () => {
41-
logger.info(`=================================`)
42-
logger.info(`======= ENV: ${this.env} =======`)
43-
logger.info(`🚀 App listening on the port ${this.port}`)
44-
logger.info(`=================================`)
45-
})
47+
try {
48+
const socketController = new SocketController();
49+
const server = createServer(this.app);
50+
App.io = new Server(server);
51+
52+
App.io.on('connection', (socket) => {
53+
socketController.handleJoinRoom(socket);
54+
socketController.handleSocketEvents(socket);
55+
});
56+
57+
server.listen(this.port, () => {
58+
logger.info(`=================================`)
59+
logger.info(`======= ENV: ${this.env} =======`)
60+
logger.info(`🚀 App listening on the port ${this.port}`)
61+
logger.info(`=================================`)
62+
})
63+
64+
} catch (error) {
65+
console.error('Error initializing server:', error);
66+
}
4667
}
4768

4869
public getServer() {

server/src/channels/base.channel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ export class BaseChannel {
5959
initConversationId(userId: string) {
6060
return this.contactId + '-' + userId
6161
}
62-
}
62+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { AUTO_REGISTER_WEBHOOK } from "@/config";
2+
import { ChannelInfo } from "@/interfaces/channels.interface";
3+
import { LineChannel } from "./line.channel";
4+
import { MessengerChannel } from "./messenger.channel";
5+
import { WebChannel } from "./web.channel";
6+
7+
export class Creator {
8+
static createChannel(channel: ChannelInfo) {
9+
try {
10+
const { id, contactId, contactName, channelType, credentials } = channel;
11+
12+
switch (channelType) {
13+
case 'MSG':
14+
return new MessengerChannel(
15+
id,
16+
contactId,
17+
contactName,
18+
channelType,
19+
credentials,
20+
)
21+
case 'LIN':
22+
const linChannel = new LineChannel(
23+
id,
24+
contactId,
25+
contactName,
26+
channelType,
27+
credentials,
28+
)
29+
if (AUTO_REGISTER_WEBHOOK == '1') {
30+
linChannel.registerWebhook();
31+
}
32+
return linChannel
33+
case 'WEB':
34+
return new WebChannel(
35+
id,
36+
contactId,
37+
contactName,
38+
channelType,
39+
credentials,
40+
)
41+
default:
42+
throw new Error(`Does not support channel type ${channel.channelType}`);
43+
break;
44+
}
45+
} catch (err) {
46+
throw err;
47+
}
48+
}
49+
}

server/src/channels/line.channel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class LineChannel extends BaseChannel {
9090
data: null,
9191
})
9292
}
93-
} catch (e) {}
93+
} catch (e) { }
9494
}
9595

9696
public async sendMessageToUser({ userId, text }) {

server/src/channels/web.channel.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { logger } from '@/utils/logger'
2+
import { Request, Response } from 'express'
3+
import { App } from '../app'
4+
import { BaseChannel } from './base.channel'
5+
6+
export class WebChannel extends BaseChannel {
7+
credentials: string
8+
9+
constructor(
10+
id: string,
11+
contactId: string,
12+
contactName: string,
13+
channelType: string,
14+
credentials: string,
15+
) {
16+
super(id, contactId, contactName, channelType)
17+
this.channelType = channelType;
18+
this.credentials = credentials;
19+
}
20+
21+
async prepareMessage(req: Request, res: Response) { }
22+
23+
public async sendMessageToUser({ userId, text }) {
24+
try {
25+
if (App.io) {
26+
App.io.to(userId).emit('message', { userId, message: text });
27+
}
28+
} catch (e) {
29+
logger.info(
30+
`[WEB] Bot send message to User failed - Error: ${e.message}`,
31+
)
32+
}
33+
}
34+
}

server/src/config/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ export const {
3333
MAIL_USER,
3434
MAIL_PASS,
3535
API_KEY,
36+
AUTO_REGISTER_WEBHOOK
3637
} = process.env

0 commit comments

Comments
 (0)