Skip to content

Instantly share code, notes, and snippets.

@mage1k99
Last active June 19, 2021 11:01
Show Gist options
  • Save mage1k99/6cfe2627902ac16f18bad578159ce6c4 to your computer and use it in GitHub Desktop.
Save mage1k99/6cfe2627902ac16f18bad578159ce6c4 to your computer and use it in GitHub Desktop.
db_python
#The PyMongo distribution contains tools for interacting with MongoDB database from Python
import pymongo
import urllib.parse
#def adduser(update, context):
# db(update)
def db(data):
user = "username"
pwd = urllib.parse.quote("password")
DATABASE_NAME = '' #Your Database Name here.
myclient = pymongo.MongoClient("mongodb+srv://{}:{}@<cluser>".format(user, pwd))
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