python微信公众号消息接口_Python 微信公众号消息发送

import requests

import time

import random

import json

class switch(object):

def __init__(self, value):

self.value = value

self.fall =False

def __iter__(self):

"""Return the match method once, then stop"""

yield self.match

raise StopIteration

def match(self, *args):

"""Indicate whether or not to enter a case suite"""

if self.fallor not args:

return True

elif self.valuein args:# changed for v1.5, see below

self.fall =True

return True

else:

return False

def send(openid,token,appmsgid):

header ={

'Accept':'application/json, text/javascript, */*; q=0.01',

'Accept-Encoding':'gzip, deflate, br',

'Accept-Language':'zh-CN,zh;q=0.9',

'Connection':'keep-alive',

'Content-Length':'11406',

'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',

'Cookie':open('./cookies.txt',encoding='utf-8',mode='r').read(),

'Host':'mp.weixin.qq.com',

'Origin':'https://mp.weixin.qq.com',

'Referer':'https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&tofakeid='+openid+'&token='+token+'&lang=zh_CN',

'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',

'X-Requested-With':'XMLHttpRequest',

}

url ='https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&f=json&token='+token+'&lang=zh_CN'

post_data ={

'token':token,

'lang':'zh_CN',

'f':'json',

'ajax':1,

'random':0.29628174341321656,

'smart_product':0,

'type':10,

'app_id':appmsgid,

'isMulti':1,

'multi_item':[],

'tofakeid':openid,

'appmsgid':appmsgid,

'imgcode':'',

}

try:

res = requests.post(url, data=post_data, headers=header)

ret = json.loads(res.text)['base_resp']['ret']

for casein switch(ret):

if case(200003):

print('invalid session')

break

if case(200040):

print('invalid csrf token')

break

if case(200002):# default, could also just omit condition or 'if True'

print("openid error")

break

if case(10705):# default, could also just omit condition or 'if True'

print("app msg id not exist")

break

if case(0):# default, could also just omit condition or 'if True'

print("message send success!!!")

break

if case():

print(res.text)

except Exception as e:

print(e)

if __name__ =='__main__':

file =open("./openid.txt",encoding='utf-8',mode='r').read().strip("\n")

if file !='':

openids = file.split("\n")

print(len(openids))

action =int(input("请输入0|1:\n"))

token =input("token:\n")

appmsgid =input("appmsgid|app_id:\n")

# app_id 图文管理查询

if action ==0:

for openidin openids:

if openid !='':

send(openid,token,appmsgid)

time.sleep(random.randint(60,120))

if action ==1:

send(input("请输入openid:\n"),token,appmsgid)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值