package com.hkphantom.earch.bot;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hkphantom.earch.util.NsHttp;
import com.vdurmont.emoji.EmojiParser;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.ParseMode;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 电报搜群机器人
*/
public class TelegramSearch extends TelegramLongPollingBot {
private String token = ""; // 电报机器人token
private String userName = ""; // 电报机器人 userName
String proxyHost = "127.0.0.1"; // 本地代理
String proxyPort = "10809"; // 本地代理端口
String httpUrl = "https://www.googleapis.com/customsearch/v1"; // 谷歌搜索引擎地址
String cx = "b0bcb1b09813012d8"; // 搜索引擎编号
String h1 = "zh-CN";
String key = ""; // 搜索引擎API秘钥
Map<String, HashMap> map = new HashMap<String, HashMap>();
public TelegramSearch() {
this(new DefaultBotOptions());
}
public TelegramSearch(DefaultBotOptions options) {
super(options);
}
/**
* 处理搜索查询
*
* @param update
*/
@Override
public void onUpdateReceived(Update update) {
if (update.hasMessage()) {// 处理普通文本消息
// 获取查询关键字
Message message = update.getMessage();
if (message.hasText()) {
// 打印日志
this.log(update);
String message_text = message.getText();//发送的消息内容
Long chat_id = message.getChatId(); //发送信息的用户id
//执行查询结果
StringBuffer stringBuffer = search(chat_id, message_text);
if (stringBuffer == null) {
return;
}
SendMessage sendMessage = getSendMessage(stringBuffer.toString(), message.getChatId());
try {
execute(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
} else if (update.hasCallbackQuery()) { // 处理内联消息
CallbackQuery callbackQuery = update.getCallbackQuery();
Message msg = callbackQuery.getMessage();
String data = callbackQuery.getData();
SendMessage sendMessage = new SendMessage();
if (data.equals("下一页")) {
String s = "";
// 获取上次关键词和开始页码
HashMap hashMap = map.get(String.valueOf(msg.getChatId()));
System.out.println("hashMap==========" + hashMap);
if (hashMap == null) {
s = EmojiParser.parseToUnicode("只能查询自己的关键词 ");
} else {
StringBuffer search = search(msg.getChatId(), "");
System.out.println(map);
s = search.toString();
}
sendMessage = getSendMessage(s, msg.getChatId());
}
try {
execute(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
} else {
return;
}
}
@Nullable
private StringBuffer search(Long chat_id, String q) {
//进来查询先创建查询数据
//查询是否已经创建记录
// 如果包含就是下一页直接获取,如果没有记录就是第一次查询创建记录
if (!map.containsKey(chat_id) && StringUtils.isNotBlank(q)) {
HashMap<String, String> dataMap = new HashMap<>();
dataMap.put("chatId", String.valueOf(chat_id));
dataMap.put("q", q);
dataMap.put("start", String.valueOf(1));
System.out.println(dataMap);
map.put(String.valueOf(chat_id), dataMap);
}
// 对http开启代理
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", proxyPort);
// 对https也开启代理
System.setProperty("https.proxyHost", proxyHost);
System.setProperty("https.proxyPort", proxyPort);
//根据关键词进行查询
//获取关键词
HashMap hashMap = map.get(String.valueOf(chat_id));
String start = hashMap.get("start").toString();
NsHttp nsHttp = new NsHttp();
HashMap<String, Object> paramsMap = new HashMap<>();
paramsMap.put("key", key);
paramsMap.put("cx", cx);
paramsMap.put("h1", h1);
paramsMap.put("q", hashMap.get("q"));
paramsMap.put("start", start);
paramsMap.put("num", 10);
System.out.println("start=======" + hashMap.get("start"));
String response = nsHttp.doGet(httpUrl, paramsMap);
if (StringUtils.isBlank(response)) {
return null;
}
JSONObject jsonObject = JSONObject.parseObject(response);
System.out.println(jsonObject);
JSONArray items = jsonObject.getJSONArray("items");
StringBuffer stringBuffer = new StringBuffer();
System.out.println(items);
Integer j = Integer.valueOf(start);
for (int i = 0; i < items.size(); i++) {
JSONObject item = (JSONObject) items.get(i);
String link = item.getString("link");
if (link.contains("www.")) {
link = link.replace("www.", "");
}
if (link.contains("/s")) {
link = link.replace("/s", "");
}
String title = item.getString("title");
if (title.contains("–")) {
title = title.replace("–", "");
}
if (title.contains("Telegram")) {
title = title.replace("Telegram", "");
}
title = title.replaceAll("", "");
String authorizeStr = EmojiParser.parseToUnicode(
(j) + ". :bust_in_silhouette: <a href=\"" + link + "\">" + title + "</a>\n\n"
);
stringBuffer.append(authorizeStr);
j++;
}
//查询结束更新map的值
hashMap.put("start", Integer.valueOf(hashMap.get("start").toString()) + 10);
map.put(String.valueOf(chat_id), hashMap);
System.out.println(stringBuffer);
return stringBuffer;
}
@Override
public String getBotUsername() {
return this.userName;
}
@Override
public String getBotToken() {
return this.token;
}
//发送普通消息
private SendMessage getSendMessage(String message_text, Long chat_id) {
SendMessage message = new SendMessage();// Create a message object object
message.setChatId(String.valueOf(chat_id));
message.setText(message_text);
message.setParseMode(ParseMode.HTML);
//�
没有合适的资源?快使用搜索试试~ 我知道了~
Telegram 搜群机器人 springboot+谷歌搜索引擎实现
共141个文件
xml:115个
png:11个
java:6个
需积分: 5 9 下载量 173 浏览量
2022-04-26
17:13:47
上传
评论 5
收藏 999KB ZIP 举报
温馨提示
springboot+谷歌搜索引擎实现Telegram搜群机器人 Telegram搜群机器人 提供实现思路方法 内附详细图文说明,替换参数即可运行。 供学习,和参考。 若有不足,欢迎互相学习。
资源详情
资源评论
资源推荐
收起资源包目录
Telegram 搜群机器人 springboot+谷歌搜索引擎实现 (141个子文件)
mvnw.cmd 7KB
.gitignore 395B
.gitignore 184B
telegram_search.iml 13KB
maven-wrapper.jar 57KB
TelegramSearch.java 10KB
NsHttp.java 8KB
RetryIntercepter.java 1KB
StartMain.java 1KB
TelegramSearchApplication.java 340B
TelegramSearchApplicationTests.java 228B
Telegram搜群机器人.md 24KB
mvnw 10KB
telegram1.png 264KB
father2.png 165KB
google4.png 112KB
google5.png 77KB
google3.png 70KB
google1.png 69KB
idea1.png 40KB
google2.png 32KB
father4.png 30KB
father3.png 24KB
father1.png 10KB
maven-wrapper.properties 233B
uiDesigner.xml 9KB
workspace.xml 4KB
pom.xml 2KB
jarRepositories.xml 1KB
Maven__com_google_guava_listenablefuture_9999_0_empty_to_avoid_conflict_with_guava.xml 958B
Maven__com_fasterxml_jackson_module_jackson_module_jaxb_annotations_2_13_2.xml 866B
Maven__com_fasterxml_jackson_module_jackson_module_parameter_names_2_13_2.xml 859B
Maven__org_glassfish_jersey_containers_jersey_container_grizzly2_http_2_35.xml 857B
compiler.xml 836B
Maven__org_springframework_boot_spring_boot_test_autoconfigure_2_6_7.xml 836B
Maven__com_fasterxml_jackson_jaxrs_jackson_jaxrs_json_provider_2_13_2.xml 834B
Maven__com_vaadin_external_google_android_json_0_0_20131108_vaadin1.xml 823B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jsr310_2_13_2.xml 818B
Maven__org_springframework_boot_spring_boot_starter_logging_2_6_7.xml 815B
Maven__org_springframework_boot_spring_boot_starter_tomcat_2_6_7.xml 808B
Maven__com_fasterxml_jackson_datatype_jackson_datatype_jdk8_2_13_2.xml 804B
Maven__org_glassfish_jersey_media_jersey_media_json_jackson_2_35.xml 802B
Maven__org_springframework_boot_spring_boot_autoconfigure_2_6_7.xml 801B
Maven__org_springframework_boot_spring_boot_starter_json_2_6_7.xml 794B
Maven__org_springframework_boot_spring_boot_starter_test_2_6_7.xml 794B
Maven__org_glassfish_hk2_external_aopalliance_repackaged_2_6_1.xml 788B
Maven__org_springframework_boot_spring_boot_starter_web_2_6_7.xml 787B
Maven__org_apache_tomcat_embed_tomcat_embed_websocket_9_0_62.xml 783B
Maven__org_glassfish_jersey_ext_jersey_entity_filtering_2_35.xml 780B
Maven__com_google_errorprone_error_prone_annotations_2_3_4.xml 775B
Maven__com_fasterxml_jackson_core_jackson_annotations_2_13_2.xml 774B
Maven__com_fasterxml_jackson_jaxrs_jackson_jaxrs_base_2_13_2.xml 771B
Maven__com_fasterxml_jackson_core_jackson_databind_2_13_2_1.xml 767B
Maven__org_springframework_boot_spring_boot_starter_2_6_7.xml 759B
Maven__org_jetbrains_kotlin_kotlin_stdlib_common_1_6_21.xml 757B
Maven__org_junit_platform_junit_platform_commons_1_8_2.xml 756B
Maven__jakarta_validation_jakarta_validation_api_2_0_2.xml 756B
Maven__jakarta_annotation_jakarta_annotation_api_1_3_5.xml 756B
Maven__jakarta_activation_jakarta_activation_api_1_2_2.xml 756B
Maven__org_junit_platform_junit_platform_engine_1_8_2.xml 749B
Maven__org_apache_tomcat_embed_tomcat_embed_core_9_0_62.xml 748B
Maven__org_glassfish_grizzly_grizzly_http_server_2_4_4.xml 747B
Maven__org_glassfish_hk2_osgi_resource_locator_1_0_3.xml 745B
Maven__org_springframework_boot_spring_boot_test_2_6_7.xml 738B
Maven__org_junit_jupiter_junit_jupiter_params_5_8_2.xml 738B
Maven__org_junit_jupiter_junit_jupiter_engine_5_8_2.xml 738B
Maven__org_apache_tomcat_embed_tomcat_embed_el_9_0_62.xml 734B
Maven__jakarta_xml_bind_jakarta_xml_bind_api_2_3_3.xml 734B
Maven__org_glassfish_grizzly_grizzly_framework_2_4_4.xml 733B
Maven__org_glassfish_hk2_external_jakarta_inject_2_6_1.xml 732B
Maven__org_springframework_spring_expression_5_3_19.xml 732B
Maven__org_apache_logging_log4j_log4j_to_slf4j_2_17_2.xml 731B
Maven__com_fasterxml_jackson_core_jackson_core_2_13_2.xml 725B
Maven__org_mockito_mockito_junit_jupiter_4_0_0.xml 721B
Maven__org_junit_jupiter_junit_jupiter_api_5_8_2.xml 717B
Maven__org_glassfish_jersey_core_jersey_client_2_35.xml 714B
Maven__org_glassfish_jersey_core_jersey_server_2_35.xml 714B
Maven__org_glassfish_jersey_core_jersey_common_2_35.xml 714B
Maven__org_springframework_spring_context_5_3_19.xml 711B
Maven__com_google_j2objc_j2objc_annotations_1_3.xml 710B
Maven__org_jetbrains_kotlin_kotlin_stdlib_1_6_21.xml 708B
Maven__net_bytebuddy_byte_buddy_agent_1_11_22.xml 708B
Maven__org_apache_httpcomponents_httpclient_4_5_13.xml 707B
Maven__org_springframework_spring_webmvc_5_3_19.xml 704B
Maven__org_springframework_boot_spring_boot_2_6_7.xml 703B
Maven__org_glassfish_jersey_inject_jersey_hk2_2_35.xml 701B
Maven__jakarta_ws_rs_jakarta_ws_rs_api_2_1_6.xml 701B
Maven__ch_qos_logback_logback_classic_1_2_11.xml 698B
Maven__org_glassfish_grizzly_grizzly_http_2_4_4.xml 698B
Maven__org_telegram_telegrambots_meta_5_7_1.xml 697B
Maven__org_springframework_spring_beans_5_3_19.xml 697B
Maven__org_apache_logging_log4j_log4j_api_2_17_2.xml 696B
Maven__org_apiguardian_apiguardian_api_1_1_2.xml 695B
Maven__org_checkerframework_checker_qual_3_5_0.xml 694B
Maven__org_apache_httpcomponents_httpmime_4_5_13.xml 693B
Maven__org_apache_commons_commons_lang3_3_8_1.xml 693B
Maven__org_apache_httpcomponents_httpcore_4_4_15.xml 693B
Maven__org_springframework_spring_test_5_3_19.xml 690B
Maven__org_springframework_spring_core_5_3_19.xml 690B
Maven__org_junit_jupiter_junit_jupiter_5_8_2.xml 689B
共 141 条
- 1
- 2
HKkkkkSky
- 粉丝: 21
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0