繁体   English   中英

如何在 aiogram 上与 aiottp 一起运行电报机器人?

[英]How to run a telegram bot on aiogram together with aiottp?

我在aiogram上编写了一个电报机器人,它为我提供了有关我的帐户market.csgo.com的信息。 脚本的含义很简单——我点击按钮,它会显示文本,然后运行 ​​function。 我的函数发送异步请求并且工作正常,但我不知道如何让aiohttpaiogram一起工作。

from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
from auth import *
import asyncio
import aiohttp


bot = Bot(token=token)
dp = Dispatcher(bot)


def users():
    ***Data of my accounts from txt to dict***

async def get_info(session, dictt, message):
    total_wallet = 0
    async with session.get(f'https://market.csgo.com/api/v2/get-money?key={dictt[1][1]}') as resp:
        html = await resp.json()
        total_wallet += int(html['money'])
    #await bot.send_message(message.from_user.id, f'{total_wallet}')

async def get_on_sale(session, dictt, message):
    sale_total_sum = 0
    async with session.get(f'https://market.csgo.com/api/v2/items?key={dictt[1][1]}') as resp:
        html = await resp.json()
        for i in html['items']:
            sale_total_sum += i['price']
        #await bot.send_message(message.from_user.id, f'{sale_total_sum}')


@dp.message_handler(content_types=['text'])
async def Main():
    try:
        profiles = users()
        async with aiohttp.ClientSession(trust_env=True) as session:
            tasks = [] 
            if message.text == 'info 📊':
                await bot.send_message(message.from_user.id, 'Wait for information..')
                for i in profiles.items():
                    task = asyncio.ensure_future(get_info(session, i))
                    tasks.append(task)
                await asyncio.gather(*tasks)
            if message.text == 'on sale 💰':
                await bot.send_message(message.from_user.id, 'Wait for information..')
                for i in profiles.items():
                    task = asyncio.ensure_future(get_on_sale(session, i))
                    tasks.append(task)
                await asyncio.gather(*tasks)
    except Exception as ex:
        print(f'Error {ex}')


loop = asyncio.get_event_loop()
loop.run_until_complete(Main())

executor.start_polling(dp, skip_updates=True)

我的问题是我不知道如何正确地将message参数传递给Main function

@dp.message_handler(content_types=['text'])
async def Main(): #async def Main(message)

并与 aiohttp 一起运行 aiogram。

loop.run_until_complete(Main()) #loop.run_until_complete(Main(message))

如果我这样做: async def Main(message)loop.run_until_complete(Main(message))然后我得到一个错误:

    loop.run_until_complete(Main(message))
NameError: name 'message' is not defined

或者如果我只使用async def Main(message)得到这个:

    loop.run_until_complete(Main())
TypeError: Main() missing 1 required positional argument: 'message'

解决方案:

async def loop_on(message):
    loop = asyncio.get_event_loop()
    loop.run_until_complete(Main(message))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM