Invoke Salesforce Messaging for Web REST API using Python
763 views
Feb 14, 2025
Salesforce Messaging for Web REST API helps us to connect to the Messaging Services via REST API. Check the following link for getting started with the Salesforce Messaging for Web REST API. https://www.infallibletechie.com/2024/11/salesforce-messaging-for-in-app-and-web-rest-api.html Blog Post: https://www.infallibletechie.com/2025/02/invoke-salesforce-messaging-for-web-rest-api-using-python.html
View Video Transcript
0:07
hello everyone in this video we are
0:09
going to see how to invoke Salesforce
0:12
messaging for web rest
0:15
API and we are going to make use of
0:18
Python Programming so if you have a
0:20
requirement where you wanted to invoke
0:22
Salesforce messaging for web rest apas
0:24
from uh python uh code then you can make
0:28
use of this video as your
0:31
reference if you are pretty new to
0:33
Salesforce messaging for uh web rest API
0:37
then please make use of this link using
0:40
which you have to create a messaging
0:43
Channel and then the messaging channel
0:45
should be
0:47
active when you create uh the embedded
0:49
service deployment it uh you have to
0:52
create uh um using custom client as the
0:55
type you will get the code snippet using
0:59
which we will be be invoking the
1:01
Salesforce messaging for web rest
1:04
API if you wanted to first check using
1:08
Postman client then uh in this uh blog
1:12
post I have given screenshots how to
1:14
generate token how to create
1:16
conversation how to subscribe to the
1:18
server side events how to close the
1:21
conversation how to send a message so
1:23
all these uh get and post and delete
1:26
requests are available with the sample
1:29
uh uh payload also in this block post so
1:32
you can make use of it now let's jump on
1:35
to um the same Salesforce messaging for
1:39
web rest API from python
1:41
code I'm going to make rest API calls so
1:44
I'm installing requests Library so using
1:48
this command you will be able to install
1:51
the requests
1:53
Library I'm going to listen to the uh
1:56
Salesforce messaging for web uh servers
1:59
ID events so I am installing ssse client
2:03
using this command you should be able to
2:05
install ssse client
2:08
Library the first step is I created a
2:11
folder with the name Salesforce
2:14
miaw next I created a python file with
2:18
this content I have created this
2:20
variables. py file so that I can reuse
2:24
all the
2:25
variables uh let's take access token for
2:28
all the
2:30
calls uh after getting the access token
2:33
we have to make use of the access token
2:35
in the header instead of hardcoding it
2:38
in each and every file I'm going to
2:40
write that particular value to this
2:42
variables. py file so that I can reuse
2:46
across multiple python
2:49
files so first I created
2:51
this variables. py uh file uh which has
2:56
my conversation ID as of now it is empty
2:59
and access token is also empty and this
3:01
is my base URL which I got it from the
3:04
code snipper so the variables. py fil is
3:09
ready next I am importing requests I'm
3:13
importing uh variables I am making uh
3:16
rest API call uh in order to get the
3:19
access token the base URL plus the path
3:24
I'm passing uh uh the body as Json and
3:28
then uh I will be able to to get the
3:31
access token once the access token is uh
3:34
uh retrieved from that particular uh
3:37
Json response I am writing
3:41
that to this variables. py uh file so
3:46
that I can reuse across multiple files
3:48
so let's run this I'm able to get the
3:51
access token so the access token is
3:54
updated in this file also so I can reuse
3:58
across multiple files
4:00
the next step is I'm importing uu ID to
4:03
create conversation ID um this
4:07
conversation ID will be
4:09
used uh in the variable sorry in the
4:12
variables. pyi and also when we make a
4:16
call to create the conversation once we
4:18
get the access token the next step is we
4:20
have to create the conversation in order
4:22
to create the conversation I'm making
4:25
use of uu ID do uuid4 method and then
4:28
I'm getting the conversation ID I'm
4:30
making use of that conversation ID so
4:34
the first step is I'm updating that in
4:35
the file next I'm making a call to the
4:39
base URL plus conversation and this is
4:42
the body and in the headers I am passing
4:45
the access token and also I'm making use
4:48
of the conversation ID so now if I run
4:52
this so this is the conversation
4:55
ID it is updated here also since it
4:59
created the conversation as an agent I
5:02
will be able to receive the messaging
5:03
session record in Salesforce so as an
5:06
agent I'm able to view the um messaging
5:09
session as an agent I was able to make
5:12
available myself a Omni so that I can
5:14
receive the
5:15
request so now this is also done next
5:19
I'm going to listen to the uh server
5:22
side events with the Salesforce
5:24
messaging for rest API we can listen to
5:26
the um s events so that we can see all
5:30
the back and forth conversation between
5:32
agent agent and the visitor so I'm going
5:36
to run
5:38
this so it is uh it
5:42
started um now what I'm going to do is
5:46
I'm going to send a message using this
5:49
uh sample code I'm going to send a
5:51
message uh to the agent uh the message
5:54
is testing from python I'm getting I'm
5:57
using the same access token and also I'm
5:59
making use of the same conversation ID
6:01
in order to have back and forth
6:03
conversation so I'm going to send
6:06
it so it's I was able to send a message
6:10
from my python code to
6:12
Salesforce so this is also
6:16
done in the SS event I should be able
6:28
to okay
6:32
again rerun
6:35
it okay I'm able to see event data ID if
6:39
I send a message I should see that in
6:41
the SS
6:43
event okay I'm able to see I'm able to
6:46
see the start indicator the conversation
6:48
message and also the conversation typing
6:51
stopped indicator event so if the agent
6:55
sends multiple message I should be able
6:57
to see it
7:00
and even if I send a message I should be
7:02
able to see
7:09
that yeah I should be able to see
7:17
that if you want to close the
7:20
conversation you can make a uh Delete
7:23
request uh to the base URL plus
7:26
conversation as the path slash in the in
7:29
the full path you have to use the
7:30
conversation ID uh which was uh um which
7:35
was when the conversation was created
7:37
when we did the create conversation post
7:41
request um so you have to make use of
7:43
the same conversation ID and uh in the
7:46
parameter uh we have to
7:48
pass es developer name equal to the AP
7:52
name of the
7:55
um um embod service deployment uh which
7:58
we created for this custom client so if
8:02
I do this it is 204 so definitely it got
8:08
succeeded so it ended the
8:14
session please check the video
8:16
description in the video description I
8:18
have given my blog post from the blog
8:20
post you should be able to get the
8:22
sample python code which we were
8:24
executing in this video as your
8:26
reference
8:34
I hope it was
8:40
helpful thank you for watching
#Business & Productivity Software
#Intelligent Personal Assistants
#Internet Software
#Other