在机器人中选择答案

时间:2018-10-23 02:01:22

标签: python-3.x bots

电报中有一个漫游器。您能告诉我如何在出现address_text消息后提问吗?

即,用户输入了address_text,并且在分别按下2个按钮后,分别输入了他输入的文本和该信息通过变量result_text发送给用户的时间。

我该怎么办?

import asyncio
import logging
import datetime
from constants2 import *
from utils import *
import pandas as pd
from io import StringIO
from aiogram import Bot, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import Dispatcher
from aiogram.utils.executor import start_polling
from aiogram.types import ReplyKeyboardMarkup, ReplyKeyboardRemove


logging.basicConfig(level=logging.INFO)

loop = asyncio.get_event_loop()
bot = Bot(token=token, loop=loop)
storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage)

# states
BUY_TOKENS = 'Buy_tokens'
ENTRY_ADDRESS = 'Entry_address'
ENTRY_HASH = 'Entry_hash'


markup = ReplyKeyboardMarkup(resize_keyboard=True, selective=True)
markup.row(view_course_button, buy_tokens_button)

@dp.message_handler(state = '*', commands=['start'])
async def send_welcome(message: types.Message):
    await bot.send_message(message.from_user.id, text=greeting_text, reply_markup=markup)

@dp.message_handler(state = '*', commands=['answer'])
async def answer(message: types.Message):
    if message.chat.id == send_result_id:
        try:
            spl = message.text.split()
            user_id = spl[1]
            answer = spl[2]
            text = answers[int(answer)]
            await bot.send_message(user_id, text=text)
        except:
            pass

# entry sum and code
@dp.message_handler(state = '*', func=lambda message: message.text == buy_tokens_button)
async def entry_sum(message: types.Message):
    with dp.current_state(chat=message.chat.id, user=message.from_user.id) as state:
        await state.set_state(BUY_TOKENS)
    await bot.send_message(message.from_user.id, text=entry_tokens_amount_text, reply_markup=ReplyKeyboardRemove())

@dp.message_handler(state = BUY_TOKENS, regexp='^[0-9]\d*(\.\d+)?$')
async def entry_address(message: types.Message):
    address1 = get_adress()
    with dp.current_state(chat=message.chat.id, user=message.from_user.id) as state:
        await state.update_data(sum=message.text, address1=address1)
        await state.set_state(ENTRY_ADDRESS)
    price = get_course()
    text = address_text%(message.text, price, float(message.text)*price, address1)
    await bot.send_message(message.from_user.id, text=text)


@dp.message_handler(state = ENTRY_MEW)


@dp.message_handler(state = ENTRY_ADDRESS)
async def entry_hash(message: types.Message):
    with dp.current_state(chat=message.chat.id, user=message.from_user.id) as state:
        await state.update_data(address2 = message.text)
        await state.set_state(ENTRY_HASH)
    await bot.send_message(message.from_user.id, text=hash_text)

@dp.message_handler(state = ENTRY_HASH)
async def finish_entry(message: types.Message):
    state = dp.current_state(chat=message.chat.id, user=message.from_user.id)
    await state.set_state(None)
    data = await state.get_data()
    sum = data.get('sum')
    address1 = data.get('address1')
    address2 = data.get('address2')
    hash = message.text
    time = datetime.datetime.now()
    result_text = '\n'.join(['UserID: ' + str(message.from_user.id), 'Amount: ' + sum, 'User address: ' + address2,'Hash: '+ hash, 'Time: ' + str(time)])
    #res = 'UserID: ' + str(message.from_user.id), 'Amount: ' + sum, 'User address: ' + address2,'Hash: '+ hash, 'Time: ' + str(time)

    await bot.send_message(message.from_user.id, text=finish_text, reply_markup=markup)
    await bot.send_message(send_result_id, text=result_text)


# request
@dp.message_handler(state = '*', func=lambda message: message.text == view_course_button)
async def view_course(message: types.Message):
    await bot.send_message(message.from_user.id, text=get_course())





if __name__ == '__main__':
    start_polling(dp, loop=loop, skip_updates=True)

0 个答案:

没有答案