Telegram的bot创建和开发

创建机器人

telegram机器人创建和discord里机器人的创建区别很大,tg里所有创建流程都是在频道里发送消息完成。
根据如下导航创建属于你的telegram机器人:https://www.teleme.io/articles/create_your_own_telegram_bot?hl=zh-hans

1.先在BotFather频道里创建你的机器人

https://telegram.me/BotFather
加入father频道
创建完成之后,会发送你关于机器人的API相关的文档链接。和一些指令发送。
根据指令设置你的机器人名称和username,并且获取你唯一的token访问秘钥。
设置名称获得token秘钥
创建完成之后你的页面会多出一个你命名的机器人,在机器人频道发送**/start**消息,启动机器人。
当然你也可以代码直接编写启动命令,启动你的机器人。

根据token测试机器人

根据官方提供的机器人访问接口,测试你的机器人接口API。
https://api.telegram.org/bot你的秘钥/getMe
访问成功示例

编写你的机器人应用

如果你的机器人用于群组管理和应用,需要提前把机器人加入对应的群组里,按需要设置是否为管理员。
方便用于测试和调试。
机器人的消息接收又两种模式,一种update模式,一种webhook模式。
代码展示update模式:
github上有许多封装好的关于telegram相关bot的代码,我引用的是telegrambots库。

    <!--tg boot-->
    <dependency>
      <groupId>org.telegram</groupId>
      <artifactId>telegrambots</artifactId>
      <version>6.8.0</version>
    </dependency>

示例为用户加入群组发送欢迎信息示例,下面展示核心代码:

import com.tg.bot.BotConfig;
import com.tg.entity.TelegramJoin;
import com.tg.mapper.BotMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

import java.util.List;

@Service
public class BotTowServiceImpl extends TelegramLongPollingBot {

    private final BotConfig config;

    public BotTowServiceImpl(BotConfig config) {
        super(config.getDefaultBotOptions("off"));
        this.config = config;
    }

    @Override
    public String getBotUsername() { //todo 机器人名称
        return config.getBotname();
    }

    @Override
    public String getBotToken() { //todo 密钥
        return config.getToken();
    }

    @Override
    public void onUpdateReceived(Update update) { //todo 回调
        if(update.hasMessage()){
            String message_text = update.getMessage().getText();
            System.out.println(message_text);
            List<User> users = update.getMessage().getNewChatMembers();
            if(users.size()>0){
                long chat_id = update.getMessage().getChatId();
                String group = update.getMessage().getChat().getTitle();
//                message.setText(message_text);
                for (User user:users) {
                    String lastName = user.getLastName()==null?"":user.getLastName();
                    message_text = "Hey,"+user.getFirstName()+" "+lastName+",Welcome to join "+group;
                    SendMessage message = new SendMessage(); // 创建消息对象
                    message.setChatId(chat_id);
                    message.setText(message_text);
                    try {
                        execute(message); // 发送我们的消息对象给用户
                    } catch (TelegramApiException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}

在应用过程中发现etUpdates 方式,API访问速率限制,如果你的服务器网络和配置不是太好,这会出现如下报错。

Error executing org.telegram.telegrambots.meta.api.methods.updates.GetUpdates query: [429] Too Many Requests: retry after 5

如果不想被API访问速率限制,你可以设置哦webhook的方式,具体配置官方文档写的太抽象看不懂,有会搞得欢迎留言私信讨论。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值