forked from dymik739/modular-bot-framework-for-telegram
		
	add new module: translator
This commit is contained in:
		
							parent
							
								
									bd22e8e9b6
								
							
						
					
					
						commit
						763cc4d131
					
				
							
								
								
									
										34
									
								
								modules/translator/index.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								modules/translator/index.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,34 @@
 | 
			
		||||
command = self.MESSAGE['text'].split(" ", 2)
 | 
			
		||||
command_length = len(command)
 | 
			
		||||
 | 
			
		||||
if (command[0] in self.aliases) and (1 <= command_length <= 3):
 | 
			
		||||
    try:
 | 
			
		||||
        import requests
 | 
			
		||||
 | 
			
		||||
        if command_length == 1:
 | 
			
		||||
            chosen_model = "auto-uk"
 | 
			
		||||
        else:
 | 
			
		||||
            chosen_model = command[1]
 | 
			
		||||
 | 
			
		||||
        source, target = chosen_model.split("-")
 | 
			
		||||
 | 
			
		||||
        if command_length == 3:
 | 
			
		||||
            text_to_translate = command[2]
 | 
			
		||||
        else:
 | 
			
		||||
            text_to_translate = self.MESSAGE['reply_to_message']['text']
 | 
			
		||||
        
 | 
			
		||||
        data = {"q": text_to_translate,
 | 
			
		||||
                "source": source,
 | 
			
		||||
                "target": target,
 | 
			
		||||
                "format": "text"}
 | 
			
		||||
 | 
			
		||||
        res = requests.post("http://127.0.0.1:5000/translate", data = data)
 | 
			
		||||
        result = json.loads(res.text)
 | 
			
		||||
 | 
			
		||||
        if source == "auto":
 | 
			
		||||
            self.RESPONSE = f"Результат ({result['detectedLanguage']['language']} - {result['detectedLanguage']['confidence']}%): {result['translatedText']}"
 | 
			
		||||
        else:
 | 
			
		||||
            self.RESPONSE = f"Результат: {result['translatedText']}"
 | 
			
		||||
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        print(f"[translit-decoder] Got exception: {e}")
 | 
			
		||||
							
								
								
									
										7
									
								
								modules/translator/meta.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								modules/translator/meta.json
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
			
		||||
{
 | 
			
		||||
    "version": 1,
 | 
			
		||||
    "index_file": "index.py",
 | 
			
		||||
    "start_on_boot": true,
 | 
			
		||||
    "alias": "translator",
 | 
			
		||||
    "predefine": "predefine.py"
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								modules/translator/predefine.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								modules/translator/predefine.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
			
		||||
self.aliases = ["!translate", "!t"]
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user