java 电报机器人——使用 API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30156609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:33:59  来源:igfitidea点击:

A Telegram Bot -- Using APIs

javawikipediatelegram-bot

提问by Quontas

So I have limited experience in Java (have some experience with APIs but little experience making bots or clients). However, I have switched to using Telegram instead of iMessage simply because it's easier to use and more responsive. I also appreciate the open-source nature and the ability for me to create a bot. But I don't know much in terms of other languages, and am only a student. I was wondering if it was possible to create a bot that, when a certain string is entered, followed by a term, the bot searches Wikipedia and returns the first page that matches the term (or some exception).

所以我在 Java 方面的经验有限(有一些 API 经验,但几乎没有制作机器人或客户端的经验)。但是,我已经改用 Telegram 而不是 iMessage,因为它更易于使用且响应速度更快。我也很欣赏开源性质和我创建机器人的能力。但我对其他语言了解不多,只是一个学生。我想知道是否有可能创建一个机器人,当输入某个字符串时,后跟一个术语,机器人搜索维基百科并返回与该术语匹配的第一页(或某些例外)。

Lofty idea... but I have no idea what I'm doing. Does anyone know if there's a place I could find a tutorial (not Youtube) or if somebody with more knowledge than me (most people who read this) could teach me? That'd be amazing.

崇高的想法......但我不知道我在做什么。有谁知道是否有一个地方我可以找到教程(不是 Youtube),或者是否有比我知识丰富的人(大多数阅读本文的人)可以教我?那太棒了。

My background: Processing AP Computer Science A Limited API use

我的背景:处理 AP 计算机科学 A 有限的 API 使用

TL;DR Wikipedia bot for Telegram, need help.

TL;DR 用于 Telegram 的 Wikipedia bot,需要帮助。

采纳答案by Stas Parshin

My simple Java API for Telegram bots

我的用于 Telegram 机器人的简单 Java API

https://github.com/pengrad/java-telegram-bot-api

https://github.com/pengrad/java-telegram-bot-api

compile 'com.github.pengrad:java-telegram-bot-api:3.6.0'

回答by mcont

回答by Peter

Maybe this Java API is what you are searching for: TelegramBot JavaAPI

也许这个 Java API 就是你要搜索的:TelegramBot JavaAPI

You have to create a class implementing IReceiverService and then something like that:

您必须创建一个实现 IReceiverService 的类,然后是这样的:

public class GetMessage implements IReceiverService {

    @Override
    public void received(Message message) {
        switch (message.getMessageType()) {
        case TEXT_MESSAGE:
            String text = message.getMessage().toString();

            // Look up Wikipedia with
            // https://en.m.wikipedia.org/wiki/<text>
            // process the response and send it back.
            String wiki = "My processed wiki content.";

            Sender.send(message.getSender().getId(), wiki);

            break;
        default:
            System.out.println("Ignore received message.");
        }
    }
}

回答by Developer Marius ?il?nas

That is possible when you have a specific search term. You can search Wikipedia when you read this:

当您有特定的搜索词时,这是可能的。阅读本文时,您可以搜索维基百科:

Do try

尝试

To see how to read contents from an url read Java tutorialand then "Java notes v7" book by David J. Eck, section 11.4

要了解如何从 url 读取内容,请阅读Java 教程,然后阅读David J. Eck 的“Java notes v7”一书,第 11.4 节

回答by Seeya

It is possible to do it. You can check out this telegram-botcreated on github. It's written in LUA.

有可能做到这一点。您可以查看在 github 上创建的这个电报机器人。它是用 LUA 编写的。

The idea you were thinking about has already been done. There's a plugin for wikipediausing that bot. I suggest reading the source code and from there try to make sense out of it and then try making another plugin on your own referring to the source codes found in the plugin folder.

你的想法已经实现了。有一个使用该机器人的维基百科插件。我建议阅读源代码并从中尝试理解它,然后参考插件文件夹中的源代码尝试自己制作另一个插件。