Skip to content

Commit

Permalink
debug for new code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
sytpb committed Apr 24, 2023
1 parent 14d4b0b commit 5418c83
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 17 deletions.
20 changes: 12 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import { config } from "dotenv";
import bodyParser from 'body-parser';
import bodyParserXml from 'body-parser-xml';

import getAIChat from "./openai.js";
import { Message } from "./message.js";
import { initAccessToken } from "./config.js";

//import getAIChat from "./openai.js";
//import Message from "./message.js";
import { initAccessToken } from "./comm/config.js";
import conversation from "./route/conversation.js";

config();
bodyParserXml(bodyParser);

const app = express();
const PORT = process.env.PORT;
const message = new Message();
//const message = new Message();

/*message.log();*/

Expand All @@ -26,12 +28,14 @@ app.get('/healthz', function (req, res, next) {
res.status(200).end();
});

/*receive server url setting*/
/*receive server url setting
app.get('/message', function (req, res, next) {
message.urlSetting(req, res);
});
});*/

/*passive message response*/

app.use('/message',conversation);
/*passive message response
app.post('/message', function (req, res, next) {
message.getMsgObj(req).then(result => {
Expand All @@ -57,7 +61,7 @@ app.post('/message', function (req, res, next) {
})
})
});
});*/

/*init access_token*/
initAccessToken();
Expand Down
19 changes: 19 additions & 0 deletions chat/chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict"

const types = {text: "TEXT", image: "IMAGE", voice: "AUDIO"};

export default class Chat {

constructor(name) {
this.#type = types[name];
}

type() {
return this.#type;
}

process(req, res) {

}

}
31 changes: 31 additions & 0 deletions chat/text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict"


import Chat from "./chat.js";
import debug from "../comm/debug.js";
import getAIChat from "../openai.js";

export default class TextChat extends Chat{

constructor(name) {
super(name);
}

process(xml) {

const question = xml.content;

getAIChat(question).then(result => {
const content = result?.data?.choices[0]?.message?.content;
if(!!content) {
const answer = content;

debug.log(answer);
console.log(answer);
//message.sendMsg(answer, toUser);
}

})
}

}
File renamed without changes.
20 changes: 14 additions & 6 deletions debug.js → comm/debug.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
"use strict"

Object.defineProperty(global, '__stack', {
get: function(){
var orig = Error.prepareStackTrace;
get: function() {

const orig = Error.prepareStackTrace;
Error.prepareStackTrace = function(_, stack){ return stack; };
var err = new Error;

const err = new Error;
Error.captureStackTrace(err, arguments.callee.caller);
var stack = err.stack;

const stack = err.stack;
Error.prepareStackTrace = orig;

return stack;
}
});

Object.defineProperty(global, '__line', {
get: function(){
get: function() {

return __stack[1].getLineNumber();
}
});

Object.defineProperty(global, '__file', {
get: function(){
get: function() {

let last = __stack[1].getFileName().lastIndexOf('/');
return __stack[1].getFileName().slice(last);
}
Expand Down
19 changes: 16 additions & 3 deletions message.js → comm/message.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//import fs from "fs";
import { config } from "dotenv";
import request from "request";

import crypto from "crypto";
import { parseString } from "xml2js";
import { getAccessToken } from "./config.js";
import { xmlmsg1,xmlmsg2 } from "./templates.js";
import { xmlmsg1,xmlmsg2 } from "../templates.js";

config();

Expand All @@ -14,7 +13,7 @@ const base = {
};


export class Message {
export default class Message {
constructor() {

this.secret = process.env.SECRET;
Expand Down Expand Up @@ -132,6 +131,20 @@ export class Message {
return msg.xml.Content[0];
}

async decode(data) {

const xmlString = this.decrypt(data.xml.Encrypt[0]);
const result = await new Promise((resolve, reject) =>
parseString(xmlString,(err, res) => {
if (err)
reject(err);
else
resolve(res);
}));

return result.xml;
}

async getMsgObj(req) {

const xmlString = this.decrypt(req.body.xml.Encrypt[0]);
Expand Down
45 changes: 45 additions & 0 deletions handler/conversation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

import debug from "../comm/debug.js";
import Message from "../comm/message.js";

export default class Conversation {

constructor() {
}

urlconfig(req, res) {
const message = new Message();
message.urlSetting(req, res);
}

async process(body, res) {

let chat = null;
const data = body;
const message = new Message();
const xml = await message.decode(data);
const msgType = xml.MsgType;
debug.log(msgType);

if(msgType === "text") {
chat = new textChat(msgType,xml);
}
else if (msgType === "image") {
chat = new imageChat(msgType,xml);
}
else if (msgType === "voice") {
chat = new audioChat(msgType,xml);
}

if(!!chat) {
const answer = chat.process(xml);

//comm.response(answer,res)
return;
}

//debug.log("Not support msgtype []");
}


}
19 changes: 19 additions & 0 deletions route/conversation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

import Conversation from "../handler/conversation.js";

import express from "express";
const router = express.Router();

router.use('/', function(req, res, next) {
let method = req.method;
if(method == 'GET') {
}
else if(method == 'POST') {
console.log(`------------------------ROUTER MSG [CONVERSATION]-------------------------`);
const conversation = new Conversation();
conversation.process(req.body,'',res);
}
});

export default router;

File renamed without changes.

0 comments on commit 5418c83

Please sign in to comment.