setup bot connection to telegram

This commit is contained in:
David Todd 2020-01-19 17:45:21 -06:00
parent a356c50ce0
commit 6979c88d14
1 changed files with 13 additions and 0 deletions

View File

@ -10,6 +10,8 @@
import datetime import datetime
import sqlite3 import sqlite3
import uuid import uuid
import os
from telethon.sync import TelegramClient
from .db import Database from .db import Database
class TelethonDatabase(Database): class TelethonDatabase(Database):
@ -27,6 +29,17 @@ class TelethonDatabase(Database):
""" """
super(TelethonDatabase, self).__init__(database) super(TelethonDatabase, self).__init__(database)
# Load in the API tokens required for a bot
api_id = os.environ.get('telethon_api_id', False)
api_hash = os.environ.get('telethon_api_hash', False)
api_token = os.environ.get('telethon_token', False)
# Create an instance of the Telethon bot
if api_id and api_hash and api_token:
self.bot = TelegramClient('bot', api_id, api_hash).start(bot_token=api_token)
else:
self.bot = False
# Automatically create the table if it doesn't # Automatically create the table if it doesn't
# already exist in the selected database # already exist in the selected database
self._create_table() self._create_table()