subzeroid / instagrapi

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

Direct Message (dm) #1890

Open thegreatref opened 5 months ago

thegreatref commented 5 months ago

from instagrapi import Client

--- Initialize Instagrapi client client = Client()

--- Login to your Instagram account username = password = client.login(username, password)

thread = client.direct_threads(2)[0]

--- Target user's username target_username =

--- Get the user ID of the target user user_info_dict = client.user_info_by_username_v1(username).dict() target_user_id = user_info_dict['pk']

--- Your message message = "Hello"

--- Send a direct message try: client.direct_send(message, target_user_id) print("Direct message sent successfully!") except Exception as e: print(f"Error sending direct message: {e}")

Returns

Error sending direct message: {"action":"item_ack","status_code":"403","message":"We're sorry, but something went wrong. Please try again.","payload":{"client_context":"6800090932895711343","message":"We're sorry, but something went wrong. Please try again."},"status":"fail"}

Please assist

thegreatref commented 4 months ago

Alternative DM with Instagrapi

from instagrapi import Client from myInfo import my_password, my_username, receiver_username, message, time

log in to your Instagram account

api = Client() api.login(my_username, my_password) # if this line throws an error, then either your login info is wrong, OR instagram policies were triggered because you logged in too many times. If so, you need to manually login to your account and verify the account to proceed print('Logged in successfully!')

search for the receiver's user by username

user = api.search_users(receiver_username)

from datetime import datetime from time import sleep def send_message(message, user_id): while True: now = datetime.now() if [now.hour, now.minute] == time: api.direct_send(message, [user_id]) print('Message sent successfully!') sleep(60) # check the time every minute

if user: # if receiver's user is found send_message(message, user[0].pk) # user[0].pk = user_id else: print(f"No search results found for username '{receiver_username}'")

log out of your Instagram account

api.logout()