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()