Send email to a specific email using mailchimp - javascript

I know about the mail chimp JSON based API. However, I'm not sure if there is any way to use it to send an email to a specific recipient using javascript.
Any ideas on how to do this?

Following steps you have to take:
I assume you already created a mail template in mail chimp
I am using mandrill to send mail through mail chimp. Install node package manager mandrill-api
npm install mandrill-api
Call package in top of your code
var mandrill = require('mandrill-api/mandrill');
var mandrill_client = new mandrill.Mandrill('YOUR_API_KEY');
In your method define message variable
var message = {
"from_email": send_as_email,
"from_name": send_as_name,
"merge_language": "mailchimp",
"global_merge_vars": null
};
message.to = [{
"email":'SENDER_EMAIL',
"name":'SENDER_NAME',
"type": "to"
}];
message.subject ='SUBJECT OF MAIL';
mandrill_client.messages.sendTemplate({
"template_name":'NAME_OF_MAILCHIMP_TEMPLATE',
"template_content": null,
"message": message
}, function (result) {
//DO ON RESPONSE
}, function (e) {
//TRACE Your Error
});

Related

Not able to autheticate google cloud vision api . How to autheticate it to use it further

My Code looks like this :-
var vision = require('#google-cloud/vision');
handleSubmit = () =>{
console.log("encoded string submitted=",this.state.files);
this.useVisionCloud();
}
useVisionCloud = () =>{
const client = new vision.ImageAnnotatorClient();
const request_body = {
"requests": [
{
"image": {
"content": this.state.files
},
"features": [
{
"type": "TEXT_DETECTION"
}
]
}
]
};
client.textDetection(request_body).then(response => {
console.log("text got=",response);
// doThingsWith(response);
})
.catch(err => {
console.log("error got=",err);
});
}
I already tried setting environment variable but didn't work .
I have also created service account and downloaded the file.json
but dont know how to use it for authentication
im getting the following error :-
Uncaught Error: {"clientConfig":{},"port":443,"servicePath":"vision.googleapis.com","scopes":["https://www.googleapis.com/auth/cloud-platform","https://www.googleapis.com/auth/cloud-vision"]}You need to pass auth instance to use gRPC-fallback client in browser. Use OAuth2Client from google-auth-library.
on triggering a post request to Google API .
Final Query is :- I am not able to understand how to authenticate API so that i can further use it to detect text in my images
Set the env variable eg:
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
I would recommend having a separate backend that calls vision api. Then have the javascript code in your browser call your backend.

Delete By Query API working as curl but not in Node-Red

Background: What I am trying to achieve is to delete multiple values from elastic using a single API call. Our app uses Node-Red to create the backend API's.
I am using below curl command to delete multiple doc id's and it is working like a charm. It deletes the docs found with id's xxxxx and yyyyy.
POST /tom-access/doc/_delete_by_query
{
"query": {
"terms": {
"_id": [
"xxxxx",
"yyyyy"
]
}
}
}
However, when I try to do the same via Node-Red (using a JavaScript function), I am getting below error.
{"error":{"root_cause":[{"type":"action_request_validation_exception","reason":"Validation
Failed: 1: query is
missing;"}],"type":"action_request_validation_exception","reason":"Validation
Failed: 1: query is missing;"},"status":400}
Here is what I have inside the Node-Red JavaScript function:
if (!msg.headers) msg.headers = {};
msg.req = {
"query": {
"terms": {
"id": [
"xxxxx",
"yyyyy"
]
}
}
};
msg.headers = {
"Content-Type": "application/json",
"Authorization" : "Basic xxxxxxxxxxxxxxxxxxxxx"
};
msg.method = "POST"
// New elastic
msg.url = "http://elastic.test.com/tom-access/doc/_delete_by_query";
return msg;
The next node makes an HTTP CALL using above msg object but results in the error mentioned above. I am new to Node-Red, JavaScript and Elastic as well. HEEELP!!!
The endpoint is probably expecting the query to be in the body of the requests.
You should be setting it under msg.payload not msg.req.

Pushing messages from WhatsApp to Twilio Flex via Twilio Studio results in Schema Validation Warning 12200

I have set the project up as described in the blog post Twilio WhatsApp API and Flex in Minutes.
I followed the instructions and even after repeating the process twice, on two different accounts, when I send the message (through sandbox), the following error occurs:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'Enqueue'. One of '{Sms, Message, Redirect}' is expected.
The task does not show in Flex.
I have tried checking what WhatsApp sends vs what is sent when SMS arrives. This is what a simple function says about trigger.message for SMS:
{
EventType=onMessageSent,
InstanceSid=IS5aa457db5a2d44049c95f2d0b2699f56,
Attributes={\"proxied\":true},
DateCreated=2019-01-16T12:37:43.664Z,
Index=3,
From=sms_g13aggbhlwpgk7tbah3llj2kyjpsvvju,
MessageSid=IM9123e909eeda41ac92890201a6c3f1b4,
Source=API,
AccountSid=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX,
ChannelSid=CHbc79143368db4680a66c6c504103619f,
RetryCount=0,
ClientIdentity=sms_g13aggbhlwpgk7tbah3llj2kyjpsvvju,
WebhookType=studio,
To=CHbc79143368db4680a66c6c504103619f,
Body=Test Message,
ChannelAttributes={
status=ACTIVE,
forwarding=true,
serviceNumber=sms_g13aggbhlwpgk7tbah3llj2kyjpsvvju,
twilioNumber=+1228xxxxxxx,
from=+44798xxxxxxx,
channel_type=sms,
proxySession=KC1be96a19ed42aca39f9b6a9f06c26997
},
WebhookSid=WHcfc83a43a7c2424693fd0053b8b00a01
}
This is what is shows for WhatsApp message:
{
ApiVersion=2010-04-01,
SmsSid=SMb44cfdb4854c41e9ee255032a5449199,
SmsStatus=received,
SmsMessageSid=SMb44cfdb4854c41e9ee255032a5449199,
NumSegments=1,
From=whatsapp:+44798xxxxxxx,
To=whatsapp:+1228xxxxxxx,
MessageSid=SMb44cfdb4854c41e9ee255032a5449199,
Body=Test Message,
AccountSid=ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX,
NumMedia=0
}
As you can see, the ChannelAttributes and ChannelSid are missing, and this is what Flex uses.
I then tried to transform the response with a function:
exports.handler = function(context, event, callback) {
let response;
if (!event.message.ChannelAttributes) {
response = {
name: event.message.From,
channelType: 'sms',
channelSid: 'CHbc79143368db4680a66c6c504103619f'
};
}
else {
response = {
name: "WhatsApp_" + event.message.ChannelAttributes.from,
channelType: event.message.ChannelAttributes.channel_type,
channelSid: event.message.ChannelSid
};
}
const twilioResponse = new Twilio.Response();
twilioResponse.setStatusCode(200);
twilioResponse.appendHeader('Content-Type', 'application/json');
twilioResponse.setBody(response);
callback(null, twilioResponse);
};
And assign the attributes in Studio like:
{
"name":"{{widgets.MyFunction.body.name}}",
"channelType":"{{widgets.MyFunction.body.channelType}}",
"channelSid":"{{widgets.MyFunction.body.channelSid}}"
}
Unfortunately, this shows the same error.

How to send email using MailChimp API

I'm creating an app in nodejs to send an email using MailChimp. I've tried to use https://apidocs.mailchimp.com/sts/1.0/sendemail.func.php but changed it to use 3.0 api because 1.0 seems to no longer work (big surprise). I've setup my app with
var apiKey = '<<apiKey>>',
toEmail = '<<emailAddress>>',
toNames = '<<myName>>',
message = {
'html': 'Yo, this is the <b>html</b> portion',
'text': 'Yo, this is the *text* portion',
'subject': 'This is the subject',
'from_name': 'Me!',
'from_email': '',
'to_email': toEmail,
'to_name': toNames
},
tags = ['HelloWorld'],
params = {
'apikey': apiKey,
'message': message,
'track_opens': true,
'track_clicks': false,
'tags': tags
},
url = 'https://us13.api.mailchimp.com/3.0/SendEmail';
needle.post(url, params, function(err, headers) {
if (err) {
console.error(err);
}
console.log(headers);
}
});
I keep getting a 401 response (not authorized because I'm not sending the API key properly)
I have to use needle due to the constraints on the server.
There is no "SendEmail" endpoint in API v3.0. MailChimp's STS was a pre-cursor to its Mandrill transactional service and may only still work for user accounts that have existing STS campaigns. No new STS campaigns can be created. If you have a monthly, paid MailChimp account, you should look into Mandrill. If not, I've had good luck with Mailgun.
You should use HTTP Basic authentication in MailChimp API 3.0.
needle.get('https://<dc>.api.mailchimp.com/3.0/<endpoint>', { username: 'anystring', password: 'your_apikey' },
function(err, resp) {
// your code here...
});
EDIT
#TooMuchPete is right, the SendMail endpoint is not valid in MailChimp API v3.0. I didn't notice that and I've edited my answer.

Failed sending mail through google api in nodejs

I'm trying to send an email through Google API.
I'm using googleapis for Google API access in node.js .
My issue is that when I try to send a simple mail with no attachments, I get the following error:
'raw' RFC822 payload message string or uploading message via /upload/* URL required
I didn't define in my request that there is an attachment and I don't see any error in the email addresses.
Please help.
My code:
var google = require('googleapis');
var gmailClass = google.gmail('v1');
var email_lines = [];
email_lines.push("From: \"Some Name Here\" <rootyadaim#gmail.com>");
email_lines.push("To: hanochg#gmail.com");
email_lines.push('Content-type: text/html;charset=iso-8859-1');
email_lines.push('MIME-Version: 1.0');
email_lines.push("Subject: New future subject here");
email_lines.push("");
email_lines.push("And the body text goes here");
email_lines.push("<b>And the bold text goes here</b>");
var email =email_lines.join("\r\n").trim();
var base64EncodedEmail = new Buffer(email).toString('base64');
gmailClass.users.messages.send({
auth: OAuth2Client,
userId: "me",
message:
{
raw: base64EncodedEmail
}
},
function(err, results){});
Changes were made to version 1.0.3 of the google api. Try using the following syntax:
gmailClass.users.messages.send({
auth: OAuth2Client,
userId: "me",
resource:
{
raw: base64EncodedEmail
}
}
Make sure that base64EncodedEmail is url safe. You can use the base64EncodedEmail.replace(/\+/g, '-').replace(/\//g, '_') code posted by mscdex. This syntax worked for v. 1.0.11

Categories