const Speech = require('@google-cloud/speech'); const speech = new Speech({ projectId: 'voicy-151205', credentials: require('path/to/certificate/file.json') }); speech.startRecognition(filepath, { 'encoding': 'LINEAR16', 'sampleRate': 16000, 'languageCode': 'en-US', }) .then((results) => { const operation = results[0]; return operation.promise(); }) .then((transcription) => { console.log(transcription[0]); })
const ffmpeg = require('fluent-ffmpeg'); const temp = require('temp'); ffmpeg.ffprobe(filepath, (err, info) => { const fileSize = info.format.duration; const output = temp.path({ suffix: '.flac' }); ffmpeg() .on('end', () => console.log(output)) .input(filepath) .setStartTime(0) .duration(fileSize) .output(output) .audioFrequency(16000) .toFormat('s16le') .run(); });
<!DOCTYPE html> <html> <head> <title>Voicy payments</title> <!-- Bootstrap, jQuery, global.js, style.css Stripe--> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="/javascripts/global.js"></script> <script src="https://checkout.stripe.com/checkout.js"></script> <link rel='stylesheet' href='/stylesheets/style.css' /> </head> <body> <!-- -, id , --> <img src="/images/logo.png" alt="Voicy" class="center-block"> <h1 class="center-block text-center">Chat ID:</h1> <h1 class="center-block text-center">{{ chatId }}</h1> <p class="text-center">{{ seconds }} seconds are left in this chat.</p> <p class="text-center">You can buy more seconds below.</p> <p class="text-center"><b>$0.4 per 200 seconds</b></p> <form> <div class="center"> <!-- — , --> <form class="form-inline" id="buy"> <div class="form-group"> <!--, , chatId --> <input type="hidden" name="chatId" value="{{ chatId }}"> <!-- , , --> <input type="number" class="form-control" name="numberOfSeconds" placeholder="Enter number of seconds"> <!-- — info, error success, global.js--> <small id="infoLabel" class="form-text text-info"></small> <small id="errorLabel" class="form-text text-danger"></small> <small id="successLabel" class="form-text text-success"></small> </div> <button type="submit" class="btn btn-primary center-block" id="buyButton">Buy</button> </form> </form> </form> </body> </html>
// $(document).ready(function() { // — , var chatId; var amount; // Stripe Checkout var handler = StripeCheckout.configure({ key: '***', image: 'https://pay.voicybot.com/images/stripe.png', locale: 'auto', // alipay: true, // , - AliPay // bitcoin: true, // — US closed: function() { // , info, stripe $("#successLabel").empty(); $("#errorLabel").empty(); }, token: function(token) { // , token // (, — ) $("#infoLabel").empty(); $("#successLabel").empty(); $("#errorLabel").empty(); // , $("#infoLabel").append('Processing payment on Voicy servers...'); // , index.js $.ajax({ type: 'POST', url: 'buy', data: { 'token': token.id, 'chatId': chatId, 'amount': amount }, dataType: 'json', encode: true }) .done(function(data) { // : — ""; — if (data['error']) { $("#infoLabel").empty(); $("#successLabel").empty(); $("#errorLabel").empty(); $("#errorLabel").append(data['error']); } else { $("#infoLabel").empty(); $("#successLabel").empty(); $("#errorLabel").empty(); $("#successLabel").append('Thank you for the payment!'); } }); } }); // Stripe, , window.addEventListener('popstate', function() { $("#infoLabel").empty(); $("#successLabel").empty(); $("#errorLabel").empty(); handler.close(); }); // — "Buy" $('form').submit(function(event) { event.preventDefault(); // var seconds = $('input[name=numberOfSeconds]').val(); // chatId = $('input[name=chatId]').val(); // chat id // — 200 , if (!seconds || seconds < 200) { $("#infoLabel").empty(); $("#successLabel").empty(); $("#errorLabel").empty(); $("#errorLabel").append('Please purchase at least 200 seconds'); } else { // — ... var purch = seconds * 0.002 * 100; amount = seconds; $("#infoLabel").empty(); $("#successLabel").empty(); $("#errorLabel").empty(); $("#infoLabel").append('Please pay at Stripe Checkout'); // ... Stripe handler.open({ name: 'Voicy Bot', description: 'Purchasing ' + seconds + ' seconds', currency: 'USD', amount: purch, // alipay: true, // bitcoin: true }); } }); });
// Express router HTTP jQuery const express = require('express'); const router = express.Router(); // db — , const db = require('../helpers/db'); // Stripe — const stripe = require("stripe")("***"); /** */ router.post('/buy', (req, res, next) => { // token stripe, const token = req.body.token; // id , const chatId = parseInt(req.body.chatId); // , const amount = parseInt(req.body.amount); var charge = stripe.charges.create({ amount: amount * 0.002 * 100, // source: token, currency: "USD", description: "Buying seconds for Voicy" }, (err, charge) => { if (err) { res.send({ error: err.message }); // jQuery } else { db.findChat(chatId) // id .then((chat) => { // , "" chat.seconds = parseInt(chat.seconds) + amount; // return chat.save() // .then((newChat) => { res.send({ success: true }); // "" jQuery }); }) .catch((err) => { res.send({ error: err.message }); // jQuery }) } }); }); /* */ router.get('/:id', (req, res, next) => { const chatId = parseInt(req.params.id); // id , db.findChat(chatId) // id .then((chat) => { // ? 404! if (!chat) { const err = new Error(); err.status = 404; err.message = 'No chat found'; throw err; } // ? ! return chat; }) .then((chat) => { // , id res.render('index', { chatId: chat.id, seconds: chat.seconds, }); }) .catch(err => next(err)); // ( ) });
# HTTP - HTTPS: server { listen 80; listen [::]:80 default_server ipv6only=on; return 301 https://$host$request_uri; } # HTTPS - Node.js : server { listen 443; server_name your_domain_name; ssl on; # Lets Encrypt: ssl_certificate /etc/letsencrypt/live/pay.voicybot.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/pay.voicybot.com/privkey.pem; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers '***'; # localhost:3001: location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true; proxy_pass http://localhost:3000/; proxy_ssl_session_reuse off; proxy_set_header Host $http_host; proxy_cache_bypass $http_upgrade; proxy_redirect off; } }
Source: https://habr.com/ru/post/316824/
All Articles