【问题标题】:How to use Telethon with Google Cloud Functions如何将 Telethon 与 Google Cloud Functions 一起使用
【发布时间】:2021-07-13 00:47:56
【问题描述】:

我是 python 新手,我想用Telethon 设置一个谷歌云功能,但我不能这样做,因为我在运行时出错。

这是我的Gcloud功能代码:

from telethon import TelegramClient, events, sync
#from telethon import functions, types
#from telethon.tl.functions.channels import CreateChannelRequest, CheckUsernameRequest, UpdateUsernameRequest, InviteToChannelRequest
#from telethon.tl.types import InputChannel, InputPeerChannel

api_id = XXX
api_hash = 'XXX'

def hello_world(request):
    with TelegramClient("session_name", api_id, api_hash) as client:
        from telethon.tl.functions.channels import CreateChannelRequest, CheckUsernameRequest, UpdateUsernameRequest
        from telethon.tl.types import InputChannel, InputPeerChannel
        from telethon.tl.types import InputPhoneContact
        from telethon.tl.types import InputPeerUser
        from telethon.tl.functions.contacts import ImportContactsRequest

        contact = InputPhoneContact(client_id=11111, phone='1111111111', first_name="Twilo Test",last_name="Phone")

        newcontact = client(ImportContactsRequest(contacts=[contact]))

        return newcontact.__dict__;

我的 requeriments.txt:

# Function dependencies, for example:
# package>=version
telethon

当我运行函数 (hello_world) 时出现以下错误:

【问题讨论】:

  • 我已尝试重现此问题,但没有收到任何错误。如果您查看错误,它会显示 sqlite3.DatabaseError,但您没有提及数据库以及如何使用它,请编辑您的问题以在代码中显示数据库的使用情况。
  • 嗨@ZeenathSN 感谢您的回答。这是我在 Google Cloud Function 中的所有代码。你用了谷歌云功能吗?也许 Telethon 使用数据库!在我本地的情况下,代码运行良好。
  • 是的,我在云功能中做到了。您能否参考此文档再次尝试创建函数:cloud.google.com/functions/docs/first-python 并告诉我们您在哪一步收到此错误?

标签: python google-cloud-functions telegram telethon


【解决方案1】:

您的数据库的名称和位置是session_name。 在 Google Cloud 函数上,只有 /tmp 是可写的。

它必须看起来像这样:

with TelegramClient("/tmp/session_name", api_id, api_hash) as client:

【讨论】:

    猜你喜欢
    • 2019-05-18
    • 2020-11-11
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    相关资源
    最近更新 更多