Comments (57)
@littlecodersh
可以了,现在不能给自己发送信息的账号也可以通过文件传输助手发指令
代码修改如下:
#!/usr/bin/env python
#coding=utf-8
import os
import sys
import time
import itchat
if os.name == 'posix' and sys.version_info[0] < 3:
import subprocess32 as subprocess
else:
import subprocess
help = '''\
WeChat remote:
* download file: aria2c url
* download video: youtube-dl url
* online video: mpv url
* & most commands you use in terminal
* help(show this message)\
'''
@itchat.msg_register('Text')
def interact_filehelper(msg):
if msg['ToUserName'] != 'filehelper': return
if msg['Text'] in ['help', u'帮助']:
# print msg['Text']
itchat.send(help, 'filehelper')
else:
commands = msg.get('Content', '')
args = commands.split()
try:
proc = subprocess.Popen(args,stdout=subprocess.PIPE)
itchat.send(proc.communicate()[0].strip(), 'filehelper')
except OSError as e:
itchat.send('Commands/Files not found', 'filehelper')
itchat.auto_login(hotReload = True)
itchat.run()
另外不能给自己发送信息的账号可以把发给自己的信息转发到文件传输助手,返回的内容也返回到助手
比如:
import itchat
@itchat.msg_register('Text')
def interact_filehelper(msg):
if msg['FromUserName'] == msg['ToUserName']:
itchat.send(msg['Text'], 'filehelper')
itchat.auto_login(hotReload = True)
itchat.run()
另外观察发现,不能给自己发送信息的账号,文件传输助手上返回的内容总是插入到本条指令之前,比如在微信上先显示 help 的内容 ,再显示 help 这条指令
from itchat.
@yangxuan8282 和我描述一下需要的功能吧
from itchat.
@littlecodersh 谢谢回复。主要是两个功能:添加下载和查看正在下载
telegram 的那个bot是通过diana实现的
添加下载对应命令:
diana add url
查看下载对应命令:
diana list
也就是希望向bot发送这两条命令能实现添加下载和查看正在下载的文件
from itchat.
@yangxuan8282
我给你写了个demo,通过与自己的微信账户交互控制diana
具体操作diana的命令你改一下就好:
import subprocess, re
import itchat
help = '''\
Wechat aria2 download:
* diana list
* diana add url\
'''
@itchat.msg_register('Text')
def diana_download(msg):
if not msg['FromUserName'] == msg['ToUserName']: return
if msg['Text'] == 'diana list':
# return subprocess.Popen(['diana', 'list'], stdout=subprocess.PIPE).communicate()[0]
return subprocess.Popen(['echo', '"diana list"'], stdout=subprocess.PIPE).communicate()[0].strip()
r = re.findall('diana add (.*?)$', msg['Text'])
if not r: return 'No command matched'
# return subprocess.Popen(['diana', 'add', r[0]], stdout=subprocess.PIPE).communicate()[0]
return subprocess.Popen(['echo', '"%s"'%r[0]], stdout=subprocess.PIPE).communicate()[0].strip()
itchat.auto_login()
itchat.send(help)
itchat.run()
from itchat.
@littlecodersh 试了一下,好像添加不了下载,但看代码也没看到问题在哪
from itchat.
@yangxuan8282 你先尝试用python调用diana,成功以后再套进我给你的示例里面吧
from itchat.
不好意思,刚回复
在python里不知道怎么加参数(add),所以尝试先不用diana,直接调用aria2
不知道为什么扫码登陆之后程序就自己关了,能帮忙看下哪里有问题吗
import os
import subprocess
import itchat
itchat.auto_login()
def get_url():
@itchat.msg_register
def simple_reply(msg):
if msg.get('Type', '') == 'Text':
url = msg.get('Content', '')
return url
else:
return u'输入有误'
itchat.run()
def download_url(url):
try:
subprocess.Popen(['aria2c', url])
except:
return u'error'
from itchat.
@yangxuan8282
你没有调用get_url
方法么?
维持程序运行的itchat.run()
命令仅在该方法中被调用。
from itchat.
把 itchat.run()
移动到 itchat.auto_login()
下面之后不会自动关闭了
但是测试了一下aria2还是没能添加下载任务,是 get_url
部分的问题吗,你说的调用是该怎样来实现
from itchat.
@yangxuan8282 你能使用python使用aria2
吗?
from itchat.
我在终端里大概试了一下,是可以的
pi@raspberrypi:~ $ python
Python 2.7.9 (default, Mar 8 2015, 00:52:26)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import subprocess
>>> if 1 ==1 :
... subprocess.Popen(['aria2c', 'https://github.com/littlecodersh/ItChat/archive/master.zip'])
File "<stdin>", line 2
subprocess.Popen(['aria2c', 'https://github.com/littlecodersh/ItChat/archive/master.zip'])
^
IndentationError: expected an indented block
>>> subprocess.Popen(['aria2c', 'https://github.com/littlecodersh/ItChat/archive/master.zip'])
<subprocess.Popen object at 0x76a34830>
[#1a9e7c 0B/0B CN:1 DL:0B]
06/28 22:31:41 [NOTICE] Download complete: /home/pi/ItChat-master.zip
Download Results:
gid |stat|avg speed |path/URI
======+====+===========+=======================================================
1a9e7c|OK | 11KiB/s|/home/pi/ItChat-master.zip
Status Legend:
(OK):download completed.
from itchat.
@yangxuan8282 用.py
可以么?
from itchat.
.py是指?
LittleCoder [email protected]于2016年6月28日周二 下午10:48写道:
@yangxuan8282 https://github.com/yangxuan8282 用.py可以么?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#24 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AGpz0EXPV83SSJjC8rzUhzeAG_NcQXGqks5qQTQ3gaJpZM4I-en_
.
from itchat.
@yangxuan8282 就是写成py
文件
from itchat.
试了一下,测试的那几行写成 .py
也可以添加下载
pi@raspberrypi:~/ItChat $ python test.py
pi@raspberrypi:~/ItChat $
06/28 23:12:07 [NOTICE] Download complete: /home/pi/ItChat/robot.py
Download Results:
gid |stat|avg speed |path/URI
======+====+===========+=======================================================
1f0067|OK | 3.9MiB/s|/home/pi/ItChat/robot.py
Status Legend:
(OK):download completed.
pi@raspberrypi:~/ItChat $ cat test.py
import os
import subprocess
if 1 == 1:
subprocess.Popen(['aria2c', 'https://raw.githubusercontent.com/littlecodersh/ItChat/robot/itchat/robot.py'])
from itchat.
@yangxuan8282 你现在的itchat
测试程序会回显你发过去的内容吗?
from itchat.
@littlecodersh 不显示,只有手机上能看到
from itchat.
@yangxuan8282 就是指你发过去的内容会被微信回发给你吗?
from itchat.
EDIT: 不会回发,给自己发的话,手机和 itchat
都没显示
用别的账号给这个号发的话,只有手机上能收到信息, itchat
那边没有
from itchat.
@yangxuan8282 给自己发应该微信里会收到与发送内容相同的回复。
试一下这个程序,也不会收到么?
import itchat
@itchat.msg_register('Text')
def test(msg):
if not msg['FromUserName'] == msg['ToUserName']: return
return msg['Text']
itchat.auto_login()
itchat.run()
from itchat.
@littlecodersh 不行,给自己发两边都收不到,只能看到发出的那条,没有回复
之前碰到过类似的情况,当时是在微信的 设置
-> 隐私
里关闭了 加我为朋友时需要验证
,(没加好友)的人发的消息就不会发到 itchat
这边,不过现在没有关闭,应该不是这个原因
from itchat.
@yangxuan8282 加个print语句测试一下有检测到信息么
from itchat.
加了一个,不知道加的对不对
import itchat
@itchat.msg_register('Text')
def test(msg):
if not msg['FromUserName'] == msg['ToUserName']: return
return msg['Text']
print msg['Text']
itchat.auto_login()
itchat.run()
itchat
这边没有显示
from itchat.
@yangxuan8282 这样加肯定运行不到print的
用这个:
import itchat
@itchat.msg_register('Text')
def test(msg):
print msg['Text']
if not msg['FromUserName'] == msg['ToUserName']: return
return msg['Text']
itchat.auto_login()
itchat.run()
from itchat.
@littlecodersh 用你给的这个 itchat
那边可以打印出来给自己发送的消息
from itchat.
@yangxuan8282 那试一下这个程序会不会给自己发消息:
import itchat
itchat.auto_login()
itchat.send('This is a test message')
itchat.run()
from itchat.
@littlecodersh 没有给自己发送消息
from itchat.
@yangxuan8282 我第一次碰到这个问题,如果确定收不到的话,你可以尝试抓包看看你的账号使用网页版微信和使用这个项目给自己发消息发送的数据包有什么区别。
from itchat.
@littlecodersh 之前用 robot 分支当聊天机器人的时候, itchat
这边可以看到消息
以前试过 网易音乐 ,可以播放音乐
按 Ctrl + c 结束 itchat
后,微信手机端还是显示 微信已登录,手机通知已关闭
,这个是正常的吗
from itchat.
@yangxuan8282 如果绕过无法给自己发信息的问题的话,你可以选择把程序改成判断别的账号给自己发的消息。
正常的。
from itchat.
@littlecodersh 在微信网页版试了一下给自己发信息,也是发不出去,看RasWxMusicbox的作者提到过有些微信号无法给自己发送信息。但是用别的号发送指令的话应该不受影响
能不能帮忙测试一下一天前写的那个直接调用aria2的代码是否可用
from itchat.
@yangxuan8282 我这里没有aira2
不好意思,你自己测试一下吧。
from itchat.
@littlecodersh 今天又试了一下,可以调用 aria2
远程下载了,在你给的demo基础上稍微改动了一下
# aria2 remote for WeChat (based on ItChat: https://github.com/littlecodersh/ItChat)
import os
import subprocess
import itchat
help = '''\
Wechat aria2 remote:
* hot to use: send file url
* help(show this message)\
'''
@itchat.msg_register('Text')
def download(msg):
if msg['Text'] == 'help':
itchat.send(help)
else:
url = msg.get('Content', '')
subprocess.Popen(['aria2c', url])
itchat.auto_login()
itchat.run()
另外稍带尝试了一下在线视频播放(通过调用 mpv
播放bilibili),也成功了,只是简单替换了一下打开的程序,比如想看这个视频:http://www.bilibili.com/video/av5120798/ ,就发送给bot账号url就可以了,不过脚本要在本机上运行,如果通过SSH运行会报错(打开视频输出设备失败)
# mpv remote for WeChat (based on ItChat: https://github.com/littlecodersh/ItChat)
import os
import subprocess
import itchat
help = '''\
Wechat mpv remote:
* hot to use: send video url
* help(show this message)\
'''
@itchat.msg_register('Text')
def player(msg):
if msg['Text'] == 'help':
itchat.send(help)
else:
url = msg.get('Content', '')
subprocess.Popen(['mpv', url])
itchat.auto_login()
itchat.run()
下一步想改进一下遥控在线视频的播放,比如先尝试结束当前视频播放,然后再播放新的视频,参考RasWxMusicbox的代码,直接在 else 部分嵌套了
try:
subprocess.Popen(['pkill', 'mpv'])
time.sleep(.3)
except:
pass
finally:
subprocess.Popen(['mpv', mp3_url])
结果不行,视频都播放不起来,还得再研究下
from itchat.
@yangxuan8282 好的,如果有什么进一步要帮忙的我们再交流。
from itchat.
@littlecodersh 问下怎样才能把输出结果返回到手机微信上呢
from itchat.
@yangxuan8282 itchat.register的方法的return即可
from itchat.
@littlecodersh 照搬demo的return成功发回输出,可以实现一定程度的交互,多数命令行的命令应该都能用,比如 apt-get update&&apt-get upgrade
,之前的远程下载和视频播放也可用,可以添加参数,建了一个 repo 方便别人查找
from itchat.
@yangxuan8282 棒的!
from itchat.
@littlecodersh 问下怎样能在 subprocess
内实现多进程
比如用这个脚本发送了一条播放视频的命令:
mpv url
中途想退出,发送一条:
pkill mpv
但是这条命令没有被立即执行,而是等到上一条命令执行完(也就是视频播放完)才执行
这样有些情况就没法更好的控制了,在找了几个类似的提问,修改之后要么是不返回输出,要么是依然执行完一个命令才能执行下一个
from itchat.
@yangxuan8282 其实我不是很懂一个subprocess
是否必要?
类似这样的程序是否能够达到需求?
import subprocess
subprocess.Popen(['sleep', '10'])
subprocess.Popen(['mkdir', '1'])
from itchat.
@littlecodersh 又看了 python 文档, communicate()
方法要 Wait for process to terminate.
。这个问题可能暂时解决不了,估计只能通过调用其它的 wrapper 控制播放器。
另外看 commit 添加了短时间内保持登陆状态这个很实用的功能
根据文档改了下脚本:
if itchat.load_login_status():
@itchat.msg_register('Text')
def remote(msg):
...
itchat.run()
else:
itchat.auto_login()
itchat.dump_login_status()
print('Config stored')
itchat.run()
问下 if
内的 itchat.run()
有必要保留吗
from itchat.
@yangxuan8282 其实直接使用itchat.auto_login()
即可,新版本中默认使用了短时间保持登录状态
from itchat.
@littlecodersh 那就是不用改?
@itchat.msg_register('Text')
def remote(msg):
...
itchat.auto_login()
itchat.run()
这个短时间保持登陆大概是多久
from itchat.
@yangxuan8282
对的,不需要修改代码。
如果不需要使用这个功能可以使用这个命令:
itchat.auto_login(False)
我这里只尝试了五分钟内可以登录,进一步就没测下去了。
from itchat.
@littlecodersh
试了一下,不改的话 过大约5分钟登陆还是要重新扫码
加上 if
判定的话(按文档说的那样)可以不用重新扫码
EDIT: 后来又试了一下,直接 itchat.auto_login()
也可以保持登陆状态
from itchat.
@yangxuan8282
可以把wechat_remote.py中的32行换成与filehelper交互。
这样就不会有发送消息失败的问题了。
if msg['ToUserName'] != 'filehelper': return
from itchat.
@littlecodersh
试了一下好像和之前没区别,不能给自己发消息的账号还是没法发命令
from itchat.
@yangxuan8282 是发送消息给文件交互助手吗?不是自己的账号
from itchat.
@littlecodersh 对的,是给文件传送助手发送的消息,没有回应
from itchat.
@yangxuan8282
你用网页版微信能给文件传输助手发送消息吗?
from itchat.
@littlecodersh 在网页版可以通过文件传输助手互相发送消息
from itchat.
@yangxuan8282 不应该呀,你试试这个测试程序
import itchat
@itchat.msg_register('Text')
def interact_filehelper(msg):
if msg['ToUserName'] != 'filehelper': return
itchat.send(msg['Text'], 'filehelper')
itchat.auto_login(True)
itchat.run()
from itchat.
@littlecodersh
上一个可以返回发送的内容
然后我尝试改写了一下 wechat_remote.py
中的帮助部分
import itchat
help = '''\
WeChat remote:
* help(show this message)\
'''
@itchat.msg_register('Text')
def remote(msg):
if msg['ToUserName'] != 'filehelper': return
if msg['Text'] == 'help':
itchat.send(help, 'filehelper')
itchat.auto_login(True)
itchat.run()
结果没有回应
from itchat.
@yangxuan8282
好的,起码是可以实现的,下面就是代码的优化了
这是完整的代码还是缩减过的代码?
试试尝试添加几个print,判断一下是没有收到消息还是收到以后没有发出help吧
from itchat.
@littlecodersh
和文件传输助手交互是这版才加的吗?现在的版本短时间保持登陆状态的功能好像不稳定,用的这种方式: itchat.auto_login(hotReload = True)
经常得重新扫码
上面的代码是完整的代码,因为只是想测试 help 能不能返回;
from itchat.
@yangxuan8282 没有,一直都可以和文件传输助手交互。保持登陆功能可能是后台问题,过段时间估计就会好了。
试试尝试添加几个print,判断一下是没有收到消息还是收到以后没有发出help吧
from itchat.
@littlecodersh
问下自动回复有没有办法只对个人账号发的消息进行回复,忽略掉公众号推送的消息
from itchat.
Related Issues (20)
- Hi how i yours the App
- 现在微信网页版无法登录,还能用itchat吗 HOT 11
- iPhone手机上的8.0.43版本微信是不是改VOIP了?
- 视频号 HOT 1
- ???
- 错误
- 17年6月下旬前登陆过web网页版的微信可以使用此框架,验证微信是否支持web版本API,请点击https://wx.qq.com/ ,扫码验证即可,假如出现无法登陆,则不适用于此框架 HOT 3
- itchat.send_video 运行没有效果 HOT 2
- 如何获得微信群群人数
- 【无法登录】“为了保障你的账号安全,暂不支持使用网页版微信。你可以前往微信官网” HOT 3
- ?
- ?
- 可以与jenkins集成吗?将jenkins构建的结果推送到普通微信群中
- 关于thread过时
- 获取什么作为群聊用户的永久唯一标识? HOT 4
- set_alias修改好友备注接口怎么没有生效 HOT 3
- 我在登录后出现'xml.parsers.expat.ExpatError: mismatched tag: line 64, column 4' 怎么解决 HOT 9
- 国务院的人都是一群鲨跸 HOT 4
- 【!!!研发必看!!!】替代UOS WEB版,做机器人,小助手,营销系统,客服系统,监管系统的可以 看下这个API方案 HOT 5
- 能根据手机号获取微信头像昵称吗?个人签名。 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from itchat.