Skip to content

Commit

Permalink
use ha service and use openai every 30 min
Browse files Browse the repository at this point in the history
  • Loading branch information
lluisd committed Nov 17, 2024
1 parent bccb4be commit 8eeba6d
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main_twitch-mz-bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ jobs:
envkey_AZURE_OPENAI_ASSISTANT_ID: ${{ secrets.AZURE_OPENAI_ASSISTANT_ID }}
envkey_AZURE_STORAGE_CONNECTION_STRING: ${{ secrets.AZURE_STORAGE_CONNECTION_STRING }}
envkey_AZURE_STORAGE_CONTAINER_NAME: transcriptions
envkey_HA_ENDPOINT: ${{ secrets.HA_ENDPOINT }}
envkey_HA_API_KEY: ${{ secrets.HA_API_KEY }}
- name: Set up Node.js version
uses: actions/setup-node@v4
with:
Expand Down
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const randomLinks = require("./config/randomLinks.json");
const TempsDeFlorsService = require('./services/tempsDeFlors')
const TwitchService = require("./services/twitch");
const ScreenshotService = require("./services/screenshot");
const HAService = require("./services/ha");
const moment = require('moment-timezone')
const EventSub = require('./lib/eventSub')
const handlers = require('./handlers')
Expand All @@ -32,6 +33,7 @@ mongoose.connect(config.database).then(() => {
});

app.get('/transcribe', async function(req, res) {
await HAService.hibernateTranscriberPC()
await handlers.openAI.uploadStreamToOpenai(`#${config.twitch.channels}`, bot)
const response = {
message: 'transcription started',
Expand Down
4 changes: 4 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,9 @@ module.exports = {
blobStorage: {
connectionString: process.env.AZURE_STORAGE_CONNECTION_STRING,
containerName: process.env.AZURE_STORAGE_CONTAINER_NAME
},
ha: {
apiKey: process.env.HA_API_KEY,
endpoint: process.env.HA_ENDPOINT
}
}
7 changes: 6 additions & 1 deletion handlers/openAI.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ class OpenAI {
async askOpenAI (target, text, username, bot) {
const response = await OpenAIService.askAssistant(text, username)
if (response) {
bot.say(target, `@${username} ${response}`)
if (username) {
bot.say(target, `@${username} ${response}`)
} else {
bot.say(target, `/me response`)
}

}
}

Expand Down
6 changes: 5 additions & 1 deletion lib/notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const TelegramBot = require('node-telegram-bot-api')
const config = require('../config')
const cron = require('node-cron')
const handlers = require('../handlers')
const TwitchService = require("../services/twitch");

class Notifier {
constructor (twitchBot) {
Expand Down Expand Up @@ -36,6 +35,11 @@ class Notifier {
await handlers.openAI.createAndUploadToChat(this.target, this.twitchBot)
})

cron.schedule('*/30 * * * *', async () => {
const text = 'como si fueras un usuario del chat de forma aleatoria alguna curiosidad del streamer o de su chat anecdótica o de dato curioso para compartir'
await handlers.openAI.askOpenAI(this.target, text, null, this.twitchBot)
})

return Promise.resolve()
}

Expand Down
34 changes: 34 additions & 0 deletions services/ha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const config = require('../config')

const endpointPrefix = `${config.ha.endpoint}/api/`

async function hibernateTranscriberPC() {
let result = null
const endpoint = endpointPrefix + 'services/button/press'

const body = {
"entity_id": "button.button.hp_z230_hibernate"
}

const options = {
headers: {
'content-type': 'application/json',
'Authorization': `Bearer ${config.ha.apiKey}`
},
method: 'POST',
body: JSON.stringify(body)
}

try {
const response = await fetch(endpoint, options)
result = await response.json()
} catch {
result = null
}

return result
}

module.exports = {
hibernateTranscriberPC
}

0 comments on commit 8eeba6d

Please sign in to comment.