MCPcopy
hub / github.com/wechaty/python-wechaty / say

Method say

src/wechaty/user/contact.py:312–385  ·  view source on GitHub ↗

say something to contact, which can be text, image, file, contact, url Note: Its implementation depends on the puppet, so if you want to use this method, please check [Puppet](https://github.com/wechaty/wechaty/wiki/Puppet#3-puppet-compatible-table). Args:

(self, message: Union[str, Message, FileBox, Contact, UrlLink, MiniProgram]
                  )

Source from the content-addressed store, hash-verified

310 return 'Contact <%s> <%s>' % (self.contact_id, identity)
311
312 async def say(self, message: Union[str, Message, FileBox, Contact, UrlLink, MiniProgram]
313 ) -> Optional[Message]:
314 """say something to contact, which can be text, image, file, contact, url
315
316 Note:
317 Its implementation depends on the puppet, so if you want to use this method, please check
318 [Puppet](https://github.com/wechaty/wechaty/wiki/Puppet#3-puppet-compatible-table).
319
320 Args:
321 message: the message object to be sended to contact
322
323 Examples:
324 >>> contact = Contact.load('contact-id')
325 >>> await contact.say('hello')
326 >>> await contact.say(FileBox.from_file('/path/to/file'))
327 >>> await contact.say(contact)
328 >>> await contact.say(UrlLink('https://wechaty.js.org'))
329
330 >>> # the data format of mini program should pre-stored
331 >>> await contact.say(MiniProgram('username', 'appid'))
332
333 Returns:
334 Message: if the message is send successfully, return the message object, otherwise return None
335 """
336 if not message:
337 log.error('can"t say nothing')
338 return None
339
340 if not self.is_ready():
341 await self.ready()
342
343 # import some class because circular dependency
344 from wechaty.user.url_link import UrlLink # pylint: disable=import-outside-toplevel
345
346 if isinstance(message, str):
347 # say text
348 msg_id = await self.puppet.message_send_text(
349 conversation_id=self.contact_id,
350 message=message
351 )
352 elif isinstance(message, Contact):
353 msg_id = await self.puppet.message_send_contact(
354 contact_id=message.contact_id,
355 conversation_id=self.contact_id
356 )
357
358 elif isinstance(message, FileBox):
359 msg_id = await self.puppet.message_send_file(
360 conversation_id=self.contact_id,
361 file=message
362 )
363
364 elif isinstance(message, UrlLink):
365 # use this way to resolve circulation dependency import
366 msg_id = await self.puppet.message_send_url(
367 conversation_id=self.contact_id,
368 url=json.dumps(dataclasses.asdict(message.payload), ensure_ascii=False)
369 )

Callers

nothing calls this directly

Calls 9

readyMethod · 0.95
is_readyMethod · 0.80
message_send_textMethod · 0.80
message_send_contactMethod · 0.80
message_send_fileMethod · 0.80
message_send_urlMethod · 0.80
loadMethod · 0.45

Tested by

no test coverage detected