Last active
August 6, 2023 04:28
-
-
Save theostrich/5b28e603fbeece4deaaca909e82c6374 to your computer and use it in GitHub Desktop.
Telegram User Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#The PyMongo distribution contains tools for interacting with MongoDB database from Python | |
import pymongo | |
#def adduser(update, context): | |
# db(update) | |
def db(data): | |
DATABASE_NAME = '' #Your Database Name here. | |
myclient = pymongo.MongoClient() | |
database = myclient[DATABASE_NAME] | |
userid = data.message.chat.id | |
chattype = data.message.chat.type | |
if chattype == 'private': | |
collection = database["users"] | |
result = collection.find_one({'userid': userid}) | |
try: | |
result['userid'] | |
userexist = True | |
except: | |
userexist = False | |
username = data.message.chat.username | |
firstname = data.message.chat.first_name | |
lastname = data.message.chat.last_name | |
user = {} | |
user['userid'] = userid | |
user['chattype'] = chattype | |
user['username'] = username | |
user['firstname'] = firstname | |
user['lastname'] = lastname | |
if (userexist == False): | |
collection.insert_one(user) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment