Skip to content

Commit

Permalink
update README.md and example file for development run
Browse files Browse the repository at this point in the history
  • Loading branch information
danbilokha committed Apr 19, 2020
1 parent 64b41a4 commit da02c4a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 28 deletions.
13 changes: 9 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@ on Windows 10, x64 machine
- Open <a href='https://t.me/botfather'>BotFather</a>,
- Create your development version of the bot (for your local testing purpose) via `/newbot` command
- Receive from <a href='https://t.me/botfather'>BotFather</a> Key & Copy it,
- Create `.env` in `/server/src/bots/telegram` file and add received **key** there (<a href='https://github.com/danbilokha/covid19liveupdates/tree/master/server/src/bots/telegram'>more about it, example of the file</a>),
- Create `.env` in `/server/src/bots/telegram` file and add received **key** there (<a href='https://github.com/danbilokha/covid19liveupdates/tree/master/server/src/environments'>more about it, example of the file</a>),
- Add to the project,
- run `npm i`
- run `npm run start:watch`
- run `npm run start:watch`, `npm run start:inspect` for debugging
(<a href='https://medium.com/the-node-js-collection/debugging-node-js-with-google-chrome-4965b5f910f4'>Useful link</a>)

<b>Hints</b>
- If you\'re running locally, you can run `ngrok http 3000` copy `https url`
paste to `APP_URL` <a href='https://github.com/danbilokha/covid19liveupdates/tree/master/server/src/environments'>
env variable</a> and reloading will be faster and cheaper for CPU

##### If any questions and/or issues,
- contact any collaborator,
Expand All @@ -71,6 +76,6 @@ on Windows 10, x64 machine

### Restrictions and Limitations
For running the application we need to have secure connection (as some all known messengers
do not support running via http) thus we are using **ngrok** library for that currently. It
creates public domain with secured connection with we are using for redirecting all messages
do not support running via http) thus we are using <a href='https://github.com/bubenshchykov/ngrok'>**ngrok**</a> library
for that, currently. It creates public domain with secured connection with we are using for redirecting all messages
to our local machines.
15 changes: 8 additions & 7 deletions server/src/bots/telegram/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { Country } from '../../models/country.models';
import { flag } from 'country-emoji';
import { assistantStrategyResponse } from './botResponse/assistantResponse';
import * as TelegramBot from 'node-telegram-bot-api';
import Config from '../../environments/environment';
import { logger } from '../../utils/logger';
import { startResponse } from './botResponse/startResponse';
import { showAvailableCountriesResponse } from './botResponse/availableResponse';
Expand All @@ -42,14 +41,18 @@ import { catchAsyncError } from '../../utils/catchError';
import { LogglyTypes } from '../../models/loggly.models';
import { getErrorMessage } from '../../utils/getErrorMessages';

function runTelegramBot(app: Express, appUrl: string) {
export function runTelegramBot(
app: Express,
appUrl: string,
telegramToken: string
) {
// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(Config.TELEGRAM_TOKEN, { polling: true });
const bot = new TelegramBot(telegramToken, { polling: true });
// This informs the Telegram servers of the new webhook
bot.setWebHook(`${appUrl}/bot${Config.TELEGRAM_TOKEN}`);
bot.setWebHook(`${appUrl}/bot${telegramToken}`);

// We are receiving updates at the route below!
app.post(`/bot${Config.TELEGRAM_TOKEN}`, (req, res) => {
app.post(`/bot${telegramToken}`, (req, res) => {
bot.processUpdate(req.body);
res.sendStatus(200);
});
Expand Down Expand Up @@ -161,5 +164,3 @@ function runTelegramBot(app: Express, appUrl: string) {
bot.on('webhook_error', (err) => logger.log('error', err));
bot.on('error', (err) => logger.log('error', err));
}

export { runTelegramBot };
25 changes: 13 additions & 12 deletions server/src/environments/.env.example
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#env example file
COUNTRIESDATA_URL='https://pomber.github.io/covid19'
KNOWLEDGEBASE_URL=''
KNOWLEDGEBASE_SECRET_KEY='JWT token for knowledgebase'
TELEGRAM_TOKEN='Token from BotFather'
CONTAINER_VERSION=''
LOGGLY_TOKEN='Token for loggy'
CONTAINER_VERSION='For information purposes'
LOGGLY_TOKEN='Logs token'
LOGGLY_SUBDOMAIN='covid19liveupd'
LOGGLY_TAGS='Setup in Heroku interface'
APP_URL='Setup in Heroku interface'
NGROK_URL='Automatically setup for development purposes'
FIREBASE_API_KEY='firebase'
FIREBASE_AUTHDOMAIN='firebase'
FIREBASE_DATABASE_URL='firebase'
FIREBASE_PROJECT_ID='firebase'
FIREBASE_STORAGE_BUCKET='firebase'
FIREBASE_MESSAGING_SENDER_ID='firebase'
FIREBASE_APP_ID='firebase'
LOGGLY_TAGS='Logs, setup in Heroku interface'
APP_URL='If you\'re running locally, you can run `ngrok http 3000` cope past https url here and reloading will be faster and cheaper for CPU'
NGROK_URL='Automatically setup for development purposes, should be empty for development purposes'
FIREBASE_API_KEY="AIzaSyAHLUsFC7krb4cPctWeJYAGufQi6cpnlRU"
FIREBASE_AUTHDOMAIN="covid19liveupdates-dev.firebaseapp.com"
FIREBASE_DATABASE_URL="https://covid19liveupdates-dev.firebaseio.com"
FIREBASE_PROJECT_ID="covid19liveupdates-dev"
FIREBASE_STORAGE_BUCKET="covid19liveupdates-dev.appspot.com"
FIREBASE_MESSAGING_SENDER_ID="476819025765"
FIREBASE_APP_ID="1:476819025765:web:5e918bbb729e261a937b8c"
7 changes: 7 additions & 0 deletions server/src/environments/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
To run either for development, demo or production you need setup a few environment variables.
<a href='./.env.example'>Example file</a>.

If you believe that you should have <b>-demo</b> and/or <b>production</b>
env's, do reach out <a href='https://github.com/Proladge'>Vladimir</a>
or <a href='https://github.com/danbilokha'>Danylo</a>. Another option is to
take a look on Heroku.
21 changes: 16 additions & 5 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { runTelegramBot } from './bots/telegram';
import { runNgrok, stopNgrok } from './runNgrok';
import environments from './environments/environment';
import { initFirebase } from './services/infrastructure/firebase';
import { CONSOLE_LOG_DELIMITER, CONSOLE_LOG_EASE_DELIMITER } from './models/constants';
import {
CONSOLE_LOG_DELIMITER,
CONSOLE_LOG_EASE_DELIMITER,
} from './models/constants';
import * as firebase from 'firebase';
import { checkCovid19Updates } from './services/infrastructure/scheduler';
import { catchAsyncError } from './utils/catchError';
Expand All @@ -21,14 +24,22 @@ app.get('/', baseController.base);
const server = app.listen(PORT, async () => {
const appUrl = environments.APP_URL;
// tslint:disable-next-line:no-console
console.log('App is running at http://localhost:%d in %s mode', PORT, environmentName);
console.log(
'App is running at http://localhost:%d in %s mode',
PORT,
environmentName
);

if (environments.IsNgRokMode()) {
const [err, appUrl] = await catchAsyncError(
environments.NGROK_URL ? Promise.resolve(environments.NGROK_URL) : runNgrok(PORT)
environments.NGROK_URL
? Promise.resolve(environments.NGROK_URL)
: runNgrok(PORT)
);
// tslint:disable-next-line:no-console
console.log(`${CONSOLE_LOG_EASE_DELIMITER} NGROK started on ngRokUrl: ${appUrl}`);
console.log(
`${CONSOLE_LOG_EASE_DELIMITER} NGROK started on ngRokUrl: ${appUrl}`
);
}

const [e, isFirebaseInit] = initFirebase(environments);
Expand All @@ -42,7 +53,7 @@ const server = app.listen(PORT, async () => {
checkCovid19Updates();
// tslint:disable-next-line:no-console
console.log(`${CONSOLE_LOG_DELIMITER}Starting Telegram bot`);
runTelegramBot(app, appUrl);
runTelegramBot(app, appUrl, environments.TELEGRAM_TOKEN);
});

process.on('SIGTERM', () => {
Expand Down

0 comments on commit da02c4a

Please sign in to comment.