commit 5d70fe1ba2828628319a4f4a41a50a55b9e9ed95 Author: alex Date: Sat Apr 19 20:23:37 2025 +0200 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2206544 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +.codegpt \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5a583f4 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "taipyStudio.gUI.elementsFilePaths": [] +} \ No newline at end of file diff --git a/QRcode.png b/QRcode.png new file mode 100644 index 0000000..14dfdae --- /dev/null +++ b/QRcode.png @@ -0,0 +1 @@ +{"success":false,"message":"qr code not ready or already scanned"} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/contacts.db b/contacts.db new file mode 100644 index 0000000..b177e8c Binary files /dev/null and b/contacts.db differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..552f954 --- /dev/null +++ b/main.py @@ -0,0 +1,6 @@ +def main(): + print("Hello from whatsapp-api!") + + +if __name__ == "__main__": + main() diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..0d9f219 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,10 @@ +[project] +name = "whatsapp-api" +version = "0.1.0" +description = "Add your description here" +readme = "README.md" +requires-python = ">=3.13" +dependencies = [ + "asyncio>=3.4.3", + "httpx>=0.28.1", +] diff --git a/send_daily_msg copy.py b/send_daily_msg copy.py new file mode 100644 index 0000000..85ccb34 --- /dev/null +++ b/send_daily_msg copy.py @@ -0,0 +1,43 @@ +import httpx +import asyncio +import sqlite3 +import json + +url = 'http://94.177.199.207:3000/client' +session = 'AssistenzaPubblicaBardiProd' + +async def fetch(client, url): + response = await client.get(url) + return response.json() + +def getContactList(): + api = f'{url}/getContacts/{session}' + with httpx.Client() as client: + response = client.get(api) + return response.json() + +async def main(): + contactList = getContactList() + + print(type(contactList)) + + ''' + async with httpx.AsyncClient() as client: + tasks = fetch(client, url) + results = await asyncio.gather(*tasks) + for result in results: + print(result) + ''' + for contact in contactList["contacts"]: + print("\nDettagli contatto:") + print(f"Numero: {contact.get('number', 'N/A')}") + print(f"Nome: {contact.get('name', 'N/A')}") + print(f"Pushname: {contact.get('pushname', 'N/A')}") + print(f"ShortName: {contact.get('shortName', 'N/A')}") + print(f"Server ID: {contact['id']['server']}") + print(f"User ID: {contact['id']['user']}") + print(f"serialized: {contact['id']['_serialized']}") + +if __name__ == '__main__': + asyncio.run(main()) + diff --git a/send_daily_msg.py b/send_daily_msg.py new file mode 100644 index 0000000..231df06 --- /dev/null +++ b/send_daily_msg.py @@ -0,0 +1,69 @@ +import httpx +import asyncio +import sqlite3 + +url = 'http://94.177.199.207:3000/client' +session = 'AssistenzaPubblicaBardiProd' +contactDB = 'contacts.db' +createContactsTable = [ + 'DROP TABLE IF EXISTS contacts;', + ''' + CREATE TABLE contacts ( + id INTEGER PRIMARY KEY, + name text NOT NULL, + number text NOT NULL, + pushname text NOT NULL, + shortName text NOT NULL, + serialized text NOT NULL + ); + ''' +] + +def createContactsDB(): + try: + with sqlite3.connect(contactDB) as conn: + cursor = conn.cursor() + for sql in createContactsTable: + cursor.execute(sql) + print("Tables created successfully.") + except sqlite3.OperationalError as e: + print("Failed to create tables:", e) + +async def fetch(client, url): + response = await client.get(url) + return response.json() + +def getContactList(): + api = f'{url}/getContacts/{session}' + with httpx.Client() as client: + response = client.get(api) + return response.json() + +def init_db(): + createContactsDB() + contactList = getContactList() + try: + with sqlite3.connect(contactDB) as conn: + for id, contact in enumerate([item for item in contactList["contacts"] if str(item['number']).startswith('39')]): + conn.execute(f'''INSERT INTO contacts (id, name, number, pushname, shortName, serialized) VALUES ({id}, + "{contact.get("name", "N/A")}", {contact.get("number")}, "{contact.get("pushname", "N/A")}", + "{contact.get("shortName", "N/A")}", "{contact["id"]["_serialized"]}")''') + conn.commit() + print("Tables loaded successfully.") + except sqlite3.OperationalError as e: + print("Failed to load table:", e) + +async def main(): + init_db() + ''' + async with httpx.AsyncClient() as client: + tasks = fetch(client, url) + results = await asyncio.gather(*tasks) + for result in results: + print(result) + ''' + + +if __name__ == '__main__': + asyncio.run(main()) + diff --git a/start_session.ps1 b/start_session.ps1 new file mode 100755 index 0000000..78123b4 --- /dev/null +++ b/start_session.ps1 @@ -0,0 +1,17 @@ +function getQRcode { + Invoke-WebRequest -Uri 'http://94.177.199.207:3000/session/qr/AssistenzaPubblicaBardiProd/image' -Method GET -Outfile QRcode.png + ./QRcode.png +} + +$response = Invoke-WebRequest -Uri 'http://94.177.199.207:3000/session/status/AssistenzaPubblicaBardiProd' -Method GET +if ($response.StatusCode -eq 200) { + $state = ConvertFrom-Json $response.Content + if ($state.state -eq "CONNECTED") { + getQRcode + } + elseif ($null -eq $state.state) { + $response = Invoke-WebRequest -Uri 'http://94.177.199.207:3000/session/restart/AssistenzaPubblicaBardiProd' -Method GET + Start-Sleep -Seconds 20 + } +} + diff --git a/uv.lock b/uv.lock new file mode 100644 index 0000000..40eb30f --- /dev/null +++ b/uv.lock @@ -0,0 +1,104 @@ +version = 1 +revision = 1 +requires-python = ">=3.13" + +[[package]] +name = "anyio" +version = "4.9.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "idna" }, + { name = "sniffio" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/95/7d/4c1bd541d4dffa1b52bd83fb8527089e097a106fc90b467a7313b105f840/anyio-4.9.0.tar.gz", hash = "sha256:673c0c244e15788651a4ff38710fea9675823028a6f08a5eda409e0c9840a028", size = 190949 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/a1/ee/48ca1a7c89ffec8b6a0c5d02b89c305671d5ffd8d3c94acf8b8c408575bb/anyio-4.9.0-py3-none-any.whl", hash = "sha256:9f76d541cad6e36af7beb62e978876f3b41e3e04f2c1fbf0884604c0a9c4d93c", size = 100916 }, +] + +[[package]] +name = "asyncio" +version = "3.4.3" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/da/54/054bafaf2c0fb8473d423743e191fcdf49b2c1fd5e9af3524efbe097bafd/asyncio-3.4.3.tar.gz", hash = "sha256:83360ff8bc97980e4ff25c964c7bd3923d333d177aa4f7fb736b019f26c7cb41", size = 204411 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/22/74/07679c5b9f98a7cb0fc147b1ef1cc1853bc07a4eb9cb5731e24732c5f773/asyncio-3.4.3-py3-none-any.whl", hash = "sha256:c4d18b22701821de07bd6aea8b53d21449ec0ec5680645e5317062ea21817d2d", size = 101767 }, +] + +[[package]] +name = "certifi" +version = "2025.1.31" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/1c/ab/c9f1e32b7b1bf505bf26f0ef697775960db7932abeb7b516de930ba2705f/certifi-2025.1.31.tar.gz", hash = "sha256:3d5da6925056f6f18f119200434a4780a94263f10d1c21d032a6f6b2baa20651", size = 167577 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/38/fc/bce832fd4fd99766c04d1ee0eead6b0ec6486fb100ae5e74c1d91292b982/certifi-2025.1.31-py3-none-any.whl", hash = "sha256:ca78db4565a652026a4db2bcdf68f2fb589ea80d0be70e03929ed730746b84fe", size = 166393 }, +] + +[[package]] +name = "h11" +version = "0.14.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f5/38/3af3d3633a34a3316095b39c8e8fb4853a28a536e55d347bd8d8e9a14b03/h11-0.14.0.tar.gz", hash = "sha256:8f19fbbe99e72420ff35c00b27a34cb9937e902a8b810e2c88300c6f0a3b699d", size = 100418 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/95/04/ff642e65ad6b90db43e668d70ffb6736436c7ce41fcc549f4e9472234127/h11-0.14.0-py3-none-any.whl", hash = "sha256:e3fe4ac4b851c468cc8363d500db52c2ead036020723024a109d37346efaa761", size = 58259 }, +] + +[[package]] +name = "httpcore" +version = "1.0.8" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "certifi" }, + { name = "h11" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/9f/45/ad3e1b4d448f22c0cff4f5692f5ed0666658578e358b8d58a19846048059/httpcore-1.0.8.tar.gz", hash = "sha256:86e94505ed24ea06514883fd44d2bc02d90e77e7979c8eb71b90f41d364a1bad", size = 85385 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/18/8d/f052b1e336bb2c1fc7ed1aaed898aa570c0b61a09707b108979d9fc6e308/httpcore-1.0.8-py3-none-any.whl", hash = "sha256:5254cf149bcb5f75e9d1b2b9f729ea4a4b883d1ad7379fc632b727cec23674be", size = 78732 }, +] + +[[package]] +name = "httpx" +version = "0.28.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "anyio" }, + { name = "certifi" }, + { name = "httpcore" }, + { name = "idna" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/b1/df/48c586a5fe32a0f01324ee087459e112ebb7224f646c0b5023f5e79e9956/httpx-0.28.1.tar.gz", hash = "sha256:75e98c5f16b0f35b567856f597f06ff2270a374470a5c2392242528e3e3e42fc", size = 141406 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/2a/39/e50c7c3a983047577ee07d2a9e53faf5a69493943ec3f6a384bdc792deb2/httpx-0.28.1-py3-none-any.whl", hash = "sha256:d909fcccc110f8c7faf814ca82a9a4d816bc5a6dbfea25d6591d6985b8ba59ad", size = 73517 }, +] + +[[package]] +name = "idna" +version = "3.10" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f1/70/7703c29685631f5a7590aa73f1f1d3fa9a380e654b86af429e0934a32f7d/idna-3.10.tar.gz", hash = "sha256:12f65c9b470abda6dc35cf8e63cc574b1c52b11df2c86030af0ac09b01b13ea9", size = 190490 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/76/c6/c88e154df9c4e1a2a66ccf0005a88dfb2650c1dffb6f5ce603dfbd452ce3/idna-3.10-py3-none-any.whl", hash = "sha256:946d195a0d259cbba61165e88e65941f16e9b36ea6ddb97f00452bae8b1287d3", size = 70442 }, +] + +[[package]] +name = "sniffio" +version = "1.3.1" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/a2/87/a6771e1546d97e7e041b6ae58d80074f81b7d5121207425c964ddf5cfdbd/sniffio-1.3.1.tar.gz", hash = "sha256:f4324edc670a0f49750a81b895f35c3adb843cca46f0530f79fc1babb23789dc", size = 20372 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/e9/44/75a9c9421471a6c4805dbf2356f7c181a29c1879239abab1ea2cc8f38b40/sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2", size = 10235 }, +] + +[[package]] +name = "whatsapp-api" +version = "0.1.0" +source = { virtual = "." } +dependencies = [ + { name = "asyncio" }, + { name = "httpx" }, +] + +[package.metadata] +requires-dist = [ + { name = "asyncio", specifier = ">=3.4.3" }, + { name = "httpx", specifier = ">=0.28.1" }, +]