local hyper = {"cmd", "alt", "ctrl"}
hs.hotkey.bind(hyper, "Y", function()
local url = "https://api.openai.com/v1/completions"
local api_key = "..."
local headers = {
authorization="Bearer " .. api_key,
["content-type"]="application/json",
accept="application/json"
}
-- hs.eventtap.keyStroke({"cmd"}, "c")
local message = hs.pasteboard.readString()
local data = {
prompt=message,
model="text-davinci-003",
max_tokens=32,
temperature=0
}
hs.notify.new({title="OpenAI query", informativeText=message}):send()
hs.http.asyncPost(url, hs.json.encode(data), headers, function(status, body, headers)
local response = hs.json.decode(body)
local answer = response["choices"][1]["text"]
print(body)
hs.notify.new({title="OpenAI response", informativeText=answer}):send()
hs.pasteboard.writeObjects(answer)
-- hs.eventtap.keyStroke({"cmd"}, "v")
end)
end)
Other practical example why ternary is bad: Many code-coverage solutions break on ternary because they don't correctly see that one of the branches was missed in tests.