dyne / reflow-os

Base scripts to run Reflow OS
7 stars 2 forks source link

ValueFlows queries: Foody Zero Waste use case #26

Open vcuculo opened 2 years ago

vcuculo commented 2 years ago

As suggested by @adam-burns , I share the snippets of code involved in the Foody Zero Waste use case for the interaction with the ReflowOS backend both from the IoT device and the Telegram bot.

IoT device

  1. the phyisical device performs the login and stores the token
    mutation {
    login(emailOrUsername: graphqlUser, password: graphqlPass) {
      token
    }
    }
  2. when requested by the user, the physical device sends a new offer
    mutation{
    createOffer(intent:{
      action: "transfer",
      name:"Donazione",
      receiver:"01FKNV3D8T0CBC8SYASJASRSS6",
      resourceConformsTo:"01FKQPHEY0YWK83MKCM8T5RE7Q",
      availableQuantity:{
         hasUnit:"01FKNWF5Q3013GYDC4A9CCY65R",
         hasNumericalValue: String(quantity)
      },
      note: getEloquentQuality(quality)
    }){
      intent{
         id
      }
    }
    }
  3. It saves the newly created intent id in an array called offerIds
  4. and adopting a polling mechanism it starts checking whether an offer has been accepted
    query{
    intent(id: offerIds[i]){
      satisfiedBy{
         resourceQuantity{
            hasNumericalValue
         }
      }
    }
    }

Telegram bot

This client makes much more interactions (queries and mutations) with the backend, but all the GraphQL queries are collected in this file: queries.js and @srfsh started reviewing it here.