Links

Accept all invitations and send first message

Tags: #linkedin #content #operations #automation #invitation
Author: Florent Ravenel
Last update: 2023-05-29 (Created: 2022-04-05)
Description: This notebook helps you quickly and easily accept all LinkedIn invitations and send a personalized introductory message to each new connection.
Disclaimer: This code is in no way affiliated with, authorized, maintained, sponsored or endorsed by Linkedin or any of its affiliates or subsidiaries. It uses an independent and unofficial API. Use at your own risk.
This project violates Linkedin's User Agreement Section 8.2, and because of this, Linkedin may (and will) temporarily or permanently ban your account. We are not responsible for your account being banned.

Input

Import libraries

import naas
from naas_drivers import linkedin
import pandas as pd

Setup Variables

If you are using the Chrome Extension:
  • Copy/Paste your token in your extension
  • Login/Logout your LinkedIn account
  • Your secrets "LINKEDIN_LI_AT" and "LINKEDIN_JSESSIONID" will be added directly on your naas everytime you login and logout.
or
If you are not using the Google Chrome Extension, learn how to get your cookies on LinkedIn
  • li_at: Cookie used to authenticate Members and API clients
  • JSESSIONID: Cookie used for Cross Site Request Forgery (CSRF) protection and URL signature validation
  • cron: Cron params for naas scheduler. More information: https://crontab.guru/
  • first_message: First message to be send
# Inputs
li_at = naas.secret.get("LINKEDIN_LI_AT") or "YOUR_COOKIE_LI_AT"
JSESSIONID = naas.secret.get("LINKEDIN_JSESSIONID") or "YOUR_COOKIE_JSESSIONID"
cron = "0 * * * *"
# Outputs
first_message = "Hello, Nice to connect!"

Model

Get invitations received

df_invitation = linkedin.connect(li_at, JSESSIONID).invitation.get_received()
df_invitation

Accept pending invitations received from "Profile"

def accept_new_contact(df):
df_accept = pd.DataFrame()
# Loop
for index, row in df.iterrows():
fullname = row.FULLNAME
status = row.INVITATION_STATUS
invitation_id = row.INVITATION_ID
shared_secret = row.SHARED_SECRET
if status == "PENDING":
print(fullname)
tmp_df = linkedin.connect(li_at, JSESSIONID).invitation.accept(
invitation_id, shared_secret
)
df_accept = pd.concat([df_accept, tmp_df])
return df_accept
df_accept = accept_new_contact(df_invitation)
df_accept

Send first message to contact

def send_first_message(df):
# Loop
for index, row in df.iterrows():
fullname = row.FULLNAME
profile_id = row.PROFILE_ID
print(fullname)
linkedin.connect(li_at, JSESSIONID).message.send(FIRST_MESSAGE, profile_id)
send_first_message(df_accept)

Output

Display result

df_accept

Schedule notebook

# Schedule your notebook every hour
naas.scheduler.add(cron=cron)
# -> Uncomment the line below to remove your scheduler
# naas.scheduler.delete()