from googletrans import Translator
import json
from ibm_watson import AssistantV2
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
translator = Translator()
from googletrans import Translator
translator = Translator()
class VoiceBot:
    def __init__(self,main_api,service_url,eng_engine,hindi_engine,beng_engine,skill_date):
        self.api = main_api
        self.service_url = service_url
        self.code = 0     #0 stands for English which is bydefault set
        self.user_language = 'en'
        self.maper = {
            'Bengali':beng_engine,
            'English':eng_engine,
            'Hindi':hindi_engine,
            'skill_date': skill_date,
                     }
    
    def process(self,user_msg):
        detector = translator.detect(user_msg)
        self.user_language = str(detector.lang)
        if(self.user_language=='en'):
            date = self.maper['skill_date']
            assistant_id = self.maper['English']
            self.code = 0 # 0 stands for english
        elif(self.user_language=='hi'):
            date = self.maper['skill_date']
            assistant_id = self.maper['Hindi']
            self.code = 1 # 1 stands for Hindi
        else:
            date = self.maper['skill_date']
            assistant_id = self.maper['Bengali']
            self.code = 2 # 2 stands for Bengali

        try:
            authenticator = IAMAuthenticator(self.api)
            assistant = AssistantV2(
            version= date,
            authenticator=authenticator
            )
            assistant.set_service_url(self.service_url)
            assistant.set_disable_ssl_verification(True)
            session_details = assistant.create_session(assistant_id)
            session_id = (session_details.get_result())
            session_id = session_id['session_id']
            text = translator.translate(user_msg, dest='en', src=self.user_language)
            text = text.text
            res = assistant.message(
            assistant_id=assistant_id,
            session_id=session_id,
            input={
            'message_type': 'text',
            'text': text}
            ).get_result()
            response = res['output']['generic'][0]['text']
            return (response,self.code)
        except Exception as e:
            print(e)
            return None

from googletrans import Translator
import json
from ibm_watson import AssistantV2
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
translator = Translator()
from googletrans import Translator
translator = Translator()
class VoiceBot:
    def __init__(self,main_api,service_url,eng_engine,hindi_engine,beng_engine,skill_date):
        self.api = main_api
        self.service_url = service_url
        self.code = 0     #0 stands for English which is bydefault set
        self.user_language = 'en'
        self.maper = {
            'Bengali':beng_engine,
            'English':eng_engine,
            'Hindi':hindi_engine,
            'skill_date': skill_date,
                     }
    
    def process(self,user_msg):
        detector = translator.detect(user_msg)
        self.user_language = str(detector.lang)
        if(self.user_language=='en'):
            date = self.maper['skill_date']
            assistant_id = self.maper['English']
            self.code = 0 # 0 stands for english
        elif(self.user_language=='hi'):
            date = self.maper['skill_date']
            assistant_id = self.maper['Hindi']
            self.code = 1 # 1 stands for Hindi
        else:
            date = self.maper['skill_date']
            assistant_id = self.maper['Bengali']
            self.code = 2 # 2 stands for Bengali

        try:
            authenticator = IAMAuthenticator(self.api)
            assistant = AssistantV2(
            version= date,
            authenticator=authenticator
            )
            assistant.set_service_url(self.service_url)
            assistant.set_disable_ssl_verification(True)
            session_details = assistant.create_session(assistant_id)
            session_id = (session_details.get_result())
            session_id = session_id['session_id']
            text = translator.translate(user_msg, dest='en', src=self.user_language)
            text = text.text
            res = assistant.message(
            assistant_id=assistant_id,
            session_id=session_id,
            input={
            'message_type': 'text',
            'text': text}
            ).get_result()
            response = res['output']['generic'][0]['text']
            return (response,self.code)
        except Exception as e:
            print(e)
            return None

#0 stands for English which is bydefault set # 0 stands for english # 1 stands for Hindi # 2 stands for Bengali