Reputation: 1
i made twitter bot using twitter-autohook module and server for heroku(hobby)
my bot auto response to user who mention or send dm include keyword
my bot response to mention(using stream) but doesn't response to dm(using webhook) when this app deploy to heroku for 6~7hour(not exact) there is no error(logs) and no detected dm(logs either) but same time, it can detect mention(stream)
how can i fix it?
Upvotes: 0
Views: 494
Reputation: 116
Autohook relies on ngrok to expose its built-in webhook server from a local machine to the internet. After a few hours, ngrok will pause the tunnel, and this will prevent Autohook from receiving webhook events.
There are two ways to prevent this from happening.
Autohook accepts ngrok Authokens. An Authtoken is an authentication key issues by ngrok. When provided with one, ngrok can keep the tunnel alive for a longer period. You can obtain an Authoken by creating an account on ngrok (it's free):
.env.twitter
file, add NGROK_AUTH_TOKEN=(the authtoken you just copied)
. This will help your local development.heroku config:set NGROK_AUTH_TOKEN (the authtoken you just copied)
Autohook uses ngrok to create a webhook URL that can be reached from the internet. When you deploy to Heroku (or any other service), you already have both a public server and a public URL, and that means you don't need ngrok at all on Heroku.
Your server's router must serve a webhook route. If you're using Express, this is very simple to implement:
app.all('/webhook/twitter', async (request, response) => {
// Fulfills the CRC check when Twitter sends a CRC challenge
if (request.query.crc_token) {
const signature = validateWebhook(request.query.crc_token, {consumer_secret: process.env.TWITTER_CONSUMER_SECRET});
response.json(signature);
} else {
// Send a successful response to Twitter
response.sendStatus(200);
// Add your logic to process the event
console.log('Received a webhook event:', request.body);
}
});
When the server starts, you will not need to start ngrok. Instead, simply pass the URL to your Heroku instance to Autohook.
const HEROKU_WEBHOOK_URL = 'https://your-app-name.herokuapp.com/webhook/twitter';
const listener = server.listen(process.env.PORT, async () => {
await webhook.start(HEROKU_WEBHOOK_URL);
console.log(`Your app is listening on port ${listener.address().port}`);
});
Upvotes: 3