Skip to content

Commit 66c36f5

Browse files
committed
Now users can add chatbot to their website.
1 parent af14a74 commit 66c36f5

File tree

12 files changed

+165
-21
lines changed

12 files changed

+165
-21
lines changed

app/src/App.css

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
@import url('https://fonts.googleapis.com/css2?family=Inter&family=Noto+Sans+JP:wght@400;500&family=Noto+Sans+Kannada&display=swap');
22

3-
43
* {
54
margin: 0;
65
padding: 0;
@@ -698,7 +697,7 @@
698697
}
699698

700699
.message {
701-
border-radius: 10px;
700+
border-radius: 5px;
702701
padding: 8px;
703702
margin: 4px;
704703
max-width: 85%;
@@ -846,15 +845,6 @@
846845
width: 60%;
847846
}
848847

849-
.message-container {
850-
margin-bottom: 10px;
851-
}
852-
853-
.message {
854-
padding: 8px;
855-
border-radius: 5px;
856-
}
857-
858848
.chat-buttons > button {
859849
background-color: transparent;
860850
border: none;

app/src/components/Chat.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,34 @@ export default function Chat({ botId }) {
1818
}, [botId]);
1919

2020
const generateCode = () => {
21-
// Generate code to add chatbot to user's webpage
22-
// Copy code to clipboard (use document.execCommand('copy'))
23-
// Alert the user that the code has been copied
24-
const generatedCode = `<Your chatbot code>`;
25-
// Copy code to clipboard (implementation required)
26-
// Alert message (implementation required)
21+
const generatedCode = `
22+
<div id="semantiq-chatbot"></div>
23+
<script data-bot-id="${botId}" defer="defer" src="http://localhost:8080/static/main.ea9881ca.js"></script>
24+
<script>
25+
var link = document.createElement("link");
26+
link.href = "http://localhost:8080/static/main.ba187d75.css";
27+
link.rel = "stylesheet";
28+
document.head.appendChild(link);
29+
</script>
30+
`;
31+
32+
copyToClipboard(generatedCode);
33+
34+
// Alert message to indicate code has been copied
35+
alert('Code successfully copied to clipboard! To integrate the chatbox, insert the script elements just before the closing </body> tag, and place the div with the ID "seamntiq-chatbot" wherever you want the chatbox to appear.');
2736
};
37+
38+
function copyToClipboard(text) {
39+
const el = document.createElement('textarea');
40+
el.value = text;
41+
el.setAttribute('readonly', '');
42+
el.style.position = 'absolute';
43+
el.style.left = '-9999px';
44+
document.body.appendChild(el);
45+
el.select();
46+
document.execCommand('copy');
47+
document.body.removeChild(el);
48+
}
2849

2950
const votePositive = () => {
3051
sendVote(1);
@@ -114,7 +135,7 @@ export default function Chat({ botId }) {
114135
</div>
115136
))}
116137
</div>
117-
138+
118139
<form onSubmit={sendMessage} className="message-input">
119140
<input
120141
className='Chat-input'

server/src/main/java/com/semantiq/server/controller/BotDataController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
@RestController
1919
@RequestMapping("/api/botdata")
20+
@CrossOrigin(origins = "*")
2021
public class BotDataController {
2122
private final BotDataService botDataService;
2223
private final ChatBotService chatBotService;

server/src/main/java/com/semantiq/server/controller/ChatBotController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
@RestController
1919
@RequestMapping("/api/bots")
20-
20+
@CrossOrigin(origins = "*")
2121
public class ChatBotController {
2222
private final ChatBotService chatbotService;
2323
private final UserService userService;

server/src/main/java/com/semantiq/server/controller/EmailController.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
@Controller
1111
@RequestMapping("/api/email")
12-
1312
public class EmailController {
1413
private final EmailService emailService;
1514
private final UserService userService;
@@ -20,7 +19,6 @@ public EmailController(EmailService emailService, UserService userService) {
2019
this.userService = userService;
2120
}
2221

23-
2422
@PostMapping("/send")
2523
public ResponseEntity <?> sendEmail(@RequestParam int userId, @RequestParam String message) {
2624

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.semantiq.server.controller;
2+
3+
import org.springframework.core.io.Resource;
4+
import org.springframework.core.io.UrlResource;
5+
import org.springframework.http.HttpHeaders;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.web.bind.annotation.CrossOrigin;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
13+
import java.net.MalformedURLException;
14+
import java.nio.file.Path;
15+
import java.nio.file.Paths;
16+
17+
@Controller
18+
@RequestMapping("/static")
19+
@CrossOrigin(origins = "*")
20+
public class FileController {
21+
22+
private static final String FILES_DIRECTORY = "src/main/resources/Files/static/";
23+
24+
@GetMapping("/{file}")
25+
public ResponseEntity<Resource> serveChatbotBundle(@PathVariable String file) {
26+
return serveFile(file);
27+
}
28+
29+
private ResponseEntity<Resource> serveFile(String fileName) {
30+
Resource file = loadFileAsResource(fileName);
31+
32+
if (file == null) {
33+
return ResponseEntity.notFound().build();
34+
}
35+
36+
return ResponseEntity.ok()
37+
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")
38+
.body(file);
39+
}
40+
41+
private Resource loadFileAsResource(String fileName) {
42+
try {
43+
Path filePath = Paths.get(FILES_DIRECTORY).resolve(fileName).normalize();
44+
Resource resource = new UrlResource(filePath.toUri());
45+
46+
if (resource.exists()) {
47+
return resource;
48+
} else {
49+
return null;
50+
}
51+
} catch (MalformedURLException ex) {
52+
return null;
53+
}
54+
}
55+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.semantiq.server.service.configuration;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.web.servlet.config.annotation.CorsRegistry;
5+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
6+
7+
@Configuration
8+
public class WebConfig implements WebMvcConfigurer {
9+
@Override
10+
public void addCorsMappings(CorsRegistry registry) {
11+
// CORS configuration for /api/bots/**
12+
registry.addMapping("/api/bots/")
13+
.allowedOriginPatterns("*") // Use allowedOriginPatterns instead of allowedOrigins
14+
.allowedMethods("GET", "POST", "PUT", "DELETE")
15+
// Add more configurations if needed
16+
.allowCredentials(true);
17+
18+
// CORS configuration for /static/**
19+
registry.addMapping("/static/")
20+
.allowedOriginPatterns("*") // Use allowedOriginPatterns instead of allowedOrigins
21+
.allowedMethods("GET", "POST", "PUT", "DELETE")
22+
// Add more configurations if needed
23+
.allowCredentials(true);
24+
25+
// CORS configuration for /static/**
26+
registry.addMapping("/api/botdata/")
27+
.allowedOriginPatterns("*") // Use allowedOriginPatterns instead of allowedOrigins
28+
.allowedMethods("GET", "POST", "PUT", "DELETE")
29+
// Add more configurations if needed
30+
.allowCredentials(true);
31+
}
32+
}
33+

server/src/main/resources/Files/static/main.ba187d75.css

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

server/src/main/resources/Files/static/main.ba187d75.css.map

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

server/src/main/resources/Files/static/main.ea9881ca.js

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

0 commit comments

Comments
 (0)