From 43961c21ebf5e720a1668a894f454aee24be9a2d Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 7 Dec 2021 14:53:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E6=9C=BA=E5=99=A8=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/itca/README.md | 3 +++ api/itca/bot.py | 32 ++++++++++++++++++++++++++++++++ api/itca/itch.py | 21 +++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 api/itca/README.md create mode 100644 api/itca/bot.py create mode 100644 api/itca/itch.py diff --git a/api/itca/README.md b/api/itca/README.md new file mode 100644 index 0000000..3b45144 --- /dev/null +++ b/api/itca/README.md @@ -0,0 +1,3 @@ +# 微信机器人 + +> 一个简易的通过python调用腾讯机器人进行微信聊天的程序 \ No newline at end of file diff --git a/api/itca/bot.py b/api/itca/bot.py new file mode 100644 index 0000000..d45420b --- /dev/null +++ b/api/itca/bot.py @@ -0,0 +1,32 @@ +import json +from tencentcloud.common import credential +from tencentcloud.common.profile.client_profile import ClientProfile +from tencentcloud.common.profile.http_profile import HttpProfile +from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException +from tencentcloud.tbp.v20190627 import tbp_client, models + +def test(t): + try: + # 腾讯云的api接口 + cred = credential.Credential("tencentcloud-api", "tencentcloud-secret") + httpProfile = HttpProfile() + httpProfile.endpoint = "tbp.tencentcloudapi.com" + + clientProfile = ClientProfile() + clientProfile.httpProfile = httpProfile + client = tbp_client.TbpClient(cred, "", clientProfile) + + req = models.TextProcessRequest() + params = { + "BotId": "5cbe9f65-8fdb-488f-8cd9-3d2905477525", + "BotEnv": "release", + "TerminalId": "123", + "InputText": t + } + req.from_json_string(json.dumps(params)) + + resp = client.TextProcess(req) + return eval(resp.to_json_string())["ResponseMessage"]['GroupList'][0]['Content'] + + except TencentCloudSDKException as err: + print('调试错误') diff --git a/api/itca/itch.py b/api/itca/itch.py new file mode 100644 index 0000000..cf3e5e9 --- /dev/null +++ b/api/itca/itch.py @@ -0,0 +1,21 @@ +import itchat +from bot import test +from datetime import datetime + +now = str(datetime.now()) +@itchat.msg_register(itchat.content.TEXT) +def text_reply(msg): + with open('wechat.log','a',encoding='utf-8') as f: + fromuser = msg.FromUserName + touser = msg.ToUserName + text = msg.Text + f.write(now+'\n') + f.write(fromuser+'\n') + f.write(text+'\n') + f.write('\n') + r = test(text) + itchat.send(r, toUserName=fromuser) + +itchat.auto_login( hotReload=True) + +itchat.run()