# gewechat-python
gewechat python client
## Usage
安装 gewechat client
```sh
pip install gewechat-client
```
使用demo
```python
from gewechat_client import GewechatClient
import os
def main():
# 配置参数
base_url = os.environ.get("BASE_URL", "http://127.0.0.1:2531/v2/api")
token = os.environ.get("GEWECHAT_TOKE", "xxx")
app_id = os.environ.get("APP_ID", "xxx")
send_msg_nickname = "张伟" # 要发送消息的好友昵称
# 创建 GewechatClient 实例
client = GewechatClient(base_url, token)
# 登录, 自动创建二维码,扫码后自动登录
app_id, error_msg = client.login(app_id=app_id)
if error_msg:
print("登录失败")
return
try:
# 获取好友列表
fetch_contacts_list_result = client.fetch_contacts_list(app_id)
if fetch_contacts_list_result.get('ret') != 200 or not fetch_contacts_list_result.get('data'):
print("获取通讯录列表失败:", fetch_contacts_list_result)
return
# {'ret': 200, 'msg': '操作成功', 'data': {'friends': ['weixin', 'fmessage', 'medianote', 'floatbottle', 'wxid_abcxx'], 'chatrooms': ['1234xx@chatroom'], 'ghs': ['gh_xx']}}
friends = fetch_contacts_list_result['data'].get('friends', [])
if not friends:
print("获取到的好友列表为空")
return
print("获取到的好友列表:", friends)
# 获取好友的简要信息
friends_info = client.get_brief_info(app_id, friends)
if friends_info.get('ret') != 200 or not friends_info.get('data'):
print("获取好友简要信息失败:", friends_info)
return
# {
# "ret": 200,
# "msg": "获取联系人信息成功",
# "data": [
# {
# "userName": "weixin",
# "nickName": "微信团队",
# "pyInitial": "WXTD",
# "quanPin": "weixintuandui",
# "sex": 0,
# "remark": "",
# "remarkPyInitial": "",
# "remarkQuanPin": "",
# "signature": null,
# "alias": "",
# "snsBgImg": null,
# "country": "",
# "bigHeadImgUrl": "https: //wx.qlogo.cn/mmhead/Q3auHgzwzM6H8bJKHKyGY2mk0ljLfodkWnrRbXLn3P11f68cg0ePxA/0",
# "smallHeadImgUrl": "https://wx.qlogo.cn/mmhead/Q3auHgzwzM6H8bJKHKyGY2mk0ljLfodkWnrRbXLn3P11f68cg0ePxA/132",
# "description": null,
# "cardImgUrl": null,
# "labelList": null,
# "province": "",
# "city": "",
# "phoneNumList": null
# }
# ]
# }
# 找对目标好友的wxid
friends_info_list = friends_info['data']
if not friends_info_list:
print("获取到的好友简要信息列表为空")
return
wxid = None
for friend_info in friends_info_list:
if friend_info.get('nickName') == send_msg_nickname:
print("找到好友:", friend_info)
wxid = friend_info.get('userName')
break
if not wxid:
print(f"没有找到好友: {send_msg_nickname} 的wxid")
return
print("找到好友:", wxid)
# 发送消息
send_msg_result = client.post_text(app_id, wxid, "你好啊")
if send_msg_result.get('ret') != 200:
print("发送消息失败:", send_msg_result)
return
print("发送消息成功:", send_msg_result)
except Exception as e:
print("Failed to fetch contacts list:", str(e))
if __name__ == "__main__":
main()
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hanfangyuan4396/gewechat-python",
"name": "gewechat-client",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "gewechat api client",
"author": "hanfangyuan",
"author_email": "i@hanfangyuan.cn",
"download_url": "https://files.pythonhosted.org/packages/78/fb/a70f9e6f62a9f9b3cf315d7d3e632de2e56915f04324cd804a05f37e07ee/gewechat_client-0.1.5.tar.gz",
"platform": null,
"description": "# gewechat-python\n\ngewechat python client\n\n## Usage\n\n\u5b89\u88c5 gewechat client\n```sh\npip install gewechat-client\n```\n\n\u4f7f\u7528demo\n```python\nfrom gewechat_client import GewechatClient\nimport os\n\ndef main():\n # \u914d\u7f6e\u53c2\u6570\n base_url = os.environ.get(\"BASE_URL\", \"http://127.0.0.1:2531/v2/api\")\n token = os.environ.get(\"GEWECHAT_TOKE\", \"xxx\")\n app_id = os.environ.get(\"APP_ID\", \"xxx\")\n send_msg_nickname = \"\u5f20\u4f1f\" # \u8981\u53d1\u9001\u6d88\u606f\u7684\u597d\u53cb\u6635\u79f0\n\n # \u521b\u5efa GewechatClient \u5b9e\u4f8b\n client = GewechatClient(base_url, token)\n\n # \u767b\u5f55, \u81ea\u52a8\u521b\u5efa\u4e8c\u7ef4\u7801\uff0c\u626b\u7801\u540e\u81ea\u52a8\u767b\u5f55\n app_id, error_msg = client.login(app_id=app_id)\n if error_msg:\n print(\"\u767b\u5f55\u5931\u8d25\")\n return\n try:\n\n # \u83b7\u53d6\u597d\u53cb\u5217\u8868\n fetch_contacts_list_result = client.fetch_contacts_list(app_id)\n if fetch_contacts_list_result.get('ret') != 200 or not fetch_contacts_list_result.get('data'):\n print(\"\u83b7\u53d6\u901a\u8baf\u5f55\u5217\u8868\u5931\u8d25:\", fetch_contacts_list_result)\n return\n # {'ret': 200, 'msg': '\u64cd\u4f5c\u6210\u529f', 'data': {'friends': ['weixin', 'fmessage', 'medianote', 'floatbottle', 'wxid_abcxx'], 'chatrooms': ['1234xx@chatroom'], 'ghs': ['gh_xx']}}\n friends = fetch_contacts_list_result['data'].get('friends', [])\n if not friends:\n print(\"\u83b7\u53d6\u5230\u7684\u597d\u53cb\u5217\u8868\u4e3a\u7a7a\")\n return\n print(\"\u83b7\u53d6\u5230\u7684\u597d\u53cb\u5217\u8868:\", friends)\n\n # \u83b7\u53d6\u597d\u53cb\u7684\u7b80\u8981\u4fe1\u606f\n friends_info = client.get_brief_info(app_id, friends)\n if friends_info.get('ret') != 200 or not friends_info.get('data'):\n print(\"\u83b7\u53d6\u597d\u53cb\u7b80\u8981\u4fe1\u606f\u5931\u8d25:\", friends_info)\n return\n # {\n # \"ret\": 200,\n # \"msg\": \"\u83b7\u53d6\u8054\u7cfb\u4eba\u4fe1\u606f\u6210\u529f\",\n # \"data\": [\n # {\n # \"userName\": \"weixin\",\n # \"nickName\": \"\u5fae\u4fe1\u56e2\u961f\",\n # \"pyInitial\": \"WXTD\",\n # \"quanPin\": \"weixintuandui\",\n # \"sex\": 0,\n # \"remark\": \"\",\n # \"remarkPyInitial\": \"\",\n # \"remarkQuanPin\": \"\",\n # \"signature\": null,\n # \"alias\": \"\",\n # \"snsBgImg\": null,\n # \"country\": \"\",\n # \"bigHeadImgUrl\": \"https: //wx.qlogo.cn/mmhead/Q3auHgzwzM6H8bJKHKyGY2mk0ljLfodkWnrRbXLn3P11f68cg0ePxA/0\",\n # \"smallHeadImgUrl\": \"https://wx.qlogo.cn/mmhead/Q3auHgzwzM6H8bJKHKyGY2mk0ljLfodkWnrRbXLn3P11f68cg0ePxA/132\",\n # \"description\": null,\n # \"cardImgUrl\": null,\n # \"labelList\": null,\n # \"province\": \"\",\n # \"city\": \"\",\n # \"phoneNumList\": null\n # }\n # ]\n # }\n \n # \u627e\u5bf9\u76ee\u6807\u597d\u53cb\u7684wxid\n friends_info_list = friends_info['data']\n if not friends_info_list:\n print(\"\u83b7\u53d6\u5230\u7684\u597d\u53cb\u7b80\u8981\u4fe1\u606f\u5217\u8868\u4e3a\u7a7a\")\n return\n wxid = None\n for friend_info in friends_info_list:\n if friend_info.get('nickName') == send_msg_nickname:\n print(\"\u627e\u5230\u597d\u53cb:\", friend_info)\n wxid = friend_info.get('userName')\n break\n if not wxid:\n print(f\"\u6ca1\u6709\u627e\u5230\u597d\u53cb: {send_msg_nickname} \u7684wxid\")\n return\n print(\"\u627e\u5230\u597d\u53cb:\", wxid)\n\n # \u53d1\u9001\u6d88\u606f\n send_msg_result = client.post_text(app_id, wxid, \"\u4f60\u597d\u554a\")\n if send_msg_result.get('ret') != 200:\n print(\"\u53d1\u9001\u6d88\u606f\u5931\u8d25:\", send_msg_result)\n return\n print(\"\u53d1\u9001\u6d88\u606f\u6210\u529f:\", send_msg_result)\n except Exception as e:\n print(\"Failed to fetch contacts list:\", str(e))\n\nif __name__ == \"__main__\":\n main()\n\n```\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "A package for interacting with the Gewechat API",
"version": "0.1.5",
"project_urls": {
"Homepage": "https://github.com/hanfangyuan4396/gewechat-python"
},
"split_keywords": [
"gewechat",
"api",
"client"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9d06c8eefab6feff9cf56f2359629b0108922c2867c267a34097e8b4d6fad617",
"md5": "c48856ce0857f03b2e488735bac8e245",
"sha256": "86bbed31773065d673c071e9e7591fc0b4a6059d08a8a8c57428c083923f1183"
},
"downloads": -1,
"filename": "gewechat_client-0.1.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c48856ce0857f03b2e488735bac8e245",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 19414,
"upload_time": "2024-12-03T14:09:40",
"upload_time_iso_8601": "2024-12-03T14:09:40.371879Z",
"url": "https://files.pythonhosted.org/packages/9d/06/c8eefab6feff9cf56f2359629b0108922c2867c267a34097e8b4d6fad617/gewechat_client-0.1.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "78fba70f9e6f62a9f9b3cf315d7d3e632de2e56915f04324cd804a05f37e07ee",
"md5": "c32a0f2b47dadfc5da207afde89caed9",
"sha256": "fc232fe86b22fb620d695b60f3afe0a6ae27d93c09171fc5ca1682d01dc850d2"
},
"downloads": -1,
"filename": "gewechat_client-0.1.5.tar.gz",
"has_sig": false,
"md5_digest": "c32a0f2b47dadfc5da207afde89caed9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 16132,
"upload_time": "2024-12-03T14:09:42",
"upload_time_iso_8601": "2024-12-03T14:09:42.089607Z",
"url": "https://files.pythonhosted.org/packages/78/fb/a70f9e6f62a9f9b3cf315d7d3e632de2e56915f04324cd804a05f37e07ee/gewechat_client-0.1.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-03 14:09:42",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hanfangyuan4396",
"github_project": "gewechat-python",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "gewechat-client"
}