은승우

Update app.js

Showing 1 changed file with 15 additions and 4 deletions
...@@ -82,9 +82,20 @@ async function handleEvent(event) { ...@@ -82,9 +82,20 @@ async function handleEvent(event) {
82 } 82 }
83 else if (event.type == 'message'&& event.message.type == 'image') { 83 else if (event.type == 'message'&& event.message.type == 'image') {
84 return new Promise(async(resolve,reject)=>{ 84 return new Promise(async(resolve,reject)=>{
85 - fs.writeFileSync('./photo/image.png',event.message.image) 85 +
86 + client.getMessageContent(event.message.id)
87 + .then((stream) => {
88 + stream.on('data', (chunk) => {
89 + const fs = require('fs');
90 + const file = fs.createWriteStream('./photo/image.jpeg');
91 + for(let i=0; i<= 1e6; i++) {
92 + file.write(chunk);
93 + }
94 + file.end();
95 + });
96 + });
86 var cheerio = require('cheerio'); 97 var cheerio = require('cheerio');
87 - var file=fs.readFileSync('./photo/image.png'); 98 +
88 var uriBase = 'https://koreacentral.api.cognitive.microsoft.com/vision/v2.1/ocr'; 99 var uriBase = 'https://koreacentral.api.cognitive.microsoft.com/vision/v2.1/ocr';
89 var options = { 100 var options = {
90 uri: uriBase, 101 uri: uriBase,
...@@ -94,10 +105,10 @@ async function handleEvent(event) { ...@@ -94,10 +105,10 @@ async function handleEvent(event) {
94 language: 'en' 105 language: 'en'
95 }, 106 },
96 headers: { 107 headers: {
97 - 'Content-Type': 'application/octet-stream', 108 + 'Content-Type': 'application/json',
98 'Ocp-Apim-Subscription-Key': '979dc5d63344438fa4701c62feebb7dc' 109 'Ocp-Apim-Subscription-Key': '979dc5d63344438fa4701c62feebb7dc'
99 }, 110 },
100 - body: file 111 + body: fs.readFileSync('./photo/image.jpeg')
101 }; 112 };
102 113
103 request.post(options, function (error, response, body) { 114 request.post(options, function (error, response, body) {
......