File: assistant.py

package info (click to toggle)
python-telethon 1.40.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,508 kB
  • sloc: python: 16,125; javascript: 200; makefile: 16; sh: 11
file content (70 lines) | stat: -rw-r--r-- 1,736 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
This file is only the "core" of the bot. It is responsible for loading the
plugins module and initializing it. You may obtain the plugins by running:

    git clone https://github.com/Lonami/TelethonianBotExt plugins

In the same folder where this file lives. As a result, the directory should
look like the following:

    assistant.py
    plugins/
        ...
"""
import asyncio
import os
import sys
import time

from telethon import TelegramClient

try:
    # Standalone script assistant.py with folder plugins/
    import plugins
except ImportError:
    try:
        # Running as a module with `python -m assistant` and structure:
        #
        #     assistant/
        #         __main__.py (this file)
        #         plugins/    (cloned)
        from . import plugins
    except ImportError:
        print('could not load the plugins module, does the directory exist '
              'in the correct location?', file=sys.stderr)

        exit(1)


def get_env(name, message, cast=str):
    if name in os.environ:
        return os.environ[name]
    while True:
        value = input(message)
        try:
            return cast(value)
        except ValueError as e:
            print(e, file=sys.stderr)
            time.sleep(1)


API_ID = get_env('TG_API_ID', 'Enter your API ID: ', int)
API_HASH = get_env('TG_API_HASH', 'Enter your API hash: ')
TOKEN = get_env('TG_TOKEN', 'Enter the bot token: ')
NAME = TOKEN.split(':')[0]


async def main():
    bot = TelegramClient(NAME, API_ID, API_HASH)

    await bot.start(bot_token=TOKEN)

    try:
        await plugins.init(bot)
        await bot.run_until_disconnected()
    finally:
        await bot.disconnect()


if __name__ == '__main__':
    asyncio.run(main())