This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/miku/plugins/tts.lua
2016-08-24 22:30:41 +02:00

50 lines
1.5 KiB
Lua

local tts = {}
function tts:init(config)
tts.triggers = {
"^/tts (.+)$",
"^/tts(%w+) (.+)$"
}
tts.doc = [[*
]]..config.cmd_pat..[[tts* _<Text>_: Sendet Sprachnachricht mit diesem Text
*]]..config.cmd_pat..[[tts*_<Sprachcode>_ _<Text>_: Sendet Sprachnachricht mit diesem Text in der gewählten Sprache
]]
end
tts.command = 'tts <Text>'
function tts:action(msg, config, matches)
if matches[2] == nil then
text = matches[1]
lang = 'de'
else
text = matches[2]
lang = matches[1]
end
local text = string.gsub(text, "%+", "plus")
local text = string.gsub(text, "%s+", "+")
local text = string.gsub(text, "ä", "ae")
local text = string.gsub(text, "Ä", "Ae")
local text = string.gsub(text, "ö", "oe")
local text = string.gsub(text, "Ö", "Oe")
local text = string.gsub(text, "ü", "ue")
local text = string.gsub(text, "Ü", "Ue")
local text = string.gsub(text, "ß", "ss")
local text = string.gsub(text, "%&", "und")
if string.len(text) > 160 then utilities.send_reply(msg, 'Text zu lang!') return end
local text = string.gsub(text, " ", "+")
local url = 'http://tts.baidu.com/text2audio?lan='..lang..'&ie=UTF-8&text='..text
utilities.send_typing(msg.chat.id, 'record_audio')
local file = download_to_file(url, text..'.mp3')
if not file then
utilities.send_reply(msg, 'Ein Fehler ist aufgetreten, besuche die URL eventuell <a href="'..url..'">direkt</a>?\n', 'HTML')
return
end
utilities.send_voice(msg.chat.id, file, msg.message_id)
end
return tts