Friday, 21 April 2017

Is there a way to disconnect from Podio authentication?

i am currently working on a node.js app which waits for a item.create event in podio and then sends the item data + participation question via Slack to the user. I am using the password auth for testing purposes, which means that i am just storing the emails and pws to log into podio in a mongodb database. To send the data and participation question to the user i am using the mongoose stream function (much like a for loop, i think) to auth in podio with each user and then sending the information. I need to login/auth with each user seperatly in order to send the message to the right person in slack and set the participation for the specific user.

Problem: The sending to all users happens basically at the same time and podio always uses the authentication data was sent at last, as far is I see it. This means that i am just able to set the podio-event participation for the same user over and over again. Each user recieves a message with the participation question but it only effects the same account in podio over and over again. The "looping" happens in the cursor.on function.

I think the solution would be to disconnect every time podio has auth the user and i recieved the participation i need.

Any help would be highly appreciated. Thanks !

      /* Mongoose Datenbank */
      var cursor = Benutzer.find().cursor();

      cursor.on('data', function(doc) {
        // Called once for every document
        var slackuserid = doc.slack_id;
        var podiomail = doc.podio_mail;
        var podiopasswort = doc.podio_passwort;

        // Podio | Authentifizierung mit Username und Passwort !Austauschen mit email_raw, passwort
        podio.authenticateWithCredentials(podiomail, podiopasswort, function() {

          console.log("§", podiomail, podiopasswort);
          console.log("§ Podio-Authentifizierung erfolgreich druchgeführt!");

        // Podio | Get Item Daten
        podio.request('GET', `/item/${item_id}`)
        .then(function(responseData) {

          var datum = responseData.title;
          var ersteller = responseData.created_by.name;
          var link = responseData.link;

          console.log(`§ Podio Logged in als: ${podiomail}`);

          console.log("§ Slack: Starting private conversation and sending information to Slack User ...");
        // Slackbot | Sending Information + Teilnahmefrage an User - startPrivateConversation
        podibot.startPrivateConversation({user: slackuserid}, function(err,convo) {

          if (err) {
            console.log(err);
           } else {
               // 1. Sende icouncil Protokoll Attachment
               convo.say({
                 username: "Podibot",
                 icon_url: 'https://avatars.slack-edge.com/2017-04-02/163327137508_3ed9a7c247005d295dea_72.png',
                 "attachments": [
                    {
                      "fallback": "Fallback Text",
                      "text": ":grey_exclamation:New event!:grey_exclamation:\n ",
                        "fields": [
                            {
                                "title": "Datum",
                                "value": `${datum}`,
                                "short": true
                            },
                            {
                                "title": "Ersteller",
                                "value": `${ersteller}`,
                                "short": true
                            },
                            {
                                "title": "Age",
                                "value": "Test",
                                "short": true
                            },
                            {
                                "title": "Link",
                                "value": "<" + `${link}` + "|podio.com/protokolle>",
                                "short": true
                            }
                        ],
                 "color": "#0F5Ba5"
                  }
                ]
              });
              // 2. Send participation
              convo.ask('Bitte Antworte mit : `Zusage` | `Absage` | `Vielleicht`', function(response, convo) {

                controller.storage.users.get({user: slackuserid}, function(err, user) {
                    if (!user) {
                      user = {
                          id: {user: slackuserid},
                      };
                    }

                    part_antwort = convo.extractResponse('teilnahme');

                    // 3. Erwarte Anwort über icouncil Teilnahme
                    if (part_antwort === 'Zusage') {

                      var requestData = { status: 'accepted' };
                      podio.request('PUT', `/item/${item_id}/participation`, requestData)
                      .then(function(requestData){
                        console.log(`§ Podio: Teilnahme hat funktioniert! Durchgeführt von ${podiomail}`);
                      });

                      convo.say(`Deine Zusage wurde gespeichert.`);

                    } else if (part_antwort === 'Absage') {

                        var requestData = { status: 'declined' };
                        podio.request('PUT', `/item/${item_id}/participation`, requestData)
                        .then(function(requestData){
                          console.log(`§ Podio: Absage hat funktioniert! Durchgeführt von ${podiomail}`);
                        });

                        convo.say(`Deine Absage wurde gespeichert.`);

                      } else if (part_antwort === 'Vielleicht') {
                          var requestData = { status: 'tentative' };
                          podio.request('PUT', `/item/${item_id}/participation`, requestData)
                          .then(function(requestData){
                            console.log(`§ Podio: Vielleicht hat funktioniert! Durchgeführt von ${podiomail}`);
                          });

                          convo.say(`Du wirst vielleicht teilnehmen.`);

                      } else {
                        convo.say(`Diese Antwort ist leider nicht möglich. Achte auf Gross/Kleinschreibung.`);
                      };
                });
                convo.next();
              }, {'key': 'teilnahme'});

             }
             // ENDE Slackbot | else Sending Information + Teilnahmefrage 
        });
        // ENDE Slackbot | Sending Information + Teilnahmefrage an User - startPrivateConversation

      }).catch(function(err){
        console.log("Error:" + err.message);
        });
        // ENDE Podio | then Get Item Daten
      });
      // ENDE Podio | Authentifizierung mit Username und Passwort
      });

      cursor.on('close', function() {
        // Called when done
      });



via miaue

No comments:

Post a Comment