You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.7 KiB

  1. import express from 'express'
  2. import cors from 'cors'
  3. import bodyParser from 'body-parser'
  4. import amqp from 'amqplib'
  5. // import nodemailer from 'nodemailer'
  6. import SibApiV3Sdk from 'sib-api-v3-sdk'
  7. import dotenv from 'dotenv'
  8. dotenv.config()
  9. const app = express()
  10. const queueName = process.env.QUEUE_NAME
  11. const senderEmail = process.env.SENDER_EMAIL
  12. // const mailTransporter = nodemailer.createTransport({
  13. // service: 'gmail',
  14. // auth: {
  15. // user: process.env.TRANS_NAME,
  16. // pass: process.env.TRANS_PASS
  17. // }
  18. // })
  19. var defaultClient = SibApiV3Sdk.ApiClient.instance
  20. var apiKey = defaultClient.authentications['api-key']
  21. apiKey.apiKey = process.env.SENDING_BLUE_API_KEY
  22. var apiInstance = new SibApiV3Sdk.TransactionalEmailsApi()
  23. var sendSmtpEmail = new SibApiV3Sdk.SendSmtpEmail()
  24. app.use(cors())
  25. app.use(bodyParser.json())
  26. app.get('/', (_req, res) => {
  27. res.status.send(200).send("Amigo Receipt Sender Service Homepage!")
  28. })
  29. amqp.connect(process.env.AMQP_SERVER).then(async conn=> {
  30. const ch = await conn.createChannel()
  31. const queue = ch.assertQueue(queueName, { durable: true })
  32. if (queue) {
  33. queue.then(() => {
  34. return ch.consume(queueName, async (msg) => {
  35. var messageBody = JSON.parse(msg.content.toString())
  36. console.log(`[*] Message Received! email : ${messageBody.email} and pesan : ${messageBody.html}`)
  37. // sendReceipt(messageBody.email, messageBody.html)
  38. }, { noAck: true })
  39. }).then(() => {
  40. console.log('* Waiting for messages from queue ' + queueName)
  41. })
  42. }
  43. }).catch(console.warn)
  44. function sendReceipt(recepientEmail, receipt) {
  45. var date = new Date()
  46. sendSmtpEmail = {
  47. sender: {email: senderEmail, name: 'ReceiptSenderAutomation'},
  48. to: [{email: recepientEmail, name: 'Customer'}],
  49. subject: "test",
  50. textContent: "testing",
  51. htmlContent: receipt
  52. }
  53. apiInstance.sendTransacEmail(sendSmtpEmail).then(function(data) {
  54. console.log('API called successfully. Returned data: ' + data)
  55. console.log(date.toLocaleString())
  56. }, function(error) {
  57. console.error(error)
  58. })
  59. // ============ GMAIL's ============
  60. // var nodemailerOptions = {
  61. // from: process.env.SENDER_EMAIL,
  62. // to: receiverEmail,
  63. // subject: "Nota " + date.toLocaleDateString(),
  64. // text: receiptToSend
  65. // }
  66. // mailTransporter.sendMail(nodemailerOptions, function(err, info) {
  67. // if (err) {
  68. // console.log(err.message)
  69. // }
  70. // if (info) {
  71. // console.log(`Email sent!` + " | " +today.toLocaleTimeString("id"))
  72. // }
  73. // })
  74. }
  75. // sendReceipt("michael.pandu270@gmail.com", "Berikut detail nota terbaru anda")