Skip to content

Commit c5b8118

Browse files
gitCommitWiLgitCommitWiL
authored andcommitted
Added way to send url previews
sendUri(link, message) will send the preview for link below the message in chat
1 parent b461873 commit c5b8118

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

fbchat/_client.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,52 @@ def sendMessage(self, message, thread_id=None, thread_type=ThreadType.USER):
10661066
Message(text=message), thread_id=thread_id, thread_type=thread_type
10671067
)
10681068

1069+
def sendUri(self, uri, message=None, thread_id=None, thread_type=ThreadType.USER):
1070+
"""Send a uri preview to a thread.
1071+
1072+
Args:
1073+
uri: uri to preview
1074+
message (Message): Message to send
1075+
thread_id: User/Group ID to send to. See :ref:`intro_threads`
1076+
thread_type (ThreadType): See :ref:`intro_threads`
1077+
1078+
Returns:
1079+
:ref:`Message ID <intro_message_ids>` of the sent message
1080+
1081+
Raises:
1082+
FBchatException: If request failed
1083+
"""
1084+
1085+
urlData = self._state._uri_share_data({"uri": uri})
1086+
thread_id, thread_type = self._getThread(thread_id, thread_type)
1087+
thread = thread_type._to_class()(thread_id)
1088+
data = thread._to_send_data()
1089+
if message is not None:
1090+
data.update(message._to_send_data())
1091+
data["action_type"] = "ma-type:user-generated-message"
1092+
data["shareable_attachment[share_type]"] = urlData["share_type"]
1093+
# most uri params will come back as dict
1094+
if isinstance(urlData["share_params"], dict):
1095+
data["has_attachment"] = True
1096+
for key in urlData["share_params"]:
1097+
if isinstance(urlData["share_params"][key], dict):
1098+
for key2 in urlData["share_params"][key]:
1099+
data[
1100+
"shareable_attachment[share_params][{}][{}]".format(
1101+
key, key2
1102+
)
1103+
] = urlData["share_params"][key][key2]
1104+
else:
1105+
data[
1106+
"shareable_attachment[share_params][{}]".format(key)
1107+
] = urlData["share_params"][key]
1108+
# some (such as facebook profile pages) will just be a list
1109+
else:
1110+
data["has_attachment"] = False
1111+
for index, val in enumerate(urlData["share_params"]):
1112+
data["shareable_attachment[share_params][{}]".format(index)] = val
1113+
return self._doSendRequest(data)
1114+
10691115
def sendEmoji(
10701116
self,
10711117
emoji=None,

fbchat/_state.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,10 @@ def _do_send_request(self, data):
329329
"Error when sending message: "
330330
"No message IDs could be found: {}".format(j)
331331
)
332+
333+
def _uri_share_data(self, data):
334+
data["image_height"] = 960
335+
data["image_width"] = 960
336+
data["__user"] = self.user_id
337+
j = self._post("/message_share_attachment/fromURI/", data)
338+
return j["payload"]["share_data"]

0 commit comments

Comments
 (0)