Skip to content

Commit

Permalink
add new script for antigen test (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
hummusonrails authored Feb 13, 2022
1 parent 175c420 commit a45093a
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 17 deletions.
6 changes: 5 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
USERNAME=
PASSWORD=
URL=
URL=
PARENT_TZ=
PARENT_NAME=
CHILDRENS_TZ=
CHILDRENS_NAMES=
35 changes: 35 additions & 0 deletions .github/workflows/declare_antigen.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ DEPENDENCIES
webdrivers

BUNDLED WITH
2.1.4
2.3.4
49 changes: 36 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions bin/declare_antigen
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby

$LOAD_PATH.unshift File.expand_path('..', __FILE__)

require_relative '../declare_antigen'

DeclareAntigen.call
102 changes: 102 additions & 0 deletions declare_antigen.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/declare/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Declare
VERSION = '0.3.0'.freeze
VERSION = '0.4.0'.freeze
end
2 changes: 1 addition & 1 deletion school_declare.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
spec.email = ["[email protected]"]
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'

Expand Down
23 changes: 23 additions & 0 deletions spec/declare_antigen_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a45093a

Please sign in to comment.