rveachkc / pymsteams

Format messages and post to Microsoft Teams.
https://pypi.org/project/pymsteams/
Apache License 2.0
422 stars 78 forks source link

connector_card.send() sometimes returns True even when the message does not appear in the channel. #58

Closed x-thompson3 closed 4 years ago

x-thompson3 commented 4 years ago

Describe the bug connector_card.send() sometimes returns True even when the message does not appear in the channel. We have retry logic structure in place to attempt to send a message 3 times. Twice now, the connector_card.send() returned True but did not send a message to the Teams channel.

To Reproduce Steps to reproduce the behavior: Unable to consistently reproduce, but has happened at least twice.

Expected behavior When connector_card.send() returns True, the message within the connector_card should show up in the appropriate teams channel.

Additional context Our codebase uses a helper function to send a given message to a given channel_url. The channel_url is stored in config files. If the response from connector_card.send() is True, we return the response. If an Exception is thrown, we print the Exception and retry. See below:

connector_card = pymsteams.connectorcard(channel_url)
retry_count = self.retry_count
        while retry_count > 0:
            try:
                retry_count -= 1

                # Add text to the message.
                connector_card.text(message)
                # Send the message
                response = connector_card.send()

                if response:
                    celery_logger.info(f"Successfully sent update to Teams channel: {response}")
                    return response
                else:
                    celery_logger.info("Teams post_to_channel() failed to respond, retrying.")
            except Exception as inst:
                celery_logger.warning(f"Hit Exception during send(): {str(inst.args)}")
                print("...Hit Exception during send()!")
                print("\n" + str(inst.args) + "\n")
                continue
        return False
rveachkc commented 4 years ago

Sorry for the delay, haven't had much time to look at this. Is this problem still recurring?

Looking at the source from the most recent version, (0.1.12) link, the send code is really pretty simple. It just posts the message to the MS Teams webhook defined on class init. It relies on the webservice to report success/failure based on the http status code. Anything other than a 200 will raise an exception. requests source

    def send(self):
        headers = {"Content-Type":"application/json"}
        r = requests.post(
            self.hookurl,
            json=self.payload,
            headers=headers,
            proxies=self.proxies,
            timeout=self.http_timeout,
            verify=self.verify,
        )

        if r.status_code == requests.codes.ok:
            return True
        else:
            raise TeamsWebhookException(r.text)

If this is still recurring, maybe the message is getting hung up on the MS side? Is there anything non-standard about your Teams implementation or your network that could interfere with communication?

I'm not sure there's much I can do with the info that's been provided.

rveachkc commented 4 years ago

requested additional info, none provided.

The limitations of the webhook implementation prevent pymsteams to know anything other than the http status code returned by the teams API.

sunny1978 commented 4 years ago

Hi. I tried giving some bogus webhook and bogus channel. API is returning back true.

Webhook=“https://outlook.office.com/bogus” myTeamsMessage = pymsteams.connectorcard(Webhook) myTeamsMessage.text(test) _r = myTeamsMessage.send() _r True

What is the best way to capture the true response code?