commit bd4b4152916dc44bcbbcae1f29e2ceeffac14ad9 Author: Kris Date: Fri Aug 9 09:53:17 2024 -0400 first commit diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..faba474 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,32 @@ +name: CI + +on: + push: + branches: [ master, develop, main ] + pull_request: + branches: [ master, develop, main ] + +jobs: + container-test-job: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Install docker + run: | + apt-get update && apt-get install --no-install-recommends -y ca-certificates curl gnupg lsb-release sudo + curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg + echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null + apt-get update && apt-get install --no-install-recommends -y docker-ce docker-ce-cli containerd.io + - name: Build docker images + run: docker build -t ${{ github.sha }} . + + - name: Run Docker container + run: docker run -d -p 3000:3000 --name ${{ github.sha }} ${{ github.sha }} + + # - name: Wait for Rails to start + # run: apt-get install --no-install-recommends -y netcat && sh -c 'until nc -z localhost 3000; do sleep 1; done' + + #- name: Run Rails tests + # run: docker exec -t ${{ github.sha }} bin/rails test diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5fbe04e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM ruby:3.3-bullseye +RUN gem install mqtt telegram-bot-ruby +COPY frigate.rb /frigate.rb +CMD ["ruby /frigate.rb"] diff --git a/frigate.rb b/frigate.rb new file mode 100644 index 0000000..f1e688d --- /dev/null +++ b/frigate.rb @@ -0,0 +1,43 @@ +require 'mqtt' +require 'json' +require 'telegram/bot' + +token = ENV['TELEGRAM_TOKEN'] +chat_id = ENV['TELEGRAM_CHAT_ID'] + +mqtt_host = ENV['MQTT_HOST'] +mqtt_port = ENV['MQTT_PORT'].to_i +mqtt_user = ENV['MQTT_USER'] +mqtt_pass = ENV['MQTT_PASS'] + +frigate_url = ENV['FRIGATE_URL'] + + +Telegram::Bot::Client.run(token) do |bot| + MQTT::Client.connect(host: mqtt_host, port: mqtt_port, username: mqtt_user, password: mqtt_pass) do |c| + c.get('frigate/events') do |topic,message| + a = JSON.parse message + if a['type'] == 'new' + formatted_message = "#{a['before']['camera'].capitalize} - #{a['before']['label'].capitalize} was detected." + snapshot = "#{frigate_url}/api/events/#{a['before']['id']}/thumbnail.jpg" + bot.api.send_message(chat_id: chat_id, text: formatted_message) + timeout_reached = 0.1 + until `curl --write-out %{http_code} --silent -I --output /dev/null #{snapshot}` === "200" or timeout_reached > 10 do + sleep 0.1 + timeout_reached = timeout_reached + 0.1 + end + bot.api.send_photo(chat_id: chat_id, photo: snapshot, caption: formatted_message, show_caption_above_media: true, disable_notification: true) + elsif a['type'] == 'end' + clip = "#{frigate_url}/api/events/#{a['before']['id']}/clip.mp4" + timeout_reached = 0.1 + until `curl --write-out %{http_code} --silent -I --output /dev/null #{clip}` === "200" or timeout_reached > 10 do + sleep 0.1 + timeout_reached = timeout_reached + 0.1 + end + bot.api.send_video(chat_id: chat_id, video: clip, caption: formatted_message, show_caption_above_media: true, supports_streaming: true, disable_notification: true) + else + puts "skipped message, not new" + end + end + end +end \ No newline at end of file