Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
1.8 kB
2
Indexable
Never
const Telegraf = require('telegraf')
const path = require('path')
const TelegrafI18n = require('telegraf-i18n')
const { MongoClient } = require('mongodb');
const { session } = require('telegraf-session-mongodb');

// i18n options
const i18n = new TelegrafI18n({
    directory: path.resolve(__dirname, 'locales'),
    defaultLanguage: 'en',
    sessionName: 'session',
    useSession: true,
    templateData: {
        pluralize: TelegrafI18n.pluralize,
        uppercase: (value) => value.toUpperCase()
    }
})

const bot = new Telegraf('1917784212:AAHZV8664ATnnrMwjf0TRl6rDSgSQ9TikeY')

const mongo = async () => {
    const client = await MongoClient.connect('mongodb://localhost:27017/newBot', { useNewUrlParser: true, useUnifiedTopology: true })
    const db = client.db();
    bot.use(session(db, { collectionName: 'sessions' }));
    bot.use(i18n.middleware())
    // Start message handler
    bot.start(({ i18n, replyWithHTML }) => replyWithHTML(i18n.t('infa')))

    // Set locale to `en`
    bot.hears('en', ({ i18n, replyWithHTML }) => {
        i18n.locale('en-US')
        return replyWithHTML(i18n.t('changeLanguage'))
    })

    // Set locale to `ru`
    bot.hears('ru', ({ i18n, replyWithHTML }) => {
        i18n.locale('ru')
        return replyWithHTML(i18n.t('changeLanguage'))
    })

    bot.hears('am', ({ i18n, replyWithHTML }) => {
        i18n.locale('am')
        return replyWithHTML(i18n.t('changeLanguage'))
    })

    bot.hears('fr', ({ i18n, replyWithHTML }) => {
        i18n.locale('fr')
        return replyWithHTML(i18n.t('changeLanguage'))
    })

    bot.hears('infa', ({ i18n, replyWithHTML }) => {
        return replyWithHTML(i18n.t('infa'))
    })
}

mongo()

bot.startPolling()