Python - Send a message to MS channel

Steps

  1. End result. A message on channel.

  2. Join or create a team https://mail.google.com/chat/u/0/ and create a new space.

  3. Create a team from scratch.

  4. Team for either private or public.

  5. Team name. eg) Demo

  6. More options on channel. eg) Demo >> General >> More options

  7. Connectors to add a new incoming webhook.

  8. Add Incoming Webhook.

  9. Connectors to configure the incoming webhook.

  10. Configure Incoming Webhook.

  11. Name of the webhook.

  12. Copy the webhook url.

  13. Create a new python file and paste below code. Update url to the webhook. eg) demo.py

    #  https://developers.google.com/chat/quickstart/incoming-bot-python
    from json import dumps
    
    from httplib2 import Http
    
    
    def main():
        """Hangouts Chat incoming webhook quickstart."""
        url = '<INCOMING-WEBHOOK-URL>'
        bot_message = {
            'text' : 'Hello from a Python script!'}
    
        message_headers = {'Content-Type': 'application/json; charset=UTF-8'}
    
        http_obj = Http()
    
        response = http_obj.request(
            uri=url,
            method='POST',
            headers=message_headers,
            body=dumps(bot_message),
        )
    
        print(response)
    
    if __name__ == '__main__':
        main()
    
  14. Run demo.py. Ctrl + F5 or Right click >> "Run Python File in Terminal" within VS Code.

Github

taeheechoi © 2023