Skip to content

Commit

Permalink
make main bot kinda work
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Bekkiev committed Dec 5, 2021
1 parent 8481366 commit 524abfb
Show file tree
Hide file tree
Showing 15 changed files with 367 additions and 121 deletions.
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
To run admin panel, run docker image:

```sh
docker run --env-file=<env_file> -it --network tribalizm admin:1.0
docker run --env-file=<env_file> -it --network <tribalizm|host> admin:1.0
```

Network host is when developing locally.

## Development

NOTE: you have to read all environment variables, eg. with `set-env ./.env.develop` where `set-env`
Expand Down
90 changes: 71 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,79 @@
import express from 'express'
import { MongoClient } from 'mongodb'
import { createMongoContext, createMongoTelegramContext } from './mongo-context'
import { makeBot } from './plugins/ui/telegram/bot'
import { makeTribalizm } from './use-cases/tribalism'
import { Scheduler } from './use-cases/utils/scheduler'
import { TaskDispatcher } from './use-cases/utils/task-dispatcher'

const app = express()
app.use(express.json())
async function getDb() {
const { DB_USER, DB_HOST, DB_PASS } = process.env
const url = `mongodb://${DB_USER}:${DB_PASS}@${[DB_HOST]}:27017`
const client = new MongoClient(url, { useUnifiedTopology: true })
try {
await client.connect()

/** this one used to check server is running */
// TODO replace with smth form here: https://expressjs.com/en/advanced/healthcheck-graceful-shutdown.html
app.get('/ping', (req, res) => {
console.log('ping recieved')
res.end('pong')
})
const db = client.db('tribalizm')
await db.command({ ping: 1 })
return { db, client }
} catch (e) {
await client.close()
throw e
}
}

async function main() {
const { db, client } = await getDb()

const context = createMongoContext(db)

const { tgUsersAdapter, messageStore } = createMongoTelegramContext(
db,
context.stores.userStore
)

const tribalizm = makeTribalizm(context)

makeBot({
telegramUsersAdapter: null as any,
webHook: {
domain: 'tribalizm.rblab.net',
},
tribalism: null as any,
token: process.env.BOT_TOKEN,
notificationBus: null as any,
messageStore: null as any,
}).then((bot) => {
const bot = await makeBot({
telegramUsersAdapter: tgUsersAdapter,
messageStore: messageStore,
tribalizm: tribalizm,
token: process.env.BOT_TOKEN,
notificationBus: context.async.notificationBus,
})

const scheduler = new Scheduler(context.stores.taskStore)
const taskDispatcher = new TaskDispatcher(tribalizm, scheduler)

const app = express()
app.use(express.json())

/** this one used to check server is running */
// TODO replace with something form here:
// https://expressjs.com/en/advanced/healthcheck-graceful-shutdown.html
app.get('/ping', (req, res) => {
console.log('ping received')
res.end('pong')
})
app.use(bot.webhookCallback('/tg-hook'))
app.get('/check-queue', async (req, res) => {
await taskDispatcher.run()
res.end('ok')
})

const server = app.listen(3000, () => {
console.log('listening on 3000')
bot.telegram.setWebhook('https://tribalizm-1.rblab.net/tg-hook')
})

app.listen(3000)
process.once('SIGINT', stop)
process.once('SIGTERM', stop)
async function stop() {
server.close()
await client.close()
}
}
main().catch((e) => {
console.error(e)
process.exit(1)
})
59 changes: 32 additions & 27 deletions src/plugins/ui/i18n/en/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ const en: BaseTranslation = {
requestLocation: '🌍 Share my location',
apply: 'πŸš€ Send application',
count: 'Members count:',
searchIn: 'Searchin in {city}: ',
searchIn: 'Trines in {city}: ',
applyText:
'Please, write tribe\'s chief and shaman about yourself and why do you want to join "{tribe}" tribe?',
applicationSent:
'πŸ“¨ Your application has been sent. Tribe chief will propose a meeting soon.',
applicationSentShort: 'β˜‘οΈ Applied!',
cantFindCity: "Can't find your city, please type it's name."
cantFindCity: "Can't find your city, please type it's name.",
unknownCity: 'Cannot find such a city',
nothingFound: 'No tribes in {city} so far...',
},
elders: {
chief: 'chief',
Expand Down Expand Up @@ -182,35 +184,38 @@ const en: BaseTranslation = {

errors: {
// TODO fill other errors texts!!
UpdateFinishedBrainstormError: '',
SelfVotingIdeaError: '',
DoubleVotingError: '',
UpdateFinishedIdeaError: '',
InvalidTimeProposal: '',
InvalidAcceptanceTime: '',
NotYourQuest: '',
IndeclinableError: '',
QuestIncompleteError: '',
NotParticipated: '',
VoteRangeError: '',
SelfVotingError: '',
ApplicationTransitionError: '',
NoChiefTribeError: '',
EntityNotFound: '',
NotYourTribe: '',
NoIdeaError: '',
AlreadyHaveChief: '',
ElderMismatchError: '',
WrongQuestError: '',
UpdateFinishedBrainstormError: '🚫 This brainstorm is already over.',
SelfVotingIdeaError: '🚫 You cannot vote for your own idea.',
DoubleVotingError: 'Cannot vote twice.',
UpdateFinishedIdeaError:
'This idea already in implementation (or implemented).',
InvalidTimeProposal: 'It is impossible to meet at that time!',
InvalidAcceptanceTime: '🀬 InvalidAcceptanceTime? 🀨',
NotYourQuest: '🚫 Nope. This is not your quest.',
IndeclinableError: 'You cannot decline this type of quests.',
QuestIncompleteError:
'Cannot agree on quest with incomplete information.',
NotParticipated: "🚫 Nope. You cannot vote for gathering you' declined",
VoteRangeError: 'This vot is out of available range.',
SelfVotingError: '🚫 Nope. You cannot rate yourself',
ApplicationTransitionError: '🀬 ApplicationTransitionError? 🀨',
NoChiefTribeError: '🀨 Hmm... this tribe has no chief.',
EntityNotFound: '🀨 404. Not found. What did you try to find?',
NotYourTribe: '🚫 Nope. You are not from this tribe (any more?).',
NoIdeaError: '🀬 NoIdeaError? 🀨',
AlreadyHaveChief: '🀨 Hmm... this tribe already has a chief.',
ElderMismatchError: '🀬 ElderMismatchError? 🀨',
WrongQuestError: '🀬 WrongQuestError? 🀨',
NoChiefSetError: '🀨 Hmm... this tribe has no chief.',
NoShamanSetError: '',
NoShamanSetError: '🀨 Hmm... this tribe has no shaman.',
WrongPhaseError: '🚫 Sorry, you cannot change application process now.',
VotingNotStartedError: '',
ExternalMemberVoteError: '',
NotEnoughMembers: '',
VotingNotStartedError: '🚫 Voting is not started yet',
ExternalMemberVoteError:
'🚫 Nope. You are not from this tribe (any more?).',
NotEnoughMembers: '🀬 NotEnoughMembers? 🀨',
NotAChiefError:
'🚫 Sorry, you cannot start a brainstorm, only tribe chief can',
FinalyzeBeforeVotingError:
FinalizeBeforeVotingError:
'🀬 System error! Cannot finalize storm before voting',
StormNotStarted:
'🀬 System error! Cannot add idea: brainstorm is not raging',
Expand Down
Loading

0 comments on commit 524abfb

Please sign in to comment.