GoogleCloudPlatform / dialogflow-integrations

Dialogflow integrations with multiple platforms including KIK, Skype, Spark, Twlio, Twitter and Viber
https://cloud.google.com/dialogflow/
Apache License 2.0
256 stars 503 forks source link

The application does not receiving events from the twitter app #13

Open JyothishG opened 4 years ago

JyothishG commented 4 years ago

Hi,

Already have an agent that uses the inbuilt integration with twitter. Because of the deprecation warnings, I tried to use this open-sourced integration. Done everything as said in the instructions. It is deployed in Cloud Run. I even created a new app on twitter when the existing twitter app didn't work. But no luck so far. There are no requests coming to the cloud run logs.

In short, the bot is down. I need some help urgently.

Regards. Jyothish G

mekpavit commented 4 years ago

I am now trying to reproduce your issue. If there is any progress, I will tell you here!

mekpavit commented 4 years ago

I've already tested the dialogflow-integration with Twitter's direct message. I found out, that when you deploy dialogflow-integration to Cloud Run, there is a chance, that it will not work! That is, because your dialogflow-integration on Cloud Run fail the Twitter's specification for webhook (Webhook must respond back to Twitter request within 3 seconds).

Here's what happens when you deploy dialogflow-integration to Cloud Run:

  1. dialogflow-integration sends a POST request to Twitter API to delete any subscriptions on your Twitter Account
  2. dialogflow-integration sends a POST request to Twitter API to delete any registered webhooks on your Twitter Account
  3. dialogflow-integration sends a POST request to Twitter API to register a new webhook url (the webhook url specified in targetUrl variable) to your Twitter app 3.1 Twitter sends a GET request to your dialogflow-integration to make sure that your web app supports POST requests for incoming events 3.2 If your dialogflow-integration responds Twitter request within 3 seconds, your webhook url will be registered. If not, the webhook will not be registered (this might be the reason why your dialogflow-integration did not work!)**
  4. dialogflow-integration sends a POST request to Twitter API to register a subscription to your Twitter app --> If (3) fails, this will fail as well.

In order to solve your issue, I suggest you to:

  1. Replace registerWebhook function in /twitter/server.js with the following code

    function registerWebhook() {
    return new Promise((resolve, reject) => {
    request.post('https://api.twitter.com/1.1/account_activity/all/'+
        environmentName+'/webhooks.json',{
      oauth:twitterOAuth,
      form:{
        url:targetUrl
      }
    }, function(err, resp, body){
      if (err) {
        console.error('Failed to register webhook: ' + err);
        reject();
      } else {
        // to check the webhook registration result from Cloud Run console
        console.log("register webhook: ", JSON.stringify(resp.body))
        resolve();
      }
    });
    });
    }

    Note: This code has the same function as the original registerWebhook, except it will also print the response from the Twitter to Cloud Run log.

  2. Re-deploy your dialogflow-integration to Cloud Run

  3. Check the log console. If you see the following message, try to re-deploy your dialogflow-integration again!

    register webhook: "{\"errors\":[{\"code\":214,\"message\":\"High latency on CRC GET request. Your webhook should respond in less than 3 seconds.\"}]}"

Hope this solves your problem!

JyothishG commented 4 years ago

@mekpavit Thanks for the help. I added the log as you suggested and it shows the below error message:

register webhook: "{\"errors\":[{\"code\":214,\"message\":\"Too many resources already created.\"}]}"

mekpavit commented 4 years ago

@JyothishG I suggest you to use the Twitter Activity Dashboard to see all the webhook registered to your account. I guess that maybe you have too many active webhook/subscription on your Twitter Dev account. I'm not sure because I don't get used to Twitter API that much. But I think you should try to register the webhook and subscription to the dashboard instead, so we can make sure that the problem doesn't come from dialogflow-integration source code.

So, could you please try to register the webhook/subscription directly via the dashboard and share the upcoming result?

JyothishG commented 4 years ago

@mekpavit When I tried to deploy the twitter activity dashboard application to Heroku like the instructions, it fails to launch the application. I'm always getting this error in the Heroku logs: SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

When tried in the local environment, it runs but the second step in "Configure webhook to receive events" fails (ie, Add a user subscription). It says:

Callback URL not approved for this client application. Approved callback URLs can be adjusted in your application settings

I'm using ngrok to tunnel the requests and added the callback URLs to the application settings on the twitter developer page.

I'm really stuck here.

mekpavit commented 4 years ago

@JyothishG Instead of using URL from ngrok, you can just use http://127.0.01:5000/ as a callback URL.

Try this one

  1. Add these 2 callback URL to your Twitter app http://127.0.0.1:5000/callbacks/addsub http://127.0.0.1:5000/callbacks/removesub

  2. Access your local dashboard using http://127.0.0.1:5000/

  3. Try adding/removing subscription and webhook

oort7 commented 4 years ago

I was able to register webhook url using Twitter Activity Dashboard - I also got several errors (high latency, too many resources) but after several attempts it was registered. In the next step it needs to be validated. I have disabled Twitter integration in Dialogflow and now it works through deployed container. Thanks!