Skip to content

Commit

Permalink
Added way to send url previews
Browse files Browse the repository at this point in the history
sendUri(link, message) will send the preview for link below the message in chat
  • Loading branch information
gitCommitWiL authored and gitCommitWiL committed Apr 7, 2020
1 parent b461873 commit c5b8118
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
46 changes: 46 additions & 0 deletions fbchat/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,52 @@ def sendMessage(self, message, thread_id=None, thread_type=ThreadType.USER):
Message(text=message), thread_id=thread_id, thread_type=thread_type
)

def sendUri(self, uri, message=None, thread_id=None, thread_type=ThreadType.USER):
"""Send a uri preview to a thread.
Args:
uri: uri to preview
message (Message): Message to send
thread_id: User/Group ID to send to. See :ref:`intro_threads`
thread_type (ThreadType): See :ref:`intro_threads`
Returns:
:ref:`Message ID <intro_message_ids>` of the sent message
Raises:
FBchatException: If request failed
"""

urlData = self._state._uri_share_data({"uri": uri})
thread_id, thread_type = self._getThread(thread_id, thread_type)
thread = thread_type._to_class()(thread_id)
data = thread._to_send_data()
if message is not None:
data.update(message._to_send_data())
data["action_type"] = "ma-type:user-generated-message"
data["shareable_attachment[share_type]"] = urlData["share_type"]
# most uri params will come back as dict
if isinstance(urlData["share_params"], dict):
data["has_attachment"] = True
for key in urlData["share_params"]:
if isinstance(urlData["share_params"][key], dict):
for key2 in urlData["share_params"][key]:
data[
"shareable_attachment[share_params][{}][{}]".format(
key, key2
)
] = urlData["share_params"][key][key2]
else:
data[
"shareable_attachment[share_params][{}]".format(key)
] = urlData["share_params"][key]
# some (such as facebook profile pages) will just be a list
else:
data["has_attachment"] = False
for index, val in enumerate(urlData["share_params"]):
data["shareable_attachment[share_params][{}]".format(index)] = val
return self._doSendRequest(data)

def sendEmoji(
self,
emoji=None,
Expand Down
7 changes: 7 additions & 0 deletions fbchat/_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,10 @@ def _do_send_request(self, data):
"Error when sending message: "
"No message IDs could be found: {}".format(j)
)

def _uri_share_data(self, data):
data["image_height"] = 960
data["image_width"] = 960
data["__user"] = self.user_id
j = self._post("/message_share_attachment/fromURI/", data)
return j["payload"]["share_data"]

0 comments on commit c5b8118

Please sign in to comment.