如何通过电报机在机器人和用户之间进行对话 [英] How can make conversation between bot and user with telepot

查看:210
本文介绍了如何通过电报机在机器人和用户之间进行对话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用telepot创建一个向用户询问常见问题的机器人. 例如,首先问你叫什么名字??"然后用户回答用户名",然后问您几岁?然后用户回答他的年龄和...

I want to create the bot with telepot that ask the users frequent questions. For example first ask 'whats your name.?' then the user reply 'user-name',then ask how old are you? and the user reply his age and ...

我已经为用户和机器人之间的聊天编写了代码,但有时会出错.请指导我如何使用telepot制作此机器人.?

I had written a code for this chat between user and bot,but sometimes I am getting error. Please guide me how can I make this bot with telepot.?

我想使用telepot

推荐答案

我不再维护此库.感谢您的考虑 电话. -维护者nickoala

I am no longer maintaining this library. Thanks for considering telepot. - the maintainer, nickoala


您要寻找的是DelegatorBot. 考虑本教程.


What you're looking for is DelegatorBot. Consider this tutorial.

请考虑这种情况.机器人想拥有一个聪明的人 与很多用户的对话,如果我们只能使用一个 执行行以处理消息(例如到目前为止我们所做的事情), 我们将不得不维护每个对话的一些状态变量 在消息处理功能之外.收到每条消息后, 我们首先必须检查用户是否已经进行了对话 开始,如果是这样,我们一直在谈论什么.为了避免这种情况 平凡,我们需要一种结构化的方式来维护 对话.

Consider this scenario. A bot wants to have an intelligent conversation with a lot of users, and if we could only use a single line of execution to handle messages (like what we have done so far), we would have to maintain some state variables about each conversation outside the message-handling function(s). On receiving each message, we first have to check whether the user already has a conversation started, and if so, what we have been talking about. To avoid such mundaneness, we need a structured way to maintain "threads" of conversation.

DelegatorBot为每个用户提供了一个bot实例,因此您不必考虑多个用户交谈时会发生什么. (如果有帮助,请随时参阅我如何使用它.)
本教程的示例是一个简单的计数器,用于统计用户发送了多少消息:

DelegatorBot provides you with one instance of your bot for every user, so you don't have to think about what happens when multiple users talk to it. (If it helps you, feel free to have a look at how I am using it.)
The tutorial's example is a simple counter of how many messages the user has sent:

import sys
import time
import telepot
from telepot.loop import MessageLoop
from telepot.delegate import pave_event_space, per_chat_id, create_open

class MessageCounter(telepot.helper.ChatHandler):
    def __init__(self, *args, **kwargs):
        super(MessageCounter, self).__init__(*args, **kwargs)
        self._count = 0

    def on_chat_message(self, msg):
        self._count += 1
        self.sender.sendMessage(self._count)

TOKEN = sys.argv[1]  # get token from command-line

bot = telepot.DelegatorBot(TOKEN, [
    pave_event_space()(
        per_chat_id(), create_open, MessageCounter, timeout=10),
])
MessageLoop(bot).run_as_thread()

while 1:
    time.sleep(10)

此代码为每个用户创建MessageCounter的实例.

This code creates an instance of MessageCounter for every individual user.

我已经为用户和机器人之间的聊天编写了代码,但有时会出错.

I had written a code for this chat between user and bot,but sometimes I am getting error.

如果您的问题是关于所收到的错误,而不是如何与状态进行对话,则需要提供有关所收到的错误以及何时出现错误的更多信息.

If your question was rather about the errors you're getting than about how to keep a conversation with state, you need to provide more information about what errors you're getting, and when those appear.

这篇关于如何通过电报机在机器人和用户之间进行对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆