티스토리 뷰
[다국어 번역 챗봇]
슬랙 채널에서 각기 다른 언어를 사용하는 유저들이 하나의 언어로 대화할 수 있도록
폰트 사이즈 12 고딕
폰트 사이즈 12 고딕
폰트 사이즈 12 고딕
폰트 사이즈 12 고딕
폰트 사이즈 12 고딕
import requests
from slacker import Slacker
import time
from slackclient import SlackClient
def translate(content, target_lang):
encoding = "utf-8"
url = 'https://translate.googleapis.com/translate_a/single'
params = {'client': 'gtx', 'sl': 'auto', 'tl': target_lang, 'dt': 't', 'q': content, 'ie': encoding}
res = requests.get(url=url, params=params)
res.raise_for_status()
text = ""
for caption in list(res.json())[0]:
text += caption[0]
return text
def notification(chanel, massage, token, bot):
slack = Slacker(token)
slack.chat.post_message('#'+chanel, massage, bot)
def is_head(msg):
if len(msg) > 0 and msg[0] == '#':
return True
return False
def is_lang(msg):
langs = ['en', 'ko', 'ja', 'zh-TW', 'pt', 'fr', 'vi', 'es', 'ar', 'de']
if msg[1:] in langs:
return True
return False
def change_tl(cmd):
return cmd[1:]
def set_params(msg, tl):
body = translate(msg + " [{}]".format(tl), tl)
if is_head(msg):
tl = change_tl(msg) if is_lang(msg) else tl
body = 'changed [{}]'.format(tl) if is_lang(msg) else '[{}] is not found!'.format(msg[1:])
return body, tl
def response(token, chanel, bot):
sc = SlackClient(token)
if sc.rtm_connect():
tl = 'en'
while True:
recive_data = sc.rtm_read()
if len(recive_data):
keys = list(recive_data[0].keys())
if 'type' in keys and 'text' in keys and 'user' in keys:
print(recive_data[0])
body, tl = set_params(recive_data[0]['text'], tl)
notification(chanel, body, token, bot)
time.sleep(1)
else:
print("connection failed")
slack_token = '토큰'
response(slack_token, "dev", 'translate bot')
'python lecture > basic' 카테고리의 다른 글
[edu] 함수 매개변수, 리턴값 타입 지정 (0) | 2018.11.20 |
---|---|
[edu] 프로파일링(소요시간) (0) | 2018.11.19 |
[edu] google translate rest api(구글 번역 rest) (0) | 2018.11.13 |
[edu] 순열, 조합 (0) | 2018.11.13 |
[edu] 파이썬 시작하기 - 이메일 보내기 (0) | 2018.10.31 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 면접답변
- 장고 카톡 자동응답
- 모바일 테마 적용
- gitignore
- GIT
- 파이썬 프로그래밍
- 파이썬 강좌
- 파이썬
- virtualenv
- django
- 이미지 비교
- 플러스친구 자동응답
- 면접정답
- PuTTYGen
- 장고
- 장고 플러스친구 자동응답
- 엑셀 비교
- 문서 비교
- admin.py
- 문과 코딩
- 모바일 스킨 적용
- Tistory
- 파이썬 독학
- 파이썬 입문
- chatbot
- django chatbot
- wsgi
- Python
- pycrypto
- gitlab
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
글 보관함