방언으로 대답 - LINE 번역 Bot 만들어 보았다! -AzureBotService MicrosoftTranslatorText-
소개
자신의 학습의 기록, 망비록으로서의 내용이 됩니다.
Qiita 첫 투고 때문에, 보기 어려운 부분, 미비 등이 있다고 생각합니다.
내용 : AzureBotService를 사용하여 LINE 번역 BOT을 만듭니다.
언어에 따라 일본 각지의 방언을 사용하여 대답한다.

완성 동영상
했던 일
1. node.js에서 LINE Bot 만들기
우선, 이 내용을 한번에 시험해, 기본적인 LINE Bot의 만드는 방법의 학습을 했습니다.
매우 정중하게 만들어져 알기 쉬웠습니다.
1시간에 LINE BOT 만들기 핸즈온 (자료 + 리포트) in Node 학원제 2017 #nodefest
2. AzureBotService를 사용하여 LINE 번역 Bot 만들기
향후, Azure에서의 개발도 진행해 가고 싶은 일도 있어, 상기 node.js, now를 사용한 개발 부분을 AzureBotService를 이용해 가기로 했습니다.

아래 사이트에서 AzureBotService와 LINE을 쉽게 연동할 수 있는 채널이 생겼음을 알고 이 내용에 따라 작성해 보았습니다.
축 LINE 채널 추가! Azure Bot Service에서 번역 채팅봇 만들기 (1) Azure Bot Service에서 LINE 연결 편
축 LINE 채널 추가! Azure Bot Service에서 번역 채팅봇 만들기 (2)Cognitive Services Translator Text API 편
매우 이해하기 쉽고, 이것에 따라 진행했습니다.
LINE의 응답 부분의 편집은 App Service Editor 화면의 bot.js에서 할 수 있습니다.
bot.js
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { ActivityHandler } = require('botbuilder');
const rp = require('request-promise');
class EchoBot extends ActivityHandler {
constructor() {
super();
// See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
/*
this.onMessage(async (context, next) => {
await context.sendActivity(`You said '${ context.activity.text }'`);
// By calling next() you ensure that the next BotHandler is run.
await next();
});
*/
this.onMessage(async (context, next) => {
console.log('got a message.');
// Translate
const subscriptionKey = '<SUBSCRIPTION_KEY>';
const options = {
method: 'POST',
baseUrl: 'https://api.cognitive.microsofttranslator.com/',
url: 'translate',
qs: {
'api-version': '3.0',
'to': 'ja'
},
headers: {
'Ocp-Apim-Subscription-Key': subscriptionKey,
'Content-type': 'application/json'
},
body: [{
'text': context.activity.text
}],
json: true,
};
const responseBody = await rp(options);
const translated = responseBody[0].translations[0].text;
const responseMessage = `英語で「${translated}」って言ったっスね!`
+ `(検出言語: ${responseBody[0].detectedLanguage.language})`;
await context.sendActivity(responseMessage);
await next();
});
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; ++cnt) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
await context.sendActivity('Hello and welcome!');
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}
}
module.exports.EchoBot = EchoBot;

3. 입력한 언어로 번역에 방언을 나누고 응답하는 Bot 만들기
입력 한 언어에 따라 Switch 문으로 조건 분기
이탈리아 나중에 「안녕하세요」유타얀! 라는 바람에 문말을 일본 각지의 방언을 섞어 대답한다.
bot.js
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { ActivityHandler } = require('botbuilder');
const rp = require('request-promise');
class EchoBot extends ActivityHandler {
constructor() {
super();
// See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
this.onMessage(async (context, next) => {
console.log('got a message.');
// Translate
const subscriptionKey = 'e2d153f610de4a8086ba83b7360c4c5b';
const options = {
method: 'POST',
baseUrl: 'https://api.cognitive.microsofttranslator.com/',
url: 'translate',
qs: {
'api-version': '3.0',
'to': 'ja'
},
headers: {
'Ocp-Apim-Subscription-Key': subscriptionKey,
'Content-type': 'application/json'
},
body: [{
'text': context.activity.text
}],
json: true,
};
const responseBody = await rp(options);
const translated = responseBody[0].translations[0].text;
/////////////////////////////////ここから変更//////////////////////////////////////////
const la = responseBody[0].detectedLanguage.language;
let lang = null;
let end_sent = 'って言ったっスね!';
switch(la) {
case 'en':
lang = '英語';
break
case 'fr':
lang = 'フランス語';
end_sent = 'って言ったっショ!';
break
case 'it':
lang = 'イタリア語';
end_sent = 'ゆーたやん!';
break
case 'es':
lang = 'スペイン語';
end_sent = 'っつったべ!';
break
case 'ja':
lang = '日本語';
end_sent = 'ちゆうちょっと!';
break
case 'ko':
lang = '韓国語';
end_sent = 'っゆったぺよ!';
break
case 'zh-Hans':
lang = '中国語';
end_sent = 'って言ったアルネ!';
break
case 'de':
lang = 'ドイツ語';
end_sent = 'っちゅうたじゃろ!';
break
case 'ru':
lang = 'ロシア語';
end_sent = 'って言うたがいね!';
break
default:
lang = 'ホゲホゲ語';
end_sent = 'っていったべさ!';
break
}
const responseMessage =lang+`で「${translated}」`+ end_sent
+ `(検出言語: ${responseBody[0].detectedLanguage.language})`;
/////////////////////////////////ここまで変更//////////////////////////////////////////
await context.sendActivity(responseMessage);
await next();
});
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; ++cnt) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
await context.sendActivity('Hello and welcome!');
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}
}
module.exports.EchoBot = EchoBot;
마지막으로
LINE 채널을 만들면 매우 쉽게 LINE 번역 Bot을 만들 수있었습니다.
response 부분의 편집에 의해 여러가지 바리에이션을 만들 수 있습니다.
앞으로 LUIS 을 얽힌 개발에도 도전해 나가고 싶습니다.
Reference
이 문제에 관하여(방언으로 대답 - LINE 번역 Bot 만들어 보았다! -AzureBotService MicrosoftTranslatorText-), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/dashgo5go/items/fe3ddd1d565f059e2ea2텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)