nodejs (twit) how do you follow a users LIVE twitter stream - node.js

I am trying to get twit (https://www.npmjs.com/package/twit) to stream live tweets from specific twitter users.
The code I have is
var stream = T.stream('statuses/filter', { follow: ['nodejs'] })
stream.on('tweet', function (tweet) {
console.log(tweet.text)
})
but it gives the following error
Error: Bad Twitter streaming request: 406
at Object.exports.makeTwitError (/Users/simoncarr/dev/node/nodeletetweet/scripts/get_mp_data/node_modules/twit/lib/helpers.js:74:13)
at Request.<anonymous> (/Users/simoncarr/dev/node/nodeletetweet/scripts/get_mp_data/node_modules/twit/lib/streaming-api-connection.js:96:29)
at Request.emit (events.js:187:15)
at Gunzip.<anonymous> (/Users/simoncarr/dev/node/nodeletetweet/scripts/get_mp_data/node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:273:13)
at Gunzip.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
at Request.<anonymous> (/Users/simoncarr/dev/node/nodeletetweet/scripts/get_mp_data/node_modules/twit/lib/streaming-api-connection.js:99:14)
at Request.emit (events.js:187:15)
[... lines matching original stack trace ...]
at process._tickCallback (internal/process/next_tick.js:63:19)
simply tracking words in a tweet works just fine, for instance the following code works OK.
var stream = T.stream('statuses/filter', { track: ['apples'] })
stream.on('tweet', function (tweet) {
console.log(tweet.text)
})
The twitter documentation suggests that I should be able to simply replace track with follow.
https://developer.twitter.com/en/docs/tweets/filter-realtime/api-reference/post-statuses-filter.html

You need to use the numeric user ID, not the screen name, in the follow parameter. You can use the users/show endpoint to find the user ID for a given screen name.

Related

Error in Nodejs SDk creating timestream datbase/tales

I have permissions, and can create Databases/Tables in the console just fine. but when i make code using the aws-sdk it fails with an InternalServerException: Internal Server Error. Is this only supported in the v3 api?
const params = {
DatabaseName,
Tags: getDefaultTags(customer,username)
};
console.log(params);
const promise = writeClient.createDatabase(params).promise();
to repeat, this is not a permissions issue, I have the region specified, I just get an internal error if I try to use the SDK:
InternalServerException: Internal Server Error
at Request.extractError (<<coderoot>>\node_modules\aws-sdk\lib\protocol\json.js:52:27)
at Request.callListeners (<<coderoot>>\node_modules\aws-sdk\lib\sequential_executor.js:106:20)
at Request.emit (<<coderoot>>\node_modules\aws-sdk\lib\sequential_executor.js:78:10)
at Request.emit (<<coderoot>>\node_modules\aws-sdk\lib\request.js:686:14)
at Request.transition (<<coderoot>>\node_modules\aws-sdk\lib\request.js:22:10)
at AcceptorStateMachine.runTo (<<coderoot>>\node_modules\aws-sdk\lib\state_machine.js:14:12)
at <<coderoot>>\node_modules\aws-sdk\lib\state_machine.js:26:10
at Request.<anonymous> (<<coderoot>>\node_modules\aws-sdk\lib\request.js:38:9)
at Request.<anonymous> (<<coderoot>>\node_modules\aws-sdk\lib\request.js:688:12)
at Request.callListeners (<<coderoot>>\node_modules\aws-sdk\lib\sequential_executor.js:116:18)
if I use the v3, I get the same error eventually, this example is creating a table, but it's the same idea:
InternalServerException: Internal Server Error
at deserializeAws_json1_0InternalServerExceptionResponse (<<coderoot>>\node_modules\#aws-sdk\client-timestream-write\dist-cjs\protocols\Aws_json1_0.js:899:23)
at deserializeAws_json1_0CreateTableCommandError (<<coderoot>>\node_modules\#aws-sdk\client-timestream-write\dist-cjs\protocols\Aws_json1_0.js:239:25)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async <<coderoot>>\node_modules\#aws-sdk\middleware-serde\dist-cjs\deserializerMiddleware.js:7:24
at async <<coderoot>>\node_modules\#aws-sdk\middleware-signing\dist-cjs\middleware.js:13:20
at async StandardRetryStrategy.retry (<<coderoot>>\node_modules\#aws-sdk\middleware-retry\dist-cjs\StandardRetryStrategy.js:51:46)
at async <<coderoot>>\node_modules\#aws-sdk\middleware-logger\dist-cjs\loggerMiddleware.js:6:22
at async Object.createTable (<<coderoot>>\timestream.db.js:228:22)
at async handleCustomerCreation (<<coderoot>>\router.js:60:21)
at async router (<<coderoot>>\router.js:266:13)

Duplicate Content-Length with Axios running in NodeJS/JEST

I face really weird problem with Axios / nanoexpress. I started to write JEST test and copied working piece of code from Vuex that calls my backend. This code works reliably in Vue but here it fails with a network error. The Axios version is the same on both SPA and JEST test.
JEST:
let response = await axios.get(`${API}/users/1234`, getAuthHeader());
function getAuthHeader() {
const config = { headers: { } };
config.headers.Authorization = "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxZTQwdjBiMWo1Iiwibmlja25hbWUiOiJsaXRlcmFrbCIsInB3ZFRpbWVzdGFtcCI6IjIwMjAtMDMtMjJUMTE6MTA6NDkuNDg2WiIsInJvbGVzIjpbImFkbWluOnBvbGwiXSwiaWF0IjoxNTg2NjkwNzc2LCJleHAiOjE1ODkzNjkxNzZ9.N5MfpZ9i9Sjv-izdYItR4gXCmzVkqkuVcVSEL_6Q89c";
return config;
}
When I remove the config with Authorization header, the code works well:
let response = await axios.get(`${API}/users/1234`, getAuthHeader());
This is the error:
Network Error
at createError (node_modules/axios/lib/core/createError.js:16:15)
at XMLHttpRequest.handleError (node_modules/axios/lib/adapters/xhr.js:83:14)
at XMLHttpRequest.<anonymous> (node_modules/jsdom/lib/jsdom/living/helpers/create-event-accessor.js:33:32)
at innerInvokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:316:27)
at invokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:267:3)
at XMLHttpRequestEventTargetImpl._dispatch (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:214:9)
at fireAnEvent (node_modules/jsdom/lib/jsdom/living/helpers/events.js:17:36)
at requestErrorSteps (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:121:3)
at Object.dispatchError (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:51:3)
at EventEmitter.<anonymous> (node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:675:20)
at Request.<anonymous> (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:384:47)
at Request.onRequestError (node_modules/request/request.js:877:8)
console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Error: Parse Error: Duplicate Content-Length
at Object.dispatchError (C:\Users\leos\WebstormProjects\nano-options\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:54:19)
at EventEmitter.<anonymous> (C:\Users\leos\WebstormProjects\nano-options\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:675:20)
at EventEmitter.emit (events.js:327:22)
at Request.<anonymous> (C:\Users\leos\WebstormProjects\nano-options\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:384:47)
at Request.emit (events.js:315:20)
at Request.onRequestError (C:\Users\leos\WebstormProjects\nano-options\node_modules\request\request.js:877:8)
at ClientRequest.emit (events.js:315:20)
at Socket.socketOnData (_http_client.js:486:9)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:297:12) undefined
The service is nothing fancy and it is never called when Authorization header is present and it is called when the Authorization header is missing.
app.get('/v1/users/:userId', async (req, res) => {
console.log("getUser handler starts");
const response = {};
res.send(response);
return res;
});
I have spent several hours trying to see the raw network communication but failed. It must be something with Axios because I can run successfuly exactly the same request with the Authorization header from Postman.
Update:
it seems to be related to NodeJS version. I submitted an issue: https://github.com/axios/axios/issues/2889
Update 2:
I switched from Axios to Got and the test is working properly. https://github.com/literakl/mezinamiridici/commit/e667c5661429fc8cba273716dfced2244e211abf

ERROR event: Not Found; when triggering event from another event : probot app built in nodejs

I am building a GitHub app in probot and nodejs and trying to update the PR whenever a pull_request.synchronize event occurs. I know that doesn't make much sense, but the point is I want to be able to update the PR upon the occurrence of a certain event.
app.on('pull_request.synchronize', async context => {
console.log('---------------------- on pull_request.synchronize, body of PR : ', context.payload.pull_request)
console.log('State of PR after pull_request.synchronize event :---------------', context.payload.pull_request.state)
await context.github.pulls.update({
owner:context.payload.repository.owner,
repo :context.payload.repository.name,
pull_number :context.payload.pull_request.number,
title:'updated Pull Request Title: Success',
body:'updated Pull Request Body: Success',
})
})
I get below error every time :
ERROR event: Not Found (id=ssjsk-dd-sdfsdfs-fsfs-fsfsfsffsd)
HttpError: Not Found
at response.text.then.message (C:\GitWorkspace\user\GitHubApps\github-app-name\node_modules\#octokit\request\dist-node\index.js:66:23)
at process._tickCallback (internal/process/next_tick.js:68:7)
--
event: {
"event": "pull_request.synchronize",
"id": "2332-3131-131-31231313-1313232144142",
"installation": 3352243,
"repository": "user/Demo-Repo"
}
06:24:58.460Z ERROR probot: Not Found
HttpError: Not Found
at response.text.then.message (C:\GitWorkspace\user\GitHubApps\github-app-name\node_modules\#octokit\request\dist-node\index.js:66:23)
at process._tickCallback (internal/process/next_tick.js:68:7)
06:24:58.472Z INFO http: POST / 500 - 519.93 ms (id=3424234-43242-42423478a-4242-42342342)
06:24:58.476Z ERROR probot: Internal Server Error
Error: Internal Server Error
at Request.callback (C:\GitWorkspace\user\GitHubApps\github-app-name\node_modules\superagent\lib\node\index.js:706:15)
at IncomingMessage.parser (C:\GitWorkspace\user\GitHubApps\github-app-name\node_modules\superagent\lib\node\index.js:916:18)
at IncomingMessage.emit (events.js:203:15)
at IncomingMessage.EventEmitter.emit (domain.js:448:20)
at endReadableNT (_stream_readable.js:1129:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Why is it unable to get the detail out of context, when in fact the context does get printed out by my console statement and contains the pull_request details.
I am just a beginner in both probot and nodejs so I am not sure if this has anything to to do with the syntax.
The problem is that context.payload.repository.owner is an object, compare the example payload at https://developer.github.com/v3/activity/events/types/#webhook-payload-example-28
Try to replace it with context.payload.repository.owner.login.
Probot also provides a convenience method which returns and { owner, repo } object based on the context, and you can pass extra parameters to it
Try the following
await context.github.pulls.update(context.repo({
pull_number: context.payload.pull_request.number,
title:'updated Pull Request Title: Success',
body:'updated Pull Request Body: Success',
}))

Not able to receive an attachment from Skype in BotFramework

On BotFramework (NodeJS), I was trying to replicate the demo available at https://learn.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-send-receive-attachments . It actually works well.
Code in case ms article change:
// Create your bot with a function to receive messages from the user
var bot = new builder.UniversalBot(connector, function (session) {
var msg = session.message;
if (msg.attachments && msg.attachments.length > 0) {
// Echo back attachment
var attachment = msg.attachments[0];
session.send({
text: "You sent:",
attachments: [
{
contentType: attachment.contentType,
contentUrl: attachment.contentUrl,
name: attachment.name
}
]
});
} else {
// Echo back users text
session.send("You said: %s", session.message.text);
}
});
However, the problem I am facing is that when I do a call from Skype (Normal), I receive an error message:
2017-12-07T02:16:15.815Z Error: POST to 'https://smba.trafficmanager.net/apis/v3/conversations/<My Conversation>/activities' failed: [400] Bad Request
at Request._callback (/app/node_modules/botbuilder/lib/bots/ChatConnector.js:545:46)
at Request.self.callback (/app/node_modules/request/request.js:186:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (/app/node_modules/request/request.js:1163:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> (/app/node_modules/request/request.js:1085:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
Any ideas?
[Update: It happens only when I create the attachment response. So I expect that's where I have an issue]
Actually the code on the MS Website is not up-to-date (in a way).
If I follow the code visible at : https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/core-ReceiveAttachment
By example, I can receive the attachment and save it somewhere on a public folder. Once completed, then I can send back the "public" URL as attachment and then it works.

Node server is getting down with error buffer.js:488 throw new Error('toString failed');

We have an app build on nodejs (Server A), which is fetching data from another nodejs app(Server B) using REST API's.
Similarly for downloading files Server A make request on Server B and Server B fetches files from AWS S3 and pipes the data back to server A.
This system works perfectly and we are able to download files upto 1GB.
Issue is sometimes Server A crashes with below error:
buffer.js:488
throw new Error('toString failed');
^
Error: toString failed
at Buffer.toString (buffer.js:488:11)
at Request.<anonymous> (/var/www/sky/portal/node_modules/request/request.js:1145:39)
at emitOne (events.js:95:20)
at Request.emit (events.js:182:7)
at IncomingMessage.<anonymous> (/var/www/sky/portal/node_modules/request/request.js:1091:12)
at IncomingMessage.g (events.js:273:16)
at emitNone (events.js:85:20)
at IncomingMessage.emit (events.js:179:7)
at endReadableNT (_stream_readable.js:913:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
Server A is calling server B using below code:
exports.downloadAsset = function(req, res, assetUrl, cb) {
res.cookie('fileDownload', 'true');
request(
config.api.baseUrl + assetUrl
,
function(error, response, body) {
}
).pipe(res);
}
We are not sure from where app is getting crashed.
Please suggest.
The Solution I used to sort above issue:
I had to modify our flow. We were streaming data from Server B to Server A, but now Server B is sending signed URL to Server A and file is being downloaded directly from S3 using that signed url.

Resources