Bot

class project.Telegram_Chatbot.bot.Form

Bases: aiogram.dispatcher.filters.state.StatesGroup

Note

State machine: This bot works as a state machine. At each message, the bot will be in one state. Depending on which one, the bot will react accordingly. Languaje (lang) is set on every function.

CLD = <State 'Form:CLD'>
IHD = <State 'Form:IHD'>
age = <State 'Form:age'>
asma = <State 'Form:asma'>
cold = <State 'Form:cold'>
cough = <State 'Form:cough'>
delete = <State 'Form:delete'>
deleting = <State 'Form:deleting'>
diabetes = <State 'Form:diabetes'>
diarrhea = <State 'Form:diarrhea'>
dry_cough = <State 'Form:dry_cough'>
fatigue = <State 'Form:fatigue'>
fever = <State 'Form:fever'>
gender = <State 'Form:gender'>
has_corona = <State 'Form:has_corona'>
hypertension = <State 'Form:hypertension'>
location = <State 'Form:location'>
menu = <State 'Form:menu'>
muscular = <State 'Form:muscular'>
others = <State 'Form:others'>
pneumonia = <State 'Form:pneumonia'>
res_difficult = <State 'Form:res_difficult'>
smell = <State 'Form:smell'>
smoker = <State 'Form:smoker'>
sore_throat = <State 'Form:sore_throat'>
start = <State 'Form:start'>
upload_Data = <State 'Form:upload_Data'>
username = <State 'Form:username'>
vaccine = <State 'Form:vaccine'>
async project.Telegram_Chatbot.bot.about(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

menu

--Input message

“About”

--Output state

menu

async project.Telegram_Chatbot.bot.add_my_data(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

menu

--Input message

“Add data”

--Output state

username

async project.Telegram_Chatbot.bot.cancel_handler(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

‘*’

--Input command

/cancel

--Output state

start

async project.Telegram_Chatbot.bot.cmd_start(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)

Starting point.

State

Message

Command

None

start

‘*’ (any)

Cancel

deleting

No

delete

CANCEL

menu

NOT IN:
  • About

  • Delete data

  • Add data

  • Exit

--Output state

menu

async project.Telegram_Chatbot.bot.delete_data(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
Performs a GET with user’s id. Returns all entries from that id.

State

Message

deleting

Yes

menu

  • No

  • Delete data

--Output state

delete

async project.Telegram_Chatbot.bot.deleting_data(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)

Perfoms a DELETE on the entry seleected by username variable. –Input state delete –Input message NOT IN: ‘Add data’ –Output state deleting

async project.Telegram_Chatbot.bot.exit(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

menu

--Input message

“Exit”

--Output state

start

project.Telegram_Chatbot.bot.main()
async project.Telegram_Chatbot.bot.process_age(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

age

--Input message

digit

--Output state

gender

Stores Input message as [‘age’] in the metadata.

async project.Telegram_Chatbot.bot.process_age_invalid(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

age

--Input message

NOT IN: digit

--Output state

age

Philter so age is digit only.

async project.Telegram_Chatbot.bot.process_chest_pain(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

IHD

--Input message

IN: ‘Yes’, ‘No’

--Output state

cough

Stores Input message as [‘symptoms’][‘IHD’] in the metadata.

async project.Telegram_Chatbot.bot.process_cough(message: aiogram.types.voice.Voice, state: aiogram.dispatcher.storage.FSMContext)
--Input state

cough

--Input message

IN: voice message

--Output state

others

Processes cough sample in two steps:

  • Checks for duration. If it is not comprissed between 1second and 7 second an error message will be displayed.

  • Analyzes the cough using analyze_cough. Returns veredict:

Veredict

None |Audio sample not recognized as cough

False | Covid negative

True | Covid positive

Note

The cough sample is stored in a temporary folder in the Heroku server. Since the audio is downloaded and processed during a single request this causes no trouble. The audio is deleted from this temporary folder after being stored in GridFS anyway.

  • Stores veredict as [‘audio_file’][‘covid_positive’] in the metadata.

async project.Telegram_Chatbot.bot.process_cough_invalid(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

cough

--Input message

NOT IN: voice message

--Output state

cough

Philter so only a voice message is sent.

async project.Telegram_Chatbot.bot.process_dry_cough(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

dry_cough

--Input message

IN: ‘Yes’, ‘No’

--Output state

smoker

Stores Input message as [‘symptoms’][‘dry cough’] in the metadata.

async project.Telegram_Chatbot.bot.process_gender(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

gender

--Input message

IN: ‘Male’, ‘Female’, ‘Other’

--Output state

location

Stores Input message as [‘gender’] in the metadata.

async project.Telegram_Chatbot.bot.process_gender_invalid(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

gender

--Input message

NOT IN: ‘Male’, ‘Female’, ‘Other’

--Output state

gender

Philter so gender is a correct input.

async project.Telegram_Chatbot.bot.process_has_corona(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

has_corona

--Input message

IN: “Currently positive”, “Had covid in the past”, “Never been diagnosed”

--Output state

vaccine

Stores Input message as [‘diagnosis’] in the metadata.

async project.Telegram_Chatbot.bot.process_has_corona_invalid(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

has_corona

--Input message

NOT IN: “Currently positive”, “Had covid in the past”, “Never been diagnosed”

--Output state

has_corona

Philter so diagnosis is a correct input.

async project.Telegram_Chatbot.bot.process_headache(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

sore_throat

--Input message

IN: ‘Yes’, ‘No’

--Output state

fever

Stores Input message as [‘symptoms’][‘sore_throat’] in the metadata.

async project.Telegram_Chatbot.bot.process_location(message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

location

--Input message

‘Skip’

--Output state

has_corona

Skips location.

async project.Telegram_Chatbot.bot.process_loss_smell(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

res_difficult

--Input message

IN: ‘Yes’, ‘No’

--Output state

sore_throat

Stores Input message as [‘symptoms’][‘res_difficult’] in the metadata.

async project.Telegram_Chatbot.bot.process_others(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

others

--Input message

NOT IN: ‘Yes’

--Output state

Start

  • Stores Input message as [‘symptoms’][‘others’] in the metadata.

  • Deletes file_path from the metadata (json).

  • Performs a POST request on API_HOST+’users’ with a body containing both the metadata and the audio sample in a files dictionary.

  • Resets chatbot.

Note

Metadata is stored in dict format. The DB converts it to BSON automatically when inserting the entry.

async project.Telegram_Chatbot.bot.process_others_write(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

others

--Input message

‘Yes’

--Output state

others

async project.Telegram_Chatbot.bot.process_shortness_breath(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

fever

--Input message

IN: ‘Yes’, ‘No’

--Output state

fatigue

Stores Input message as [‘symptoms’][‘fever’] in the metadata.

async project.Telegram_Chatbot.bot.process_smoker(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

smoker

--Input message

IN: ‘Yes’, ‘No’

--Output state

cold

Stores Input message as [‘symptoms’][‘smoker’] in the metadata.

async project.Telegram_Chatbot.bot.process_tiredness(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

cold

--Input message

IN: ‘Yes’, ‘No’

--Output state

res_difficult

Stores Input message as [‘symptoms’][‘cold’] in the metadata.

async project.Telegram_Chatbot.bot.process_user_invalid(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

username

--Input message

NOT IN: alpha

--Output state

username

Philter so the username is only alpha.

async project.Telegram_Chatbot.bot.process_username(message: aiogram.types.message.Message, state: aiogram.dispatcher.storage.FSMContext)
--Input state

username

--Input message

alpha

--Output state

age

Stores Input message as [‘username’] and the id as [‘id’] in the metadata.