云计算、AI、云原生、大数据等一站式技术学习平台

网站首页 > 教程文章 正文

企业微信接入DeepSeek,实现企微畅聊,告别服务器繁忙

jxf315 2025-05-14 14:21:29 教程文章 29 ℃

前言

随着企业智能化需求的增长,将AI能力集成到内部办公系统成为趋势。本文详细讲解如何通过企业微信自建应用调用DeepSeek接口,实现智能问答、文本分析等功能。在和DeepSeek官方问答时,会经常出现服务器繁忙,如下图:

DeepSeek开放接口

基于此,我们可以自己对接接口,开发一套属于自己的智能问答机器人,除了官方对外的接口开放平台,腾讯、阿里这些大厂也部署了满血版的DeepSeek,也对外提供接口,本篇文章就基于阿里云提供的接口,来和企微对接。

DeepSeek开放平台网址:
https://api-docs.deepseek.com/zh-cn/


阿里云百炼平台,可以根据平台API接口进行调用,支持Python、Java、HTTP等调用。


企业微信应用

企业微信侧配置

  • 创建自建应用

1、登录企业微信管理后台



2、进入「应用管理」→「自建应用」→「创建应用」



3、记录AgentId、Secret和企业CorpId



  • 配置可信域名
  1. 在「我的企业」→「安全与保密」设置API接收消息的域名




  1. 需完成域名所有权验证(上传验证文件)


阿里云侧:

1、在百炼大模型平台申请key(网址是:
https://bailian.console.aliyun.com/)


2、创建一个key,用于程序调用


代码实现,Talk is cheap,show me the code

1、SpringBoot+Maven项目,在pom.xml文件中引入jar包

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dashscope-sdk-java</artifactId>
            <version>2.18.2</version>
        </dependency>


2、配置的应用回调地址对应的接口中,获取应用对话的消息:

// 阿里云deepseek接口
        logger.info("接收到消息=="+map);
        if (map.containsKey("AgentID")) {
            String content = map.get("Content").toString();
            String fromUserName = map.get("FromUserName").toString();
            Text frontText = new Text();
            frontText.setContent("请稍后...");
            TextMessage frontTextMessage = new TextMessage();
            frontTextMessage.setText(frontText);
            frontTextMessage.setMsgtype("text");
            frontTextMessage.setTouser(fromUserName);
            sendMessageService.sendMessage(frontTextMessage);
            String result = singleChatService.getResult(content);
            Text text = new Text();
            text.setContent(result);
            TextMessage textMessage = new TextMessage();
            textMessage.setText(text);
            textMessage.setMsgtype("text");
            textMessage.setTouser(fromUserName);
            sendMessageService.sendMessage(textMessage);
            return;
        }

回调的事件如果包含AgentID,就代表有人向企微应用发送消息。

3、调用接口实现智能问答

package com.demo.base.deepseek.service.impl;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.demo.base.deepseek.service.SingleChatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Arrays;


@Service
public class SingleChatServiceImpl implements SingleChatService {
    private static final Logger logger = LoggerFactory.getLogger(SingleChatServiceImpl.class);

    public static GenerationResult callWithMessage(String content) throws ApiException, NoApiKeyException, InputRequiredException {
        logger.info("开始思考:");
        Generation gen = new Generation();
        String stringBuilder = content;
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content(stringBuilder)
                .build();
        GenerationParam param = GenerationParam.builder()
                // 若没有配置环境变量,请用百炼API Key将下行替换为:.apiKey("sk-xxx")
                //.apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .apiKey("sk-*************************")
                .model("deepseek-r1")
                .messages(Arrays.asList(userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();
        return gen.call(param);
    }

    @Override
    public String getResult(String content) {
        try {
            GenerationResult result = callWithMessage(content);
            logger.info("思考过程:");
            logger.info(result.getOutput().getChoices().get(0).getMessage().getReasoningContent());
            logger.info("回复内容:");
            String reslutContent = result.getOutput().getChoices().get(0).getMessage().getContent();
            logger.info(reslutContent);
            return reslutContent;
        } catch (NoApiKeyException | InputRequiredException e) {
            logger.info("生成文本发送错误,{}", e.getMessage());
        }
        return "小助手还在努力训练,争取给出准确的回答!";
    }
}

完成接口对接,当程序收到企微应用的消息,会把内容对接到deepseek进行分析推理并输出内容。

对话效果


以后这个企微应用,就是你的智能助理!

Tags:

最近发表
标签列表