如何在Node.js上直接发送消息(Twitter)?

时间:2019-02-05 12:14:19

标签: node.js twitter

我有一个nodejs服务器,我回答了一条新的直接消息。但是Twitter更改了api,现在它不起作用了。我将twit sdk用于nodejs。因此,我进行了调查,并使用sdk“ twitter-webhook”阅读了新邮件,但没有看到有关发送直接邮件或发布推文的信息。 您能否告诉我一些有关如何使用此SDK的建议?还是我必须使用其他库来回答直接消息? 谢谢

编辑:我已经添加了代码,但是它是带有参数的示例,在其中我看到了收到的消息

'use strict';
var debug = require('debug');
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
const twitterWebhooks = require('twitter-webhooks');

var routes = require('./routes/index');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);


const userActivityWebhook = twitterWebhooks.userActivity({
    serverUrl: '',
    route: '/twitter', //default : '/'
    consumerKey: '',
    consumerSecret: '',
    accessToken: '',
    accessTokenSecret: '',
    environment: '',
    app
});



// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function (err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

app.set('port', process.env.PORT || 3000);

var server = app.listen(app.get('port'), function () {
    debug('Express server listening on port ' + server.address().port);
});

//Register your webhook url - just needed once per URL
userActivityWebhook.register();
userActivityWebhook.unsubscribe({
    userId: '',
    accessToken: '',
    accessTokenSecret: ''
}).then((c) => {
    //Subscribe for a particular user activity
    userActivityWebhook.subscribe({
        userId: '',
        accessToken: '',
        accessTokenSecret: ''
    })
        .then(function (userActivity) {
            userActivity
                .on('tweet_create', (data) => console.log(userActivity.id + ' - tweet_create'))
                .on('direct_message', (data) => {


                    console.log(userActivity.id + ' - direct_message');
                })
        })
        .catch((er) => {

            console.error(er);
        });
}).catch((er) => {
    userActivityWebhook.subscribe({
        userId: '',
        accessToken: '',
        accessTokenSecret: ''
    })
        .then(function (userActivity) {
            console.log("asasas");
            userActivity
                .on('tweet_create', (data) => console.log(userActivity.id + ' - tweet_create'))
                .on('direct_message', (data) => {
                    console.log(userActivity.id + ' - direct_message');
                })
        })
        .catch((er) => {

            console.error(er);
        });

    });

//listen to any user activity
userActivityWebhook.on('event', (event, userId, data) => {

    if (event == "direct_message") {
        console.log("userid: " + userId);
        console.log("senderid: " + data["message_create"]["sender_id"]);
        console.log("userid: " + data["message_create"]["message_data"].text);


        console.log(userId + ' - event - ' + event);
    }

});

//listen to unknown payload (in case of api new features)
userActivityWebhook.on('unknown-event', (rawData) => console.log(rawData));

0 个答案:

没有答案