添加要检查的新变量后,Python Firestore事件侦听器停止工作

bakd9h0s  于 2023-02-11  发布在  Python
关注(0)|答案(1)|浏览(104)

此脚本背后的想法是侦听注册到我的Web应用程序的新用户,对于他们的签名过程,我添加了名为“newUser”的变量,默认情况下将其保留为True。我检查该用户是否是新用户(在“如果新用户== 'True',我是说True是一个字符串,因为在我使用get()和to_dict之前的一行()方法来提取“True”,我认为它是一个字符串)。然后脚本继续,找到新用户的名字和新用户的电话号码,并将它们发送到json,然后发送到一个webhook。直到昨天我的脚本和webhook还在fin工作,在我检查这个名为“newUser”的新变量之前,在脚本结束时,我将变量“newUser”更改为False。
顺便说一句:我的firestore集合叫做“users”,每个用户的文档都有一个自动生成的ID,然后是子集合,其中有两个我感兴趣的Map,一个是“info”,其中包含用户的个人详细信息,如FirstName和Phone Number,另一个是这个main,其中newUser是并设置为True(默认情况下)。
我不确定脚本的逻辑是否正确,我检查了我的webhook和它的侦听没有问题,firestore触发器正在运行,没有错误,但似乎webhook没有被触发。
有人知道吗?

#Required Libraries
import time
import json 
import requests 
import firebase_admin
from firebase_admin import firestore, credentials, threading

#Connection Details
cred = credentials.Certificate("bondupProd.json")
firebase_admin.initialize_app(cred)
print("Connection Established Successfully!")

#Access to Firestore
db = firestore.client()
print("Connection to Firestore DB Succesfully!")

#Create an event for notifying main thread
callback_done = threading.Event()

#Create function to check if newUser is True
def on_snapshot(col_snapshot, changes, read_time):
    for change in changes:
        if (change.type.name == 'ADDED') or (change.type.name == 'MODIFIED'):

            new_user = db.collection(u'users').document(
                change.document.id).get(
                    {u'main.newUser'}).to_dict()['main']['newUser']

            if new_user == 'True':

                firstName=db.collection(u'users').document(
                    change.document.id).get(
                        {u'info.firstName'}).to_dict()['info']['firstName']

                userPhone=db.collection(u'users').document(
                    change.document.id).get(
                        {u'info.phone'}).to_dict()['info']['phone']

                print(firstName, userPhone)

                msgBird="https://flows.messagebird.com/flows/...." (webhook)

                data = { "firstName": firstName,"userPhone": userPhone }

                requests.post(msgBird,data=json.dumps(data),headers={"MessageBird-Signature-JWT":"sl9zzUwEAVgJlu0qhDIP8gzu0iKy9amR", "Content-Type":"application/json"})

                #Update newUser to False
                db.collection(u'users').document(u'{usersId}').update({'newUser': False})

# Watch the collection query
col_query = db.collection(u'users').where(u'main', u'array_contains', u'newUser')
query_watch = col_query.on_snapshot(on_snapshot)

while True:
    time.sleep(1)
    print("Script running!")

解决方案?需要改变什么?

vmdwslir

vmdwslir1#

我之所以说True是一个字符串,是因为在我使用get()和to_dict()方法提取“True”之前的一行,我认为它是一个字符串
你的错误可能就在这里。
Firestore中的布尔值(就像每个JSON中的一样)在Python中仍然是布尔值,而不是字符串,对于任何用来检索值的方法都是如此:doc_snapshot.to_dict()["key"]将提供与doc_snapshot.get("key")相同的类型。
这意味着当你写.update({'newUser': False})时,字段实际上等于False而不是"False",如果出于某种原因你想要一个字符串,写.update({'newUser': "False"})
我们看不到创建文档时将字段'newUser'设置为True的代码,但很可能它是一个布尔值,因此您必须将检查更改为

if new_user == True:

相关问题