赞
踩
# wechaty 最新版本(部分客户端还无法兼容)
docker run -it -d --name wechaty_test -e WECHATY_LOG="verbose" -e WECHATY_PUPPET="wechaty-puppet-padlocal" -e WECHATY_PUPPET_PADLOCAL_TOKEN="你的token" -e WECHATY_PUPPET_SERVER_PORT="8080" -e WECHATY_TOKEN="lqSaPuhivEg4ghY3Gbw6SuUF7dWMdh761" -p "8080:8080" wechaty/wechaty:latest
# wechaty 0.65版本
docker run -it -d --name wechaty_test -e WECHATY_LOG="verbose" -e WECHATY_PUPPET="wechaty-puppet-padlocal" -e WECHATY_PUPPET_PADLOCAL_TOKEN="你的token" -e WECHATY_PUPPET_SERVER_PORT="8080" -e WECHATY_TOKEN="lqSaPuhivEg4ghY3Gbw6SuUF7dWMdh761" -p "8080:8080" wechaty/wechaty:0.65
获取pad token网址:http://pad-local.com
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <groupId>io.github.wechaty</groupId> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> <relativePath /> </parent> <artifactId>wechaty-examples</artifactId> <name>wechaty-examples</name> <version>1.0.0-SNAPSHOT</version> <properties> <kotlin.compiler.incremental>true</kotlin.compiler.incremental> <kotlin.version>1.3.72</kotlin.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>io.github.wechaty</groupId> <artifactId>wechaty</artifactId> <version>0.1.5-SNAPSHOT</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.13.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.13.2</version> </dependency> <dependency> <!-- 桥接:告诉Slf4j使用Log4j2 --> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.13.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.lmax/disruptor --> <dependency> <groupId>com.lmax</groupId> <artifactId>disruptor</artifactId> <version>3.4.2</version> </dependency> </dependencies> <repositories> <repository> <id>oss-sonatype</id> <name>oss-sonatype</name> <url>https://oss.sonatype.org/content/repositories/snapshots/</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>alimaven</id> <name>aliyun maven</name> <url>https://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyun-plugin</id> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> <executions> <!-- 替换会被 maven 特别处理的 default-compile --> <execution> <id>default-compile</id> <phase>none</phase> </execution> <!-- 替换会被 maven 特别处理的 default-testCompile --> <execution> <id>default-testCompile</id> <phase>none</phase> </execution> <execution> <id>java-compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.1.1</version> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <archive> <manifest> <mainClass>io.github.wechaty.DingDongBot</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
import org.apache.commons.lang3.StringUtils; import io.github.wechaty.Wechaty; import io.github.wechaty.filebox.FileBox; import io.github.wechaty.schemas.MessageType; import io.github.wechaty.user.Contact; import io.github.wechaty.user.Room; import io.github.wechaty.utils.QrcodeUtils; public class DingDongBot { public static void main(String[] args) throws Exception { DingDongBot.run(); } public static void run() throws Exception { // final String token = System.getenv("WECHATY_PUPPET_SERVICE_TOKEN"); final String token = "wechaty 控制台显示的token"; if (StringUtils.isBlank(token)) { System.out.println("Error: WECHATY_PUPPET_SERVICE_TOKEN is not found in the environment variables"); System.out.println("You need a TOKEN to run the Java Wechaty. Please goto our README for details"); System.out.println("https://github.com/wechaty/java-wechaty-getting-started/#run"); throw new Exception("need a token"); } Wechaty bot = Wechaty.instance(token); bot.onScan((qrcode, statusScanStatus, data) -> { System.out.println(QrcodeUtils.getQr(qrcode)); System.out.println("Online Image: https://wechaty.github.io/qrcode/" + qrcode); }); bot.onMessage(message -> { Contact from = message.from(); Room room = message.room(); String text = message.text(); MessageType type = message.type(); if (type == MessageType.Image) { FileBox file = message.file(); String base64 = file.getBase64(); String fileName = "D:\\Docker\\image\\tmp.jpg"; ImageUtil.convertBase64StrToImage(base64, fileName); System.out.println("hao"); } if (StringUtils.equals(text, "你好!")) { if (room != null) { room.say("大家好才是真的好!"); } else { from.say("大家好才是真的好!"); } } else if (StringUtils.equals(text, "今天的心情不错嘛!")) { if (room != null) { room.say("当然,心情好,一切都没好!"); } else { from.say("当然,心情好,一切都没好!"); } } else if (StringUtils.equals(text, "你是微信机器人吗?")) { if (room != null) { room.say("是的,可以接受和发送文本消息和图文消息!如果结合ORC技术则可以绑定业务,减轻人类的工作量!比如我可以识别:身份证和行驶证后,帮助保司人员进行车险报价!"); } else { from.say("是的,可以接受和发送文本消息和图文消息!如果结合ORC技术则可以绑定业务,减轻人类的工作量!比如我可以识别:身份证和行驶证后,帮助保司人员进行车险报价!"); } } }); bot.start(true); } }
import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.Base64; import javax.imageio.ImageIO; public class ImageUtil { /** * 图片转Base64字符串 * * @param imageFileName * @return */ public static String convertImageToBase64Str(String imageFileName) { ByteArrayOutputStream baos = null; try { // 获取图片类型 String suffix = imageFileName.substring(imageFileName.lastIndexOf(".") + 1); // 构建文件 File imageFile = new File(imageFileName); // 通过ImageIO把文件读取成BufferedImage对象 BufferedImage bufferedImage = ImageIO.read(imageFile); // 构建字节数组输出流 baos = new ByteArrayOutputStream(); // 写入流 ImageIO.write(bufferedImage, suffix, baos); // 通过字节数组流获取字节数组 byte[] bytes = baos.toByteArray(); // 获取JDK8里的编码器Base64.Encoder转为base64字符 return Base64.getEncoder().encodeToString(bytes); } catch (Exception e) { e.printStackTrace(); } finally { try { if (baos != null) { baos.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; } /** * Base64字符串转图片 * * @param base64String * @param imageFileName */ public static void convertBase64StrToImage(String base64String, String imageFileName) { ByteArrayInputStream bais = null; try { // 获取图片类型 String suffix = imageFileName.substring(imageFileName.lastIndexOf(".") + 1); // 获取JDK8里的解码器Base64.Decoder,将base64字符串转为字节数组 byte[] bytes = Base64.getDecoder().decode(base64String); // 构建字节数组输入流 bais = new ByteArrayInputStream(bytes); // 通过ImageIO把字节数组输入流转为BufferedImage BufferedImage bufferedImage = ImageIO.read(bais); // 构建文件 File imageFile = new File(imageFileName); // 写入生成文件 ImageIO.write(bufferedImage, suffix, imageFile); } catch (Exception e) { e.printStackTrace(); } finally { try { if (bais != null) { bais.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。