Last active
February 27, 2022 13:11
-
-
Save VityaSchel/e4fea2fca5eb98eec7ec24bf0f51efea to your computer and use it in GitHub Desktop.
Telegram Bot working on Nginx FastCGI template (nodejs/javascript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Your logs will be printed here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/root/.nvm/versions/node/v16.13.0/bin/node | |
import fss from 'fs' // DO NOT change to fs/promises because 'The "path" argument must be of type string or an instance of Buffer or URL. Received type number (0)' | |
import fs from 'fs/promises' | |
const __dirname = new URL('.', import.meta.url).pathname | |
console.log('Content-type:text/plain') | |
console.log('') | |
async function processBody() { | |
const stdinBuffer = fss.readFileSync(process.stdin.fd) | |
const stdin = stdinBuffer.toString() | |
let body = {} | |
try { | |
body = JSON.parse(stdin) | |
} catch(e) { | |
return await fs.appendFile(`${__dirname}error.log`, `Error while parsing body: ${JSON.stringify(e, Object.getOwnPropertyNames(e))}\n`) | |
} | |
/* | |
replace anything after #! in top first line with output of `which node` | |
make calls to other functions/modules here with body variable | |
use this to log anything to error.log file in order to fix bugs: | |
return await fs.appendFile(`${__dirname}error.log`, '') | |
NEVER leave the script running for long time without exiting with code=0, | |
because FastCGI won't run again and you will have to reboot your whole server | |
i'm pretty sure you can improve code quality here so please someone | |
make fork or pr to this gist | |
*/ | |
} | |
await processBody() | |
console.log('OK') | |
process.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment