subzeroid / instagrapi

🔥 The fastest and powerful Python library for Instagram Private API 2024
https://hikerapi.com/p/bkXQlaVe
MIT License
4.38k stars 686 forks source link

How to send a message to group chat? #1302

Closed momo-AUX1 closed 1 year ago

momo-AUX1 commented 1 year ago

When I try to send a message to a group chat i can get it to successfully appear to select but when I try to send it errors: e) File "/data/data/com.termux/files/home/api.py", line 16, in send_instagram_group_message client.direct_send(message, target_thread.thread_id) ^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'DirectThread' object has no attribute 'thread_id'. Did you mean: 'thread_title'?

If it has been already answered I'd like to get the link of the old issue, else does anyone know how do we do it? Thanks !

birdhouses commented 1 year ago

I think you're calling thread_id instead of thread.id which is causing your issue

birdhouses commented 1 year ago

Make sure to check the documentation, if it's still not working, ask chatGPT, if that doesn't work, provide more code concerning your issue and I'll try to help :)

>>> cl.direct_send('How are you?', thread_ids=[thread.id])
DirectMessage(id=30076213210116812312341061613568, user_id=None, thread_id=34028236684171031231231231233331238762, timestamp=datetime.datetime(2021, 8, 31, 18, 33, 5, 127298, tzinfo=datetime.timezone.utc), item_type=None, is_shh_mode=None, reactions=None, text=None, animated_media=None, media=None, media_share=None, reel_share=None, story_share=None, felix_share=None, clip=None, placeholder=None)

https://adw0rd.github.io/instagrapi/usage-guide/direct.html

momo-AUX1 commented 1 year ago

Make sure to check the documentation, if it's still not working, ask chatGPT, if that doesn't work, provide more code concerning your issue and I'll try to help :)

>>> cl.direct_send('How are you?', thread_ids=[thread.id])
DirectMessage(id=30076213210116812312341061613568, user_id=None, thread_id=34028236684171031231231231233331238762, timestamp=datetime.datetime(2021, 8, 31, 18, 33, 5, 127298, tzinfo=datetime.timezone.utc), item_type=None, is_shh_mode=None, reactions=None, text=None, animated_media=None, media=None, media_share=None, reel_share=None, story_share=None, felix_share=None, clip=None, placeholder=None)

https://adw0rd.github.io/instagrapi/usage-guide/direct.html

Sure heres my complete code I'm trying to link it to a gpt4all instance : from flask import Flask, request, jsonify

import subprocess
from instagrapi import Client

app = Flask(__name__)

def send_instagram_group_message(client, group_name, message):
    threads = client.direct_threads()
    target_thread = None

    for thread in threads:
        if thread.thread_title == group_name:
            target_thread = thread
            print(thread)
            break

    if target_thread is None:
        print(f"Group '{group_name}' not found.")
        return

    client.direct_send(message, target_thread.id)

@app.route("/", methods=["POST"])
def index():
    data = request.json
    sender = data["query"]["sender"]
    message = data["query"]["message"]

    prompt = f"{sender}: {message}"
    print(prompt)
    result = subprocess.run(['./chat', '-p', f"'{prompt}'"], stdout=subprocess.PIPE, text=True)
    response = result.stdout

    # Extract the response text
    start_index = response.find("[end of text]")
    end_index = response.rfind("[end of text]")
    response_text = response[start_index + len("[end of text]") : end_index].strip()

    # Send the message through Instagram
    client = Client()
    client.login('chat.gpt._', 'X)
    target_group_name = "🍜 Ramen Gang 🍜"
    send_instagram_group_message(client, target_group_name, response_text)

    return jsonify({"response": response_text})

if __name__ == "__main__":
    app.run(host="0.0.0.0")
birdhouses commented 1 year ago

What if you do this:

def send_instagram_group_message(client, group_name, message):
    threads = client.direct_threads()
    target_thread = None

    for thread in threads:
        if thread.thread_title == group_name:
            target_thread = client.direct_thread(thread.id) <------ get the thread implicitly
            print(thread)
            break

    if target_thread is None:
        print(f"Group '{group_name}' not found.")
        return

    client.direct_send(message, target_thread.id)
momo-AUX1 commented 1 year ago

What if you do this:

def send_instagram_group_message(client, group_name, message):
    threads = client.direct_threads()
    target_thread = None

    for thread in threads:
        if thread.thread_title == group_name:
            target_thread = client.direct_thread(thread.id) <------ get the thread implicitly
            print(thread)
            break

    if target_thread is None:
        print(f"Group '{group_name}' not found.")
        return

    client.direct_send(message, target_thread.id)

Yes it worked! I thank you I will save your messages in my text note as the basic example for group sending