Using config file

Using config file

A config file is a file that contains important credentials in it. Therefore, so that unwanted things don’t happen, this config file is created. Add tgsnake.config.js in .gitignore so that this file doesn’t get pushed to github.

tgsnake.config.js
/** @type {import('tgsnake').Options} */
const config = {
  apiHash: 'abcdefgh',
  apiId: 0123455,
  logLevel: 'error',
  plugins: [],
  clientOptions: {},
  login: {
    sessionName: 'myapp',
    forceDotSession: true,
    botToken: '',
  },
  experimental: {},
};
module.exports = config;

For the deno platform it may be a little different because deno uses ESM fully. Therefore the configuration needs to be adjusted to:

tgsnake.config.js
/** @type {import('tgsnake').Options} */
const config = {
  apiHash: 'abcdefgh',
  apiId: 0123455,
  logLevel: 'error',
  plugins: [],
  clientOptions: {},
  login: {
    sessionName: 'myapp',
    forceDotSession: true,
    botToken: '',
  },
  experimental: {}
};
export default config;

You only need to use 1 config file in your project. Make sure the config file is in the root directory of your project.

    • tgsnake.config.js
    • package.json
    • yarn.lock
  • If the configuration is not found and you did not create a configuration when creating the client, an error will appear.
    The code snippet above is an example of its implementation and the following is an explanation of each parameter.

    apiHash

    type: string
    When you create your app on my.telegram.org you will eventually get an api hash and an api id. Fill this section with the api hash you got.

    apiId

    type: number
    When you create your app on my.telegram.org you will eventually get an api hash and an api id. Fill this section with the api id you got.

    logLevel

    type: string | Array<string>
    Option to set the level of the log to be displayed on the terminal. You can fill it with an Array from the level log so you can manage 2 or more levels at once.
    By default it will read the env, read more

    plugins

    type: Array<Function> | Array<PluginObj>
    Collection of plugins that will be run according to the event handler. You must trust the plugin when you install it.
    Read guide to create plugin here

    clientOptions

    type: ClientOptions
    Additional options required to create a client. This option is the option that @tgsnake/core will use later. See »

    login

    type: LoginWithSession
    Option to complete the login action. The option must be an object that satisfies type.

    interface LoginWithSession {
      /**
       * String session or import your session class.
       */
      session?: string | Storages.AbstractSession;
      /**
       * Login as bot using bot auth token from bot father. <br/>
       * Only effected when selected session is blank.
       */
      botToken?: string;
      /**
       * Force session to .session file, If session field passed as string. <br/>
       *
       * default is true.
       */
      forceDotSession?: boolean;
      /**
       * The name of session, it will be using to read .session file. <br/>
       *
       * default is "tgsnake"
       */
      sessionName?: string;
    }

    session

    type: string | Storages.AbstractSession | undefined
    Session is a class that will be used to store login info. The session can be a string session or an instance of the Storages.AbstractSession class from @tgsnake/core.

    botToken

    type: string | undefined
    If you want to log in using the bot token you got from the Bot Father, you can fill in this section with the token.

    forceDotSession

    type: boolean | undefined
    This option will be used to create files ending in .session and .cache. The file name will match the given in sessionName. Default is true

    sessionName

    type: string | undefined
    The name of the current session that will later be used to login. Default is tgsnake

    experimental

    type: ExperimentalOptions
    Experimental option that may be unstable, we do not recommend using this option. You must bear every risk that might occur.

    paramsrequiredtypedescription
    alwaysOnlinefalsebooleanWhen a user is offline state, the user will automatically be forced to go back online. Default false.
    alwaysSyncfalsebooleanFor supergroups/channels that send messages too often so that after 2 minutes the client is running there are no more updates from Telegram. You can use this option to force the client to synchronize with predefined intervals and delays. Default false.
    customPathfalseCustomPathApply custom paths for some configurations such as login info and cache.
    onlineOnStartfalsebooleanWhen the new client is started, will it display a status user is online. Default false.
    shutdownfalsebooleanImplement the shutdown function by default into the client class. By default this option is true. Read shutdown guide.
    syncEveryfalsenumberCheck the channel every time given. If no updates are received after the ‘syncTimeout’ timeout it will call the getChannelDifference function for synchronization. Default 10000.
    syncTimeoutfalsenumberFor supergroups/channels that send messages too often so that after 2 minutes the client is running there are no more updates from Telegram. You can use this option to force the client to synchronize with predefined intervals and delays. Default 30000

    customPath

    paramsrequiredtypedescription
    loginDirfalsestringThe location or folder where all login information will be stored. This option only works when forceDotSession is active. And don’t use special sessions like RedisSession. Default is process.cwd()
    loginExtfalsestringFile extension of saved login info. By default the file extension is ‘session’. This option only works when forceDotSession is active. And don’t use special sessions like RedisSession. default is session
    cacheDirfalsestringThe location or folder where all cache will be stored. This option only works when forceDotSession is active. And don’t use special sessions like RedisSession. Default is process.cwd()
    cacheExtfalsestringFile extension of saved cache. By default the file extension is ‘cache’. This option only works when forceDotSession is active. And don’t use special sessions like RedisSession. Default is cache