tuberboy / facebook

facebook private api - auto create fb account, like, follow, video views etc all
MIT License
15 stars 3 forks source link

[suggestion] facebook auto-reply functionality #2

Open Skuxblan opened 4 months ago

Skuxblan commented 4 months ago

There's not a lot liblaries that is constantly updated and this one is preety new. I've been resarching for hours for such bot but all projects are outdated since few years ago facebook made some changes with messenger. so if suggestions are allowed I'd like to suggest auto-replies to threads function. So the program must have basic functions like: get 10 recent unread_threads by their id responsd to friend by set message. Also mark thread as read after sucesfully sending a message. Any feedback would be appreciate thank you :)

tuberboy commented 4 months ago

i will add messenger codes then use as you needed

Skuxblan commented 4 months ago

i will add messenger codes then use as you needed

Great, can't wait for it. Thank you

wezabronsz commented 3 months ago

I also would love to see auto reply bot. I'm trying to develop my own. I already can get threads ids by one of graphql request that response with such data, but I don't know how sending message works here. Anybody can help? I think it's probably done through websocket but I'm not 100% sure.

tuberboy commented 3 months ago

I also would love to see auto reply bot. I'm trying to develop my own. I already can get threads ids by one of graphql request that response with such data, but I don't know how sending message works here. Anybody can help? I think it's probably done through websocket but I'm not 100% sure.

messenger app uses websocket and xmpp like client to communication (chat) both side (server/client). can be done auto system using messenger web api easily, but todo using private api of apk file will take times...

I will share web api at this time, after some days, will share messenger app private api.

Skuxblan commented 3 months ago

I also would love to see auto reply bot. I'm trying to develop my own. I already can get threads ids by one of graphql request that response with such data, but I don't know how sending message works here. Anybody can help? I think it's probably done through websocket but I'm not 100% sure.

messenger app uses websocket and xmpp like client to communication (chat) both side (server/client). can be done auto system using messenger web api easily, but todo using private api of apk file will take times...

I will share web api at this time, after some days, will share messenger app private api.

It would be very useful if you would share how did you manage to find a request, what data it returns, why we use this request etc. I saw blog post somewhere that send graphql based on decoded binary message from websocket. For me it would help understating how it works.

So based on the blog I found I created a code that send message, maybe it'll save some time for you.

import requests
import json
import datetime
import random
import argparse
import re

parser = argparse.ArgumentParser()
parser.add_argument("-m", "--message")
parser.add_argument("-r", "--recipient", type=int)
args = parser.parse_args()

with open('cookies.json', 'r') as f:
    cookies = json.load(f)

session = requests.Session()

for cookie in cookies:
    session.cookies.set(cookie['name'], cookie['value'], domain=cookie['domain'], path=cookie['path'])

inbox_html_resp = session.get("https://www.messenger.com")
inbox_html_resp.raise_for_status()
inbox_html_page = inbox_html_resp.text

dtsg = re.search(r'"DTSGInitialData",\[\],\{"token":"([^"]+)"', inbox_html_page).group(1)
device_id = re.search(r'"deviceId":"([^"]+)"', inbox_html_page).group(1)
schema_version = "4680497022042598"

script_urls = re.findall(r'src="([^"]+)"', inbox_html_page)
scripts = [session.get(url).text for url in script_urls if not url.startswith('data:')]

doc_id = next(re.search(r'id:"([0-9]+)",metadata:\{\},name:"LSPlatformGraphQLLightspeedRequestQuery"', script).group(1) for script in scripts if "LSPlatformGraphQLLightspeedRequestQuery" in script)

timestamp = int(datetime.datetime.now().timestamp() * 1000)

epoch = timestamp << 22
otid = epoch + random.randrange(2 ** 22)

send_message_resp = session.post(
    "https://www.messenger.com/api/graphql/",
    data={
        "fb_dtsg": dtsg,
        "doc_id": doc_id,
        "variables": json.dumps(
            {
                "deviceId": device_id,
                "requestId": 0,
                "requestPayload": json.dumps(
                    {
                        "version_id": str(schema_version),
                        "tasks": [
                            {
                                "label": "46",
                                "payload": json.dumps(
                                    {
                                        "thread_id": args.recipient,
                                        "otid": str(otid),
                                        "source": 0,
                                        "send_type": 1,
                                        "text": args.message,
                                        "initiating_source": 1,
                                    }
                                ),
                                "queue_name": str(args.recipient),
                                "task_id": 0,
                                "failure_count": None,
                            },
                            {
                                "label": "21",
                                "payload": json.dumps(
                                    {
                                        "thread_id": args.recipient,
                                        "last_read_watermark_ts": timestamp,
                                        "sync_group": 1,
                                    }
                                ),
                                "queue_name": str(args.recipient),
                                "task_id": 1,
                                "failure_count": None,
                            },
                        ],
                        "epoch_id": epoch,
                    }
                ),
                "requestType": 3,
            }
        ),
    },
)

send_message_resp.raise_for_status()

print(send_message_resp.text)
wezabronsz commented 3 months ago

I also would love to see auto reply bot. I'm trying to develop my own. I already can get threads ids by one of graphql request that response with such data, but I don't know how sending message works here. Anybody can help? I think it's probably done through websocket but I'm not 100% sure.

messenger app uses websocket and xmpp like client to communication (chat) both side (server/client). can be done auto system using messenger web api easily, but todo using private api of apk file will take times...

I will share web api at this time, after some days, will share messenger app private api.

I believe pagination support while getting threads id would be very good feature.