Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[suggestion] facebook auto-reply functionality #2

Open
Skuxblan opened this issue Mar 24, 2024 · 6 comments
Open

[suggestion] facebook auto-reply functionality #2

Skuxblan opened this issue Mar 24, 2024 · 6 comments

Comments

@Skuxblan
Copy link

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
Copy link
Owner

i will add messenger codes then use as you needed

@Skuxblan
Copy link
Author

Skuxblan commented Mar 24, 2024

i will add messenger codes then use as you needed

Great, can't wait for it. Thank you

@wezabronsz
Copy link

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
Copy link
Owner

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
Copy link
Author

Skuxblan commented Mar 31, 2024

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants