diff --git a/.env.sample b/.env.sample index 1bdadb3..0030961 100644 --- a/.env.sample +++ b/.env.sample @@ -1,3 +1,7 @@ USERNAME= PASSWORD= -URL= \ No newline at end of file +URL= +PARENT_TZ= +PARENT_NAME= +CHILDRENS_TZ= +CHILDRENS_NAMES= \ No newline at end of file diff --git a/.github/workflows/declare_antigen.yml b/.github/workflows/declare_antigen.yml new file mode 100644 index 0000000..28c7673 --- /dev/null +++ b/.github/workflows/declare_antigen.yml @@ -0,0 +1,35 @@ +name: Antigen Test Declaration Submission +on: + schedule: + - cron: "0 6 * * 0,3" + workflow_dispatch: +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-ruby@v1 + with: + ruby-version: 3.1.0 + - name: Bundle Setup + run: | + gem update --system 3.1.4 -N + gem install --no-document bundler + bundle config path ${{ github.workspace }}/vendor/bundle + - name: Bundle Install + run: | + bundle install --jobs 4 --retry 3 + - name: Set Credentials + run: | + touch $HOME/work/school-health-declaration-automation/school-health-declaration-automation/.env + printf -- "---\nURL: ${URL}\nPARENT_NAME: ${PARENT_NAME}\nPARENT_TZ: ${PARENT_TZ}\nCHILDRENS_TZ: ${CHILDRENS_TZ}\nCHILDRENS_NAMES: ${CHILDRENS_NAMES}\n" > $HOME/work/school-health-declaration-automation/school-health-declaration-automation/.env + env: + URL: ${{secrets.URL}} + PARENT_NAME: ${{secrets.PARENT_NAME}} + PARENT_TZ: ${{secrets.PARENT_TZ}} + CHILDRENS_TZ: ${{secrets.CHILDRENS_TZ}} + CHILDRENS_NAMES: ${{secrets.CHILDRENS_NAMES}} + - name: Submit Antigen Test Health Form + run: | + bundle exec ruby bin/declare_antigen \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index f786253..ed55c4c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -55,4 +55,4 @@ DEPENDENCIES webdrivers BUNDLED WITH - 2.1.4 + 2.3.4 diff --git a/README.md b/README.md index 9b7770b..ba0d1ea 100644 --- a/README.md +++ b/README.md @@ -18,40 +18,63 @@ In your `Gemfile`: ### Using The Code +You can use this script to either send: + +* Daily health declaration (*No longer required*) +* Twice weekly antigen test declaration + +#### Daily health declaration automation + To use the automation script locally on your machine: * Clone the repository * Run `bundle install` -* Update the values in `.env.sample` and rename it to `.env`. (See (Environment Variables)[#environment-variables] for a description of the values) +* Update the following values in `.env.sample` and rename it to `.env`. (See (Environment Variables)[#environment-variables] for a description of the values) + * USERNAME + * PASSWORD + * URL * Run `bundle exec ruby bin/declare` * The script will output `Sent from successfully` or report what went wrong at the conclusion of its execution +#### Twice weekly antigen test declaration + +To use the antigen test declaration automation script locally on your machine: + +* Clone the repository +* Run `bundle install` +* Update the following values in `.env.sample` and rename it to `.env`. (See (Environment Variables)[#environment-variables] for a description of the values) + * URL + * PARENT_TZ + * PARENT_NAME + * CHILDRENS_TZ + * CHILDRENS_NAMES +* Run `bundle exec ruby bin/declare_antigen` +* The script will output `Sent from successfully` or report what went wrong at the conclusion of its execution + ### Environment Variables -You need to provide three environment variables for the automation script: +You need to provide three environment variables for the daily automation script: * `USERNAME`: Your username you obtained from the Ministry of Education * `PASSWORD`: Your password you obtained from the Ministry of Education * `URL`: The unique school website entry point provided by tik-tak, i.e. (`https://my_school.tik-tak.net/enter_tofes_briut`) +You need to provide the following environment variables for the twice weekly antigen test declaration form script: + + * `URL`: The URL for the antigen test declaration, i.e. (`https://apps.education.gov.il/NmmNetAnt/Antigen`) + * `PARENT_TZ`: A parent's teudat zehut number + * `PARENT_NAME`: A parent's name **in Hebrew** + * `CHILDRENS_TZ`: The teudat zehut number or numbers of the child/children. **If more than one child, separate with a single "," and NO SPACE.** + * `CHILDRENS_NAMES`: The name of names of the child/children. **If more than one child, separate with a cingle "," and NO SPACE.** + ### Headless Browsing -The script is set to run in `headless` mode as it navigates to the school website and fills out the form, which means you won't see it complete the process. If you wish, you can set `headless` to `false` and watch it from your computer as it happens instead. To do so open up `declare.rb` and on line 14 change the code to: +The script is set to run in `headless` mode as it navigates to the school website and fills out the form, which means you won't see it complete the process. If you wish, you can set `headless` to `false` and watch it from your computer as it happens instead. To do so open up `declare.rb` and on line 14 or `declare_antigen.rb` on line 45 and change the code to: ```ruby browser = Watir::Browser.new :chrome, headless: false ``` -### Send SMS Update - -This script also provides the ability to send an SMS update after completion. To enable this feature you will need to have a [Vonage Account](https://dashboard.nexmo.com) and provide the following additional values in your `.env` file: - -* `SEND_SMS=true`: A boolean set to true to enable the SMS feature -* `VONAGE_API_KEY`: Set this to your Vonage API key -* `VONAGE_API_SECRET`: Set this to your Vonage API secret -* `VONAGE_NUMBER`: Set this to the phone number you provisioned from Vonage for SMS delivery -* `TO_NUMBER`: Set this to the receipient number for the SMS to be delivered to - ## Contributing Contributions to the code are always welcome. You can either raise an issue to be discussed or submit a pull request directly. We try to follow the [GitHub Flow](https://guides.github.com/introduction/flow/) when proposing new features. diff --git a/bin/declare_antigen b/bin/declare_antigen new file mode 100755 index 0000000..bfaaa18 --- /dev/null +++ b/bin/declare_antigen @@ -0,0 +1,7 @@ +#!/usr/bin/env ruby + +$LOAD_PATH.unshift File.expand_path('..', __FILE__) + +require_relative '../declare_antigen' + +DeclareAntigen.call \ No newline at end of file diff --git a/declare_antigen.rb b/declare_antigen.rb new file mode 100644 index 0000000..753a6e4 --- /dev/null +++ b/declare_antigen.rb @@ -0,0 +1,102 @@ +# frozen_string_literal: true + +require 'dotenv' +Dotenv.load! + +require 'nokogiri' +require 'webdrivers/chromedriver' +require 'watir' +require 'vonage' + +# Declare antigen test health declaration for schools. +# +# @example +# +# DeclareAntigen.new.call +class DeclareAntigen + attr_reader :url, :parent_tz, :parent_name, :childrens_tz, :childrens_names + + def self.call + new.call + end + + def initialize(url: ENV['URL'], parent_tz: ENV['PARENT_TZ'], parent_name: ENV['PARENT_NAME'], childrens_tz: ENV['CHILDRENS_TZ'], childrens_names: ENV['CHILDRENS_NAMES']) + @url = url + @parent_tz = parent_tz + @parent_name = parent_name + @childrens_tz = childrens_tz.include?(',') ? childrens_tz.split(',') : childrens_tz + @childrens_names = childrens_names.include?(',') ? childrens_names.split(',') : childrens_names + + validate! + end + + def validate! + raise ArgumentError, 'Missing URL environment variable' unless ENV['URL'] + raise ArgumentError, 'Missing PARENT_NAME environment variable' unless ENV['PARENT_NAME'] + raise ArgumentError, 'Missing PARENT_TZ environment variable' unless ENV['PARENT_TZ'] + raise ArgumentError, 'Missing CHILDRENS_TZ environment variable' unless ENV['CHILDRENS_TZ'] + raise ArgumentError, 'Missing CHILDRENS_NAMES environment variable' unless ENV['CHILDRENS_NAMES'] + end + + def call + go_to_form + end + + def go_to_form + browser = Watir::Browser.new :chrome, headless: true + browser.goto(@url) + + fill_out_form(browser) + end + + def fill_out_form(page) + item_count = 0 + + if @childrens_tz.is_a?(Array) + @childrens_tz.count.times do + complete_individual_form(item_count, page) + submit_form(page) + check_for_errors(page) + item_count += 1 + page.refresh + end + else + complete_individual_form(page) + submit_form(page) + check_for_errors + item_count += 1 + end + + validate_success(item_count) + end + + def complete_individual_form(count = nil, page) # rubocop:disable Metrics/AbcSize + page.text_field(id: /txtMisparZehutHore/).set(@parent_tz) + page.text_field(id: /txtShemPratiHore/).set(@parent_name) + + if @childrens_tz.is_a?(Array) + page.text_field(class: /mispar-zehut-yeled form-control/).set(@childrens_tz[count]) + page.text_field(class: /shem-prati-yeled form-control/).set(@childrens_names[count]) + page.radio(name: /rdoResult_0/).click + else + page.text_field(class: /mispar-zehut-yeled form-control/).set(@childrens_tz) + page.text_field(class: /shem-prati-yeled form-control/).set(@childrens_names) + page.radio(name: /rdoResult_0/).click + end + end + + def submit_form(page) + until page.div(id: /bot/).exists? do sleep 3 end + page.iframe.click + page.button(id: /cmdSend/).click + end + + def validate_success(count) + message = "Form submitted #{count} times successfully for all children!" + puts message + end + + def check_for_errors(page) + raise 'Something went wrong' if page.div(class: /error-line/).present? + end +end diff --git a/lib/declare/version.rb b/lib/declare/version.rb index 9452ae4..7d56503 100644 --- a/lib/declare/version.rb +++ b/lib/declare/version.rb @@ -1,3 +1,3 @@ module Declare - VERSION = '0.3.0'.freeze + VERSION = '0.4.0'.freeze end \ No newline at end of file diff --git a/school_declare.gemspec b/school_declare.gemspec index d6d2127..8a80505 100644 --- a/school_declare.gemspec +++ b/school_declare.gemspec @@ -11,7 +11,7 @@ Gem::Specification.new do |spec| spec.email = ["ben.greenberg@hey.com"] spec.homepage = "https://github.com/bencgreenberg/school-health-declaration-automation" spec.summary = "Automate the sending of health declaration forms for Israeli schools." - spec.description = "A gem to help send the daily health forms for Israeli schools using the tik-tak platform." + spec.description = "A gem to help send the daily health forms for Israeli schools." spec.license = "MIT" spec.executables << 'declare' diff --git a/spec/declare_antigen_spec.rb b/spec/declare_antigen_spec.rb new file mode 100644 index 0000000..e9567a1 --- /dev/null +++ b/spec/declare_antigen_spec.rb @@ -0,0 +1,23 @@ +require_relative '../declare_antigen' + +RSpec.describe DeclareAntigen do + let(:declare_instance) do + described_class.new( + url: 'https://sample.url/school-form', + parent_name: 'פלוני אלמוני', + parent_tz: '000000000', + childrens_tz: '000000000', + childrens_names: 'ילד האלוף' + ) + end + + context 'with correct parameters' do + it 'creates an instance with the right attributes' do + expect(declare_instance.url).to eql('https://sample.url/school-form') + expect(declare_instance.parent_name).to eql('פלוני אלמוני') + expect(declare_instance.parent_tz).to eql('000000000') + expect(declare_instance.childrens_names).to eql('ילד האלוף') + expect(declare_instance.childrens_tz).to eql('000000000') + end + end +end \ No newline at end of file