TelegramBots 机器人

  1. 申请机器人
    telegram网页版
    扫码登录,再搜索 @BotFather
    在这里插入图片描述
    搜索到后打开会话
    在这里插入图片描述
  2. 自定义机器人
    我们需要创建一个springboot项目,并引入依赖,下面是GitHub的地址:
    TelegramBots
    <dependency>
        <groupId>org.telegram</groupId>
        <artifactId>telegrambots</artifactId>
        <version>5.7.1</version>
    </dependency>

创建配置类

package com.ddz.demo20220329.config;

import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;

/**
 * @author Administrator
 * @version V1.0
 * @date 2022/4/9
 */
@Configuration
public class BotConfig {
	// 梯子的IP,我的是本地的
    public static final String proxyHost = "127.0.0.1";
    // 本地监听的端口
    public static final int proxyPort = 10801;

    @Bean
    public DefaultBotOptions defaultBotOptions() {
        DefaultBotOptions botOptions = new DefaultBotOptions();
        botOptions.setProxyHost(proxyHost);
        botOptions.setProxyPort(proxyPort);
        //ProxyType是个枚举
        botOptions.setProxyType(DefaultBotOptions.ProxyType.SOCKS5);
        return botOptions;
    }

    @Bean
    public DefaultBotSession DefaultBotSession() {
        DefaultBotSession defaultBotSession = new DefaultBotSession();
        defaultBotSession.setOptions(defaultBotOptions());
        return defaultBotSession;
    }

    @SneakyThrows
    @Bean
    public TelegramBotsApi telegramBotsApi() {
        return new TelegramBotsApi(DefaultBotSession().getClass());
    }
}

创建自定义机器人,继承 TelegramLongPollingBot 类

	 //填你自己的token和username
    private final String token = "*****";
    private final String username = "*****";

    public DdzBot(DefaultBotOptions options) {
        super(options);
    }

    @Override
    public String getBotToken() {
        return this.token;
    }

    @Override
    public String getBotUsername() {
        return this.username;
    }

    /**
     * 监听机器人的消息
     */
    @SneakyThrows
    @Override
    public void onUpdateReceived(Update update) {
        if (update.hasMessage()) {
            Message message = update.getMessage();
            Long chatId = message.getChatId();
            String text = message.getText();
            switch (text) {
                case "/a":
                    text = "AAAAAAAAAA";
                    break;
                case "/b":
                    text = "BBBBBBBBBB";
                    break;
                case "/c":
                    text = "CCCCCCCCCC";
                    break;
                default:
                    text = "不处理该类指令";
                    break;
            }
            this.sendTextMsg(text, chatId.toString());
        }
    }


    /**
     * 发送文本消息
     *
     * @param text   内容
     * @param chatId 内容ID
     */
    @SneakyThrows
    @Async
    public void sendTextMsg(String text, String chatId) {
        SendMessage response = new SendMessage();
        response.setDisableNotification(false);
        response.setChatId(chatId);
        response.setText(text);
        executeAsync(response);
    }
}

创建控制器启动服务

package com.ddz.demo20220329.controller;

import com.ddz.demo20220329.bot.DdzBot;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;

import javax.annotation.Resource;

/**
 * @author Administrator
 * @version V1.0
 * @date 2022/4/9
 */
@RestController
public class BotController {
    @Resource
    private TelegramBotsApi telegramBotsApi;
    @Resource
    private DefaultBotOptions defaultBotOptions;

    @GetMapping("start")
    public String start() {
        try {
            DdzBot ddzBot = new DdzBot(defaultBotOptions);
            telegramBotsApi.registerBot(ddzBot);
        } catch (TelegramApiException e) {
            return "启动失败";
        }
        return "启动成功";
    }
}

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐