How can I post a GIF image to twitter using Twit library? - node.js

I am trying to build a small bot that posts random GIFs to twitter based on a hard-coded category (for now).
I am using Twit library for making posts to twitter using the Twitter API. How can I post a GIF to twitter?
Here's the code :
var twit = require('twit');
var config = require('./config');
var request = require('request');
var fs = require('fs');
var path = require('path');
const GIPHY_API_KEY = 'API-KEY';
const GIPHY_API_URL = 'http://api.giphy.com/v1/gifs/random? api_key='+GIPHY_API_KEY+'&tag=study';
var T = new twit(config);
getAndPostGifImg();
function getAndPostGifImg() {
request(GIPHY_API_URL,function (error,response,body) {
var resp = JSON.parse(body);
var img_url = resp.data.image_url;
console.log(img_url);
// post the image to twitter
postImg(img_url);
});
function postImg(img_url) {
request(img_url).pipe(fs.createWriteStream('images/imgpost.gif'));
var filename = path.join(__dirname,'/images/','imgpost.gif');
var params = { encoding: 'base64' };
var img = fs.readFileSync(filename,params);
T.post('media/upload', { media_data: img }, onUpload);
function onUpload(err,data,response) {
var id = data.media_id_string; console.log(id);
// post a tweet /hashtag along with image
var tweet = { status: 'random Study Tweet #giphyBotTweets', media_ids: [id] };
T.post('statuses/update',tweet, tweeted);
}
function tweeted(err,data,response){
if(err)
{
var errors = data.errors;
var i = 0;
for(i = 0 ; i < errors.length; i++)
console.log("Error Message(s) : "+errors[i].message);
}
else
{ console.log(data.text); }
}
}
}
Thanks in advance.

T.post(
"media/upload",
{
media_data: dataImage,
command: "APPEND",
media_id: data.media_id_string,
segment_index: 0
},
function(err, data2, response2) {
T.post(
"media/upload",
{ command: "FINALIZE", media_id: data.media_id_string },
function(err3, data3, response3) {
let tweet = {
status: post.caption,
media_ids: [id]
};
T.post("statuses/update", tweet, function(
err,
data4,
response
) {
if (err) console.log("Something Went wrong", err.message);
if (data4.errors) {
console.log(
"ERROR On Success : ",
data4.errors[0].message
);
} else {
let update = {status: 2, TwitpublishId: data.media_id, updatedAt: Date.now() },
options = { new: true };
Post.findByIdAndUpdate(post._id, update, options, (error, result) => {
if (error) return;
console.log('result video========twit',result);
console.log("Posted on Twitter : https://twitter.com");
});
}
});
}
);
}
);
}
);
}
}

Related

getting 403 from lambda calling api gateway

I have an api post end point which would update a customer's information in dynamodb. It is set to authenticate using AWS_IAM. I am getting 403 from my lambda when calling this api. I have allowed execute-api:Invoke permission to the api for the role lambda uses. I see in this post that I need to create a canonical request. I was able to come up with the below code and I still get a 403. I can't figure out what is missing and wish if a different eye can spot the problem. Please help!
"use strict";
const https = require("https");
const crypto = require("crypto");
exports.handler = async (event, context, callback) => {
try {
var attributes = {
customerId: 1,
body: { firstName: "abc", lastName: "xyz" }
};
await updateUsingApi(attributes.customerId, attributes.body)
.then((result) => {
var jsonResult = JSON.parse(result);
if (jsonResult.statusCode === 200) {
callback(null, {
statusCode: jsonResult.statusCode,
statusMessage: "Attributes saved successfully!"
});
} else {
callback(null, jsonResult);
}
})
.catch((err) => {
console.log("error: ", err);
callback(null, err);
});
} catch (error) {
console.error("error: ", error);
callback(null, error);
}
};
function sign(key, message) {
return crypto.createHmac("sha256", key).update(message).digest();
}
function getSignatureKey(key, dateStamp, regionName, serviceName) {
var kDate = sign("AWS4" + key, dateStamp);
var kRegion = sign(kDate, regionName);
var kService = sign(kRegion, serviceName);
var kSigning = sign(kService, "aws4_request");
return kSigning;
}
function updateUsingApi(customerId, newAttributes) {
var request = {
partitionKey: `MY_CUSTOM_PREFIX_${customerId}`,
sortKey: customerId,
payLoad: newAttributes
};
var data = JSON.stringify(request);
var apiHost = new URL(process.env.REST_API_INVOKE_URL).hostname;
var apiMethod = "POST";
var path = `/stage/postEndPoint`;
var { amzdate, authorization, contentType } = getHeaders(host, method, path);
const options = {
host: host,
path: path,
method: method,
headers: {
"X-Amz-Date": amzdate,
Authorization: authorization,
"Content-Type": contentType,
"Content-Length": data.length
}
};
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
if (res && res.statusCode !== 200) {
console.log("response from api", res);
}
var response = {
statusCode: res.statusCode,
statusMessage: res.statusMessage
};
resolve(JSON.stringify(response));
});
req.on("error", (e) => {
console.log("error", e);
reject(e.message);
});
req.write(data);
req.end();
});
}
function getHeaders(host, method, path) {
var algorithm = "AWS4-HMAC-SHA256";
var region = "us-east-1";
var serviceName = "execute-api";
var secretKey = process.env.AWS_SECRET_ACCESS_KEY;
var accessKey = process.env.AWS_ACCESS_KEY_ID;
var contentType = "application/x-amz-json-1.0";
var now = new Date();
var amzdate = now
.toJSON()
.replace(/[-:]/g, "")
.replace(/\.[0-9]*/, "");
var datestamp = now.toJSON().replace(/-/g, "").replace(/T.*/, "");
var canonicalHeaders = `content-type:${contentType}\nhost:${host}\nx-amz-date:${amzdate}\n`;
var signedHeaders = "content-type;host;x-amz-date";
var payloadHash = crypto.createHash("sha256").update("").digest("hex");
var canonicalRequest = [
method,
path,
canonicalHeaders,
signedHeaders,
payloadHash
].join("/n");
var credentialScope = [datestamp, region, serviceName, "aws4_request"].join(
"/"
);
const sha56 = crypto
.createHash("sha256")
.update(canonicalRequest)
.digest("hex");
var stringToSign = [algorithm, amzdate, credentialScope, sha56].join("\n");
var signingKey = getSignatureKey(secretKey, datestamp, region, serviceName);
var signature = crypto
.createHmac("sha256", signingKey)
.update(stringToSign)
.digest("hex");
var authorization = `${algorithm} Credential=${accessKey}/${credentialScope}, SignedHeaders=${signedHeaders}, Signature=${signature}`;
return { amzdate, authorization, contentType };
}

Can't save Data in Json Format in Mongodb

In this application, I am saving semester_id, course_id and Subject in Mongodb. All I need is to save the Subject in Json format. I want to save semester_id , course_id and save Subject in Json(not in array) with same ids - For semeter and course. I am saving subject in array and I am new to Angular. Can anyone help me out. Thanks in advance.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var SubjectSchema = new Schema({
semesterId:{type: String,ref:'semesterNew'},
courseId :{type: String,ref:'CollegeCourse'},
subject:{
subject :{ type:String}
},
createdOn : {type:Date,default:Date.now},
updatedOn : {type:Date,default:Date.now},
});
mongoose.model('Subject',SubjectSchema);
router.post('/addSubject',function(req,res){
var subjects = JSON.stringify(req.body.subject);
var subjectData = new subjectModel({
semesterId:req.body.semesterId,
courseId: req.body.courseId,
subject: subjects,
});
subjectData.save(function (err, result) {
if (err) {
console.error(err);
return res.status(400).json({
message: 'Bad Request'
});
} else {
res.json({
status: 200,
data: result
})
console.log('Check',result);
}
});
});
addSubject(item){
return this.api.post(`${this.apiController}/addSubject`,item);
}
saveSubject() {
const config = {
position: NbGlobalPhysicalPosition.BOTTOM_RIGHT
};
const formData = new FormData();
this.subjectMasterForm.controls.semesterCtrl.markAsDirty();
this.subjectMasterForm.controls.collegeCourseCtrl.markAsDirty();
// this.subjectMasterForm.controls.image.markAsDirty();
var all_subject_array = [];
if (this.subjectMasterForm.valid && this.subjectMasterForm.value.subjects.length > 0) {
if (this.semesterSubjectId == '' || this.semesterSubjectId == null || this.semesterSubjectId == 'undefined' || this.semesterSubjectId== undefined) {
var subjects_values = this.subjectMasterForm.value.subjects
var subjects_length = this.subjectMasterForm.value.subjects.length;
subjects_values.forEach(function (element) {
all_subject_array.push(element.name);
console.log('Check2',element.name);
});
this.overview_data = {
courseId: this.courseId,
semesterId:this.semesterId,
subject: all_subject_array,
semesterSubjectId: this.semesterSubjectId,
}
this.collegeTemplateApi.addSubject(this.overview_data).subscribe(data => {
if (data['status'] == 200) {
this.toasterService.show("Subject successfully Added!!!..", `Success`, config);
} else {
this.toasterService.show("Subject Already exists in our Database!!!...", `Success`, config)
}
});
} else {
if(this.courseId!=undefined && this.semesterId!=undefined){
if (this.subjectMasterForm.value.subjects.length > 0) {
var subjects_values = this.subjectMasterForm.value.subjects
var subjects_length = this.subjectMasterForm.value.subjects.length;
subjects_values.forEach(function (element) {
all_subject_array.push(element.name);
});
this.overview_data = {
courseId: this.courseId,
semesterId:this.semesterId,
subject: all_subject_array,
semesterSubjectId: this.semesterSubjectId
}
}
this.collegeTemplateApi.updateSubject(this.overview_data).subscribe(data => {
if (data['status'] == 200) {
this.toasterService.show("Subject successfully Updated!!!..", `Success`, config);
} else {
this.toasterService.show(data['message'], `Success`, config)
}
});
}
}
} else if (this.subjectMasterForm.value.subjects.length == 0) {
this.subjecterror = true;
}
setTimeout(() => this.ngOnInit(), 3000);
}
In your first code segment, the Subject model is not assigned to a variable.
mongoose.model('Subject',SubjectSchema);
Yet later in your code you declare a new instance of subjectModel.
Try assigning the model to this name.
var subjectModel = mongoose.model('Subject', SubjectSchema);

node js post request

Success localhost response
Cannot GET /u/coupons at server
Frontend script for post
<script>
var count = document.getElementById("count");
var len = document.getElementById("length");
var pattern = document.getElementById("pattern");
document.getElementById('coupons-button').onclick = function()
{
if(count.value!=="" && len.value!=="")
{
const genReq = new XMLHttpRequest();
let url = `count=${encodeURI(count.value)}&` +
`len=${encodeURI(len.value)}&` +
`pattern=${encodeURI(pattern.value)}`;
genReq.open('POST',`/u/coupons?${url}`,true);
genReq.send();
genReq.onreadystatechange = e => {
if(genReq.readyState === 4 && genReq.status === 200){
let gen = JSON.parse(genReq.response);
if (gen.state === "SUCCESS")
{
var coupons = gen.coupons;
for(var i=0;i<coupons.length;i++)
{
var div = document.createElement('div');
var text = document.createTextNode(coupons[i]);
div.appendChild(text);
document.getElementById('coupons-check').appendChild(div);
}
} else {
var div = document.createElement('div');
var text = document.createTextNode("FAIL TO GENERATE");
div.appendChild(text);
document.getElementById('coupons-check').appendChild(div);
}
}
}
}
}
Server script
admin.post( '/u/coupons' ,(req,res)=>{
let params = getParameters(req);
CouponWorker.generateCoupons({
"len":decodeURI(params.len),
"count":decodeURI(params.count),
"pattern":decodeURI(params.pattern)
}).then((_res) =>{
console.log(_res)
if(_res.success === true)
{
res.status(200).json({
"state":"SUCCESS",
"coupons":_res.coupons
});
}
else{
res.status(200).json({"state" : "FAILED"});
}
});
});
CouponWorker
const firebase = require('./Database');
const voucher_codes = require('voucher-code-generator');
exports.generateCoupons = function(params)
{
var len = params.len;
var count = params.count;
var pattern = params.pattern;
//pattern = pattern.replace(/1/g,'#');
const cpn_db = firebase.firebase.database();
var coupons;
return new Promise((resolve,reject)=>{
coupons = voucher_codes.generate({
length:len,
count:count,
prefix:"AMP-",
pattern: '####-####',
charset:"0123456789ABCDEFGHIJKLMNOPQRSTUVXYZ"
});
if(coupons!==null)
{
for(var i =0;i<count;i++)
{
cpn_db.ref('coupons/cid-'+coupons[i]).set({
"addedOn": getDateTime(),
"code" : coupons[i],
"amount" : 20,
"expireDate" : null,
"isActive" : true,
"isDeleted":false
});
}
resolve({
"success":true,
"coupons":coupons
});
}
else
{
resolve({
"success":false
});
}
});
}
Above given snaps shows the same code running on localhost and server,
localhost working fine,giving output and saving data to firbase while server response 404 not found resource.
I can not find the reason of this error.I tried url req on postman and responses are the same as above

NodeJS Error in if else case - Can't set headers after they are sent

I know, this is something old. There are many questions regarding to this. But none of them didn't guide me or didn't gave me the actual concept.
My case is:
if something
render something
else
render somethingAnother
Why is this logic generates this error enter image description here
After 1st execution, I'm not able to continue this process (I could continue for a while, but after some time error will come), by pressing back button of browser and then returning back to the home page. Everytime I should restart my server using node command. Why headers won't reset if I press back button of browser, how to do some header reset or something which will correct my logic.
const cv = require('opencv4nodejs');
var async = require('async');
var OrderID;
var OrderNo;
var compare = 0;
var CompanyName;
var notSimilar = 0;
const download = require('image-downloader')
const distanceThreshold = 30;
var url;
var FolderPath;
var isSimilar = 0;
var j = 0;
var image1;
var dbPath;
var filterCount = 0;
var image2;
var dbImgCount = 0;
var express = require('express');
var request = require('request');
var app = express();
app.set('view engine', 'pug')
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './');
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' + Date.now());
}
});
var upload = multer({ storage : storage}).single('userPhoto');
const sql = require("mssql");
var config = {
user: '***',
password: '****',
server: '192.168.5.100\\SQLEXPRESS',
database: 'Test_MatdesignDB1',
connectionTimeout: 300000,
requestTimeout: 300000,
pool: {
idleTimeoutMillis: 300000,
max: 100
}
};
sql.connect(config).then(pool => {
return pool.request()
.query('select count(*) from OrderImageUpload; select FolderPath from OrderImageUpload;')
}).then(result => {
var a = JSON.stringify(result.recordset[0]);
dbImgCount = a.slice(4,6);
FolderPath = result.recordsets[1];
sql.close();
}).catch(err => {
console.log(err);
sql.close();
})
app.get('/',function(req,res){
res.sendFile(__dirname + "/index.html");
});
app.post('/api/photo',function(req,res){
compare = 1;
upload(req,res,function(err) {
if(err) {
console.log(err);
res.send("File uploading error");
}
else{
// console.log("Success");
image1 = req.file.filename;
var matchFeatures = ({ url, img1, img2, detector, matchFunc }) => {
// detect keypoints
const keyPoints1 = detector.detect(img1);
const keyPoints2 = detector.detect(img2);
// compute feature descriptors
const descriptors1 = detector.compute(img1, keyPoints1);
const descriptors2 = detector.compute(img2, keyPoints2);
// match the feature descriptors
const matches = matchFunc(descriptors1, descriptors2);
// only keep good matches
const bestN = 40;
const bestMatches = matches.sort(
(match1, match2) => (match1.distance - match2.distance)
).slice(0, bestN);
//console.log(bestMatches);
for(var i=0; i<bestN; i++){
if((bestMatches[i].distance) <= (distanceThreshold)){
filterCount++;
}
}
if(filterCount >= (bestN/4))
isSimilar = 1;
if(isSimilar){
notSimilar = 0;
filterCount = 0;
isSimilar = 0;
console.log("Similar images\n");
dbPath = url;
sql.close();
(async function() {
try {
let pool = await sql.connect(config)
let result1 = await pool.request()
.query("select OrderID from Test_MatdesignDB1.dbo.OrderImageUpload where FolderPath = '"+dbPath+"';")
OrderID = result1.recordset[0].OrderID;
let result2 = await pool.request()
.query('select OrderNo , CompanyName from Test_MatdesignDB1.dbo.[Order] where OrderID = '+OrderID);
OrderNo = result2.recordset[0].OrderNo;
CompanyName = result2.recordset[0].CompanyName;
res.render('similar', { title: 'Similar', CompanyName: CompanyName, OrderID: OrderID, OrderNo: OrderNo, img_path_var : dbPath }) //Render number 1 in 'if' case
} catch (err) {
console.log(err);
sql.close();
}
sql.close();
})()
sql.on('error', err => {
console.log(err);
})
}
else{
isSimilar = 0;
filterCount = 0;
notSimilar++;
if(notSimilar >= (dbImgCount ))
{
notSimilar = 0;
res.render('notSimilar', { title: 'Not Similar', message: 'No Similar Images' }) //Render number 2 in 'else' case
}
console.log("Not similar\n");
}
return cv.drawMatches(
img1,
img2,
keyPoints1,
keyPoints2,
bestMatches
);
};
for (j=0; j<dbImgCount; j++) {
(function(j) {
async.waterfall([
async function downloadIMG(done) {
try {
var options = {
url: FolderPath[j].FolderPath,
dest: '/home/ubuntu/imgCompare/DBimages/'
}
const { filename, image } = await download.image(options);
return [filename, options.url];
} catch (e) {
console.error(e)
}
},
async function featureMatching([a, MatchURL], done){
const img1 = cv.imread(image1);
url = MatchURL;;
const img = a.slice(33);
const img2 = cv.imread('./DBimages/'+img);
const orbMatchesImg = matchFeatures({
url,
img1,
img2,
detector: new cv.ORBDetector(),
matchFunc: cv.matchBruteForceHamming
});
done(null);
}
],
function (err) {});
})(j);
}
}
});
});
app.listen(5000,function(){
console.log("Working on port 5000");
});
You need to add return before rendering a view. It's happening because the view rendering is happening more than 1 time there must be a condition in your code which is letting views to render multiple times. Add this return statement:
return res.render();
You're getting this error because you're calling matchFeatures() multiple times within a for loop.
app.post('/api/photo', function (req, res) {
var matchFeatures = ({url, img1, img2, detector, matchFunc}) => {
if (isSimilar) {
res.render('similar', {
title: 'Similar',
...
}) //Render number 1 in 'if' case
} else {
res.render('notSimilar', {
title: 'Not Similar',
message: 'No Similar Images'
}) //Render number 2 in 'else' case
}
};
for (j = 0; j < dbImgCount; j++) {
async function featureMatching() {
const orbMatchesImg = matchFeatures({ // since you're calling it multiple times here
url, // matchFeatures() will attempt to send
img1, // responses multiple times
img2,
detector: new cv.ORBDetector(),
matchFunc: cv.matchBruteForceHamming
});
}
}
});
To fix this, You need to consolidate all these responses and send to client only once.
I figured out the error. I didn't reset the variable notSimilar at the entry point.
Done resetting of notSimilar as below, no error! Thanks Everyone.
app.post('/api/photo',function(req,res){
notSimilar = 0;

TTS with a websocket:Creating a WAV from binary data fails

I try to create a WAV from binary data using the websocket connection on a node.js server. I use the BluemixTTS to create the speech. Here is my current code:
'use strict';
const WebSocket = require('ws');
var express = require('express');
var watson = require('watson-developer-cloud');
var vcapServices = require('vcap_services');
var extend = (extend = require('util')._extend);
var fs = require('fs');
var ttsConfig = extend(
{
version: 'v1',
url: 'https://stream.watsonplatform.net/text-to-speech/api',
username: 'myusernamehere',
password: "mypasswordhere"
},
vcapServices.getCredentials('text_to_speech')
);
var ttsAuthService = watson.authorization(ttsConfig);
var websocket;
ttsAuthService.getToken({ url: ttsConfig.url }, function(err, token) {
if (err) {
console.log('Error retrieving token: ', err);
return;
}
var voice = 'en-US_AllisonVoice';
var wsURI = 'wss://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=' +
voice + '&watson-token=' + token;
websocket = new WebSocket(wsURI);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
});
function onOpen(evt) {
var message = {
text: 'Hello world',
accept: 'audio/wav',
timings: ['words']
};
websocket.send(JSON.stringify(message));
}
var messages;
var audioStream = null;
function onMessage(evt) {
if (typeof evt.data === 'string') {
messages += evt.data;
} else {
if(audioStream == null){
audioStream = evt.data;
}else{
audioStream += evt.data;
}
}
}
function onClose(evt) {
console.log(messages);
var wstream = fs.createWriteStream('test.wav');
wstream.write((audioStream));
wstream.end();
}
function onError(evt) {
}
I get the token, trigger the TTS specifying audio/wav, build my buffer in the onMessage method and then write it into a .wav file. Everything seems fine.
However the file is somehow broken, and cannot be opened with any music-player. Do I miss some special encoding ?
Any help is appreciated
Regards,
Rambazamba
As the data contains a buffer one has to write the buffer in the file directly each time you get a message and then close the file stream in the onClose event. Like this:
var messages;
var wstream = fs.createWriteStream('test.wav');
function onMessage(evt) {
if (typeof evt.data === 'string') {
messages += evt.data;
} else {
wstream.write(evt.data)
}
}
function onClose(evt) {
console.log(messages);
wstream.end();
}
function onError(evt) {
}

Resources