This repository was archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsend-message.py
More file actions
46 lines (33 loc) · 1.44 KB
/
send-message.py
File metadata and controls
46 lines (33 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import asyncio
import logging
from pathlib import Path
from linkedin_messaging import ChallengeException, LinkedInMessaging
from linkedin_messaging.api_objects import URN, AttributedBody, MessageCreate
cookie_path = Path(__file__).parent.joinpath("cookies.pickle")
urn = URN("urn:li:fs_conversation:2-OTNkODIyYTEtODFjZS00NTdlLThlYTItYWQyMDg2NTc4YWMyXzAxMA==")
async def main():
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
linkedin = LinkedInMessaging()
if cookie_path.exists():
with open(cookie_path, "rb") as cf:
linkedin = LinkedInMessaging.from_pickle(cf.read())
if not await linkedin.logged_in():
try:
await linkedin.login("EMAIL", "PASSWORD")
except ChallengeException:
await linkedin.enter_2fa(input("2fa code: "))
with open(cookie_path, "wb+") as cf:
cf.write(linkedin.to_pickle())
# Send a simple message that has some text.
mc = MessageCreate(AttributedBody("test"))
print(await linkedin.send_message(urn, mc))
# Send a multimedia message.
with open("/path/to/the/cool-pic.jpg", "rb") as f:
attachment = await linkedin.upload_media(f.read(), "cool-pic.jpg", "image/jpeg")
mc = MessageCreate(AttributedBody(), attachments=[attachment])
print(await linkedin.send_message(urn, mc))
await linkedin.logout()
await linkedin.close()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())