File size: 1,698 Bytes
81d9a9d
b580aa3
81d9a9d
 
 
 
7957d73
81d9a9d
 
 
 
 
 
 
 
ab10969
 
 
 
 
 
81d9a9d
 
ab10969
 
 
 
81d9a9d
ab10969
 
81d9a9d
 
 
 
ab10969
81d9a9d
 
 
 
 
ab10969
81d9a9d
 
 
 
 
 
ab10969
81d9a9d
 
 
 
ab10969
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
from modules import TTS, log_handler
from requests.exceptions import ReadTimeout
import sys
import io
import time
from config import BOT_SYSTEM_INSTRUCTION, SYSTEM_REFRESH, BARK_SMALL_MODELS, SPEAKER

# TTS Init
au_model = TTS('./assets/audio/', BARK_SMALL_MODELS)
print('TTS initialized successfully 👍')

# Log declaration
log = log_handler('./log/log.txt')

def send_message(chat_id, message):
    print(message)

def send_voice(chat_id, voice_path):
    print("Sending voice message...")

def handle_text(message):
    chat_id = message.chat.id
    if message.text != '/start':
        response = chat.query_bot(f'{message.text}', BOT_SYSTEM_INSTRUCTION, SYSTEM_REFRESH)
        send_message(chat_id, response)
        log.log_write(f'Date:{message.date} Chat_id: {chat_id} User: {message.text} Bot: {response}')
    else:
        send_message(chat_id, 'Hello!')
        log.log_write(f'Date:{message.date} Chat_id: {chat_id} User: {message.text} Bot: Hello!')

def send_audio(text, chat_id):
    try:
        au_model.to_audio(text, SPEAKER)
        send_voice(chat_id, './assets/audio/tts.ogg')
        os.remove("./assets/audio/tts.ogg")
        return
    except Exception as err:
        print('Error in send_audio function:\n'+str(err))
        log.log_write('Something went wrong with send_audio:\n'+str(err))
        send_message(chat_id, text) 
        return
        
if __name__ == "__main__":
    try:
        while True:
            try:
                polling()
            except ReadTimeout:
                print("A timeout occurred. Trying again...")
    except Exception as err:
        print('Error in bot.polling:\n' + str(err))
        log.fn_by_err(str(err))