-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdreamcatcher.py
133 lines (107 loc) · 4.47 KB
/
dreamcatcher.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""Телеграмм бот. Ловец сновидений. Минимальная рабочая версия."""
import logging
import os
import speech_recognition as sr
import telegram
from pydub import AudioSegment
from serpapi import GoogleSearch
from telegram import Update
from telegram.ext import (CallbackContext, CommandHandler, Filters,
MessageHandler, Updater)
from exceptions import SendMessageError
from settings import GOOGLE_TOKEN, TELEGRAM_TOKEN
logging.basicConfig(
format='%(asctime)s | %(name)s | %(levelname)s | '
'%(funcName)s | %(message)s',
level=logging.DEBUG,
)
def send_message(bot, chat_id, message):
"""Отправляет сообщения в чат."""
try:
bot.send_message(chat_id, message)
except telegram.error.TelegramError as e:
logging.exception(f'Ошибка отправки сообщения: `{e}`')
raise SendMessageError(e) from e
def send_photo(bot, chat_id, image, caption):
"""Отправляет сообщения в чат."""
try:
bot.send_photo(
chat_id,
image,
caption=f'Сновидение поймано:\n {caption}',
)
except telegram.error.TelegramError as e:
logging.exception(f'Ошибка отправки сообщения: `{e}`')
raise SendMessageError(e) from e
def get_image(query):
"""Получаем изображение из текста сновидения."""
search = GoogleSearch(
{
"q": query,
"localization": "google_domain",
"tbm": "isch",
"ijn": "0",
"device": "mobile",
"api_key": GOOGLE_TOKEN
}
)
result = search.get_dict()
lighthouse_photo = result.get('images_results')[0].get('original')
return lighthouse_photo
def write_text(update: Update, context: CallbackContext):
"""Обработчик введенного вручную текста."""
lighthouse = get_image(update.message.text)
chat = update.effective_chat
send_photo(context.bot, chat.id, lighthouse, caption=update.message.text)
def say_voice(update: Update, context: CallbackContext):
"""
Обработчик голосового ввода.
Преобразует его текст. Ищет маячок сновидения и прикрепляет его к нему.
Использует:
-`google serpapi` для поиска, и сервис google для перевода голоса.
"""
chat = update.effective_chat
file_id = update.message.voice.file_id
audio_tg = context.bot.get_file(file_id=file_id)
audio_tg.download('captured.ogg')
ogg_version = AudioSegment.from_ogg('captured.ogg')
ogg_version.export('output.wav', format="wav")
r = sr.Recognizer()
with sr.AudioFile('output.wav') as source:
audio = r.record(source)
text_dream = ''
try:
text_dream = r.recognize_google(audio, language='ru-RU')
logging.info(f'Голос преобразован: `{text_dream}`')
except sr.UnknownValueError:
logging.error(
"Google Speech Recognition could not understand audio"
)
except sr.RequestError as e:
logging.error(
"Could not request results from Google Speech Recognition "
"service; {0}".format(e))
lighthouse = get_image(text_dream)
send_photo(context.bot, chat.id, lighthouse, caption=text_dream)
def wake_up(update: Update, context: CallbackContext):
"""Обработчик кнопки '/start'."""
chat = update.effective_chat
msg = ('Ловец снов активирован ...'
'\nв момент пробуждения, запиши голосовое. Опиши что видел.')
send_message(context.bot, chat.id, msg)
def main(): # noqa: C901
"""Основная логика работы бота."""
updater = Updater(token=TELEGRAM_TOKEN)
updater.dispatcher.add_handler(CommandHandler('start', wake_up))
updater.dispatcher.add_handler(MessageHandler(Filters.voice, say_voice))
updater.dispatcher.add_handler(MessageHandler(Filters.text, write_text))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
print('\nStarting https://t.me/my_dreamcatcher_bot'
'\n(Quit the bot with CONTROL-C.)')
try:
main()
except KeyboardInterrupt:
print('\nShutdown dreamcatcher ...')
os._exit(0)