parent
538719120f
commit
43961c21eb
@ -0,0 +1,3 @@ |
|||||||
|
# 微信机器人 |
||||||
|
|
||||||
|
> 一个简易的通过python调用腾讯机器人进行微信聊天的程序 |
@ -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('调试错误') |
@ -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() |
Loading…
Reference in new issue