Search code examples
pythongoogle-cloud-functionstelegramtelethon

How to use Telethon with Google Cloud Functions


I'm new with python and i want to se a google cloud function with Telethon but i can't do it because i have errors when running.

Here is Gcloud function code my code:

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__;

My requeriments.txt:

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

And i get the following error when i run the function (hello_world):

python google cloud error on running


Solution

  • The Name and location of your Database is the session_name. On Google Cloud functions only /tmp is writeable.

    It has to look like this:

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