Skip to main content

Wecom(A.K.A. WeChat Work) Group Bot python API.

Project description

pywgb

Wecom(A.K.A. WeChat Work) Group Bot python API.

Homepage

ChowRex/pywgb: Wecom(A.K.A Wechat Work) Group Bot python API.

How to use

  1. Create a Wecom Group Bot.

  2. Copy the webhook URL or just the key. It MUST contains an UUID (8-4-4-4-12), like:

  3. Install this package:

    # Normally use this if you won't send voice message
    pip install -U pywgb
    # You can install full version by this
    pip install -U "pywgb[all]"
    
  4. If you want to send simple messages, refer code below:

    from pywgb import TextBot, MarkdownBot, ImageBot, NewsBot, FileBot, VoiceBot
    
    KEY = "PASTE_YOUR_KEY_OR_WEBHOOKURL_HERE"
    
    # If you want to send Text message, use this.
    msg = "This is a test Text message."
    # If you want to mention someone, refer this.
    kwargs = {
      "mentioned_list": [
        # If you know the userid
        "userid",
        # Use below for ALL people
        "@all",
      ],
      "mentioned_mobile_list": [
        # If you know the phone number
        "13800001111",
        # Use below for ALL people
        "@all",
      ]
    }
    bot = TextBot(KEY)
    bot.send(msg)
    bot.send(msg, **kwargs)
    
    # If you want to send Markdown message, use this.
    bot = MarkdownBot(KEY)
    col = [bot.green, bot.gray, bot.orange]
    msg = [col[idx % 3](ltr) for idx, ltr in enumerate("colorful")]
    msg = f"This is a {''.join(msg)} Markdown message"
    bot.send(msg)
    
    # If you want to send Image message, use this.
    file = "Path/To/Your/Image.png" or "Path/To/Your/Image.jpg"
    bot = ImageBot(KEY)
    bot.send(file_path=file)
    
    # If you want to send News message, use this.
    articles = [
        {
            "title": "This is a test news",
            "description": "You can add description here",
            "url": "www.tencent.com",
            # Here is the link of picture
            "picurl": "https://www.tencent.com/img/index/tencent_logo.png"
        },
    ]
    bot = NewsBot(KEY)
    bot.send(articles=articles)
    
    # If you want to send File message, use this.
    file = "Path/To/Your/File.suffix"
    bot = FileBot(KEY)
    bot.send(file_path=file)
    
    # If you want to send Voice message, use this.
    file = "Path/To/Your/Voice.amr"  # BE ADVISED: ONLY support amr file
    bot = VoiceBot(KEY)
    bot.send(file_path=file)
    
  5. Send template card messages (Advanced usage)

    • Upload temporary files, any kind of bot can upload file.

      from pywgb import TextBot, MarkdownBot, ImageBot, NewsBot, FileBot, VoiceBot, TextCardBot, NewsCardBot
      
      KEY = "PASTE_YOUR_KEY_OR_WEBHOOKURL_HERE"
      
      # Specify your tempory file path
      file = "Path/To/Your/File.suffix"
      # Any kind of bot can upload file
      bot = TextBot(KEY)
      media_id = bot.upload(file)
      print(media_id)
      
    • TextTemplateCard (Need more detail? click here.)

      from pywgb import TextCardBot
      
      KEY = "PASTE_YOUR_KEY_OR_WEBHOOKURL_HERE"
      
      # Prepare the text card content
      kwargs = {
          "main_title": {
              "title": "Test message",
              "desc": "This is a test template text card message"
          },
          "emphasis_content": {
              "title": "100",
              "desc": "No meaning"
          },
          "quote_area": {
              "type": 1,
              "url": "https://work.weixin.qq.com/?from=openApi",
              "title": "Title reference",
              "quote_text": "Hello\nWorld!"
          },
          "sub_title_text": "This is sub-title",
          "horizontal_content_list": [{
              "keyname": "Author",
              "value": "Rex"
          }, {
              "keyname": "Google",
              "value": "Click to go",
              "type": 1,
              "url": "https://google.com"
          }],
          "jump_list": [{
              "type": 1,
              "url": "https://bing.com",
              "title": "Bing"
          }],
          "card_action": {
              "type": 1,
              "url": "https://work.weixin.qq.com/?from=openApi",
          }
      }
      bot = TextCardBot(KEY)
      bot.send(**kwargs)
      
    • NewsTemplateCard (Need more detail? click here.)

      from pywgb import NewsCardBot
      
      KEY = "PASTE_YOUR_KEY_OR_WEBHOOKURL_HERE"
      
      # Prepare the news card content
      kwargs = {
          "source": {
              "icon_url":
                  "https://wework.qpic.cn/wwpic/252813_jOfDHtcISzuodLa_1629280209/0",
              "desc":
                  "This is for testing",
              "desc_color":
                  0
          },
          "main_title": {
              "title": "Test message",
              "desc": "This is a test template news card message"
          },
          "card_image": {
              "url":
                  "https://wework.qpic.cn/wwpic/354393_4zpkKXd7SrGMvfg_1629280616/0",
              "aspect_ratio":
                  2.25
          },
          "image_text_area": {
              "type":
                  1,
              "url":
                  "https://work.weixin.qq.com",
              "title":
                  "Welcom to use pywgb",
              "desc":
                  "This is a test message",
              "image_url":
                  "https://wework.qpic.cn/wwpic/354393_4zpkKXd7SrGMvfg_1629280616/0"
          },
          "quote_area": {
              "type": 1,
              "url": "https://work.weixin.qq.com/?from=openApi",
              "title": "Title reference",
              "quote_text": "Hello\nWorld!"
          },
          "vertical_content_list": [{
              "title": "Hi, there",
              "desc": "Welcome to use"
          }],
          "horizontal_content_list": [{
              "keyname": "Author",
              "value": "Rex"
          }, {
              "keyname": "Google",
              "value": "Click to go",
              "type": 1,
              "url": "https://google.com"
          }],
          "jump_list": [{
              "type": 1,
              "url": "https://bing.com",
              "title": "Bing"
          }],
          "card_action": {
              "type": 1,
              "url": "https://work.weixin.qq.com/?from=openApi",
          }
      }
      bot = NewsCardBot(KEY)
      bot.send(**kwargs)
      

Official Docs

Only Chinese doc: 群机器人配置说明 - 文档 - 企业微信开发者中心

Roadmap

  • v0.0.1: 🎉 Initial project. Offering send Text and Markdown type message.

  • v0.0.2: 🖼️ Add Image type message support;

    • Add overheat detect function and unified exception handling
  • v0.0.3: 📰 Add News type message support;

    • Move bots into a new module: bot
  • v0.0.4: 📂 Add File type message support;

    • Refactor bot module
  • v0.0.5: 🗣️ Add Voice type message support.

    • Refactor deco module
    • Add verify_file decorator
    • Introverted parameters check errors
    • Add more content into README.md
  • v0.0.6: 🩹 Add Voice and File type size check.

  • v0.0.7: 🗒️ Add TextCard type message support.

  • v0.0.8: 🗃️ Add NewsCard type message support.

  • v1.0.0: 👍 First FULL capacity stable version release.Fix bugs and so on.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pywgb-0.1.0.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pywgb-0.1.0-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

Details for the file pywgb-0.1.0.tar.gz.

File metadata

  • Download URL: pywgb-0.1.0.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for pywgb-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2ddd180e1a3dc0eb3405aeef5ba486793587bae6bd4802218d55ea315edf9728
MD5 339f866c43d58b86093d05c130251e64
BLAKE2b-256 60a3335732d57a90613b7c0c430ec4994a604b665929c9d4c19016e8963ae616

See more details on using hashes here.

File details

Details for the file pywgb-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pywgb-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for pywgb-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2a9ea39a80e1abb61ab0d4c4c5a52d679a0bdc09876bb5e612a22ea0fd75a37
MD5 738b3b1d396812bf15e38349b3458203
BLAKE2b-256 36f5b54fc56abfaf099be25a0e1d50d7fcdb92de7b6dec934f9dfb2a5658c17b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page