Links

Send message to new connections

Tags: #linkedin #message #naas_drivers #content #snippet #text
Author: Asif Syed
Last update: 2023-05-29 (Created: 2022-07-06)
Description: This notebook allows users to quickly and easily send messages to their new LinkedIn connections.
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

from naas_drivers import linkedin
import pandas as pd
from datetime import date
import naas

Setup LinkedIn

LI_AT = (
naas.secret.get("LI_AT") or "ENTER_YOUR_COOKIE_LI_AT_HERE"
) # EXAMPLE AQFAzQN_PLPR4wAAAXc-FCKmgiMit5FLdY1af3-2
JSESSIONID = (
naas.secret.get("JSESSIONID") or "ENTER_YOUR_COOKIE_JSESSIONID_HERE"
) # EXAMPLE ajax:8379907400220387585

Setup custom message

The message format sent to new connections is structured as follows - Greeting{space}First_Name{comma}{space}Message
eg: Hi Asif, thank you for connecting with me!
First_Name is directly fetched from the dataframe and we can customize the Greeting and Message parameters by editing the strings in the below cell.
Greeting = "Hi"
Message = "Thank you for connecting with me!"

Model

Get last connections

Get the recent connections data (limit parameter can be changed as per your requirement; it is the maximum number of new connections to whom you want to send the message)
df = linkedin.connect(LI_AT, JSESSIONID).network.get_connections(limit=100)
df.head(5)

Filter data

def filter_data(df):
df["CREATED_AT"] = pd.to_datetime(
df["CREATED_AT"]
).dt.date # changing the CREATED_AT column data to datetime format
df = df[
df["CREATED_AT"] == date.today()
] # restricting the dataframe to hold only the details of people connected today
return df
df_profiles = filter_data(df)
print("✅ New connections today:", len(df_profiles))
df_profiles

Output

Send the message

for (
index,
row,
) in (
df_profiles.iterrows()
): # looping through each profile in the dataframe of new connections
firstname = row["FIRSTNAME"]
lastname = row["LASTNAME"]
profile_url = row["PROFILE_URL"]
result = linkedin.message.send(
content=Greeting + " " + firstname + "," + " " + Message,
recipients_url=profile_url,
)
print(f"✅ Message sent to {firstname} {lastname}")