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

Direct Message (dm) #1890

Open
thegreatref opened this issue Apr 23, 2024 · 1 comment
Open

Direct Message (dm) #1890

thegreatref opened this issue Apr 23, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@thegreatref
Copy link

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 thegreatref added the bug Something isn't working label Apr 23, 2024
@thegreatref
Copy link
Author

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant