当前位置:   article > 正文

微信机器人java-wechaty

java-wechaty

微信机器人java-wechaty

wechaty docker微服务

# 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
  • 1
  • 2
  • 3
  • 4
  • 5

获取pad token

获取pad token网址:http://pad-local.com

java pom.xml

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


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157

java代码

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);
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

ImageUtil工具类

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

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/178526
推荐阅读
相关标签
  

闽ICP备14008679号