Hi I'm trying to leave a channel with id with this script but it not showing any results well LeaveChannelRequest doesn't worked for me too
here is what i tried:
import asyncio
import logging
import re
import time
import os
import sys
import requests
logging.basicConfig(level=logging.ERROR)
from telethon import TelegramClient, events
from telethon.tl.functions.channels import LeaveChannelRequest
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.tl.functions.messages import GetBotCallbackAnswerRequest
from datetime import datetime
from colorama import Fore, init as color_ama
color_ama(autoreset=True)
os.system('cls' if os.name=='nt' else 'clear')
# my.telegram.org values, get your own there
api_id = ''
api_hash = ''
input_channel = '-1001226909513'
def print_msg_time(message):
print('[' + Fore.YELLOW + f'{datetime.now().strftime("%H:%M:%S")}' + Fore.RESET + f'] {message}')
async def main():
if len(sys.argv) < 2:
print('Usage: python start.py phone_number')
print('-> Input number in international format (example: +10123456789)\n')
e = input('Press any key to exit...')
exit(1)
phone_number = sys.argv[1]
if not os.path.exists("session"):
os.mkdir("session")
client = TelegramClient('session/' + phone_number, api_id, api_hash)
await client.start(phone_number)
me = await client.get_me()
print(Fore.GREEN + f' Current account: {me.first_name}({me.username})\n' + Fore.RESET)
print_msg_time('leaving channels')
async def main():
await client.delete_dialog(input_channel)
print_msg_time(f'{input_channel} has been leaved')
asyncio.get_event_loop().run_until_complete(main())
where is my problem?
The method call to leave the channel or group is defined in inner function
[...]
print_msg_time('leaving channels')
async def main():
await client.delete_dialog(input_channel)
print_msg_time(f'{input_channel} has been leaved')
which isn't called in outer function, you could avoid using a inner function. for example, you can call delete_dialog method like this
[...]
print_msg_time('leaving channels')
await client.delete_dialog(input_channel)
print_msg_time(f'{input_channel} has been leaved')
Related
i tried to create a telegram bot in aiogram to send messages if a particular share price is reached. im using threading in order to run this in background as the bot takes user inputs. basically it takes a dict and takes the key as ticker for yfinance and gets the current price and compares with the given condition and if its a true condition it will send the alert to a chat. this is the code
#bot info
import yfinance as yf
import logging
logging.basicConfig(level=logging.INFO)
from aiogram import Bot, Dispatcher, executor
import asyncio
import threading
bot = Bot(token='bot api key')
dp = Dispatcher(bot)
sh={}
jk={}
#reads a dict from a text file
with open('shares.txt','r') as f:
a=f.read()
b=a.rstrip('}').lstrip('{').split(',')
for i in b:
try:
c=i.split(':')
com_name=c[0].strip().strip(''' ' ''').strip()
com_price=float(c[1])
sh[com_name]=com_price
except:
sh={}
break
async def send_price(pp: str):
await bot.send_message(chat_id=user chat id, text=str(pp))
#price updater
def pp():
while True:
pl = sh.copy()
index = ''
for keys, values in list(pl.items()):
if keys in jk and pl[keys] == jk[keys]:
del pl[keys]
print(pl)
kj = pl
if len(kj)==0:
break
for i in kj:
if kj[i]==0:
break
s = yf.Ticker(i)
a = (s.info)['currentPrice']
# print(s,a)
if kj[i] == 0:
continue
# print('kj',kj[i])
if kj[i] <= a:
index += (f'{i} is currently up rn up with current price being {str(a)} \n')
jk[i] = kj[i]
#print(index)
if len(index) != 0:
asyncio.run(send_price(index))
threading.Thread(target=pp).start()
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)
when i run the code i get this error .
RuntimeError: Timeout context manager should be used inside a task
So i found this piece of code somewhere on stackoverflow:
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty, InputPeerChannel, InputPeerUser
from telethon.errors.rpcerrorlist import PeerFloodError, UserPrivacyRestrictedError
from telethon.tl.functions.channels import InviteToChannelRequest
import sys
import csv
import traceback
import time
from datetime import datetime
api_id = 123456789 #Enter Your 7 Digit Telegram API ID.
api_hash = '123456789' #Enter Yor 32 Character API Hash.
phone = '123456789'
client = TelegramClient(phone, api_id, api_hash)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
client.sign_in(phone, input('Enter the code recieved to your Telegram messenger: '))
chats = []
last_date = None
chunk_size = 200
groups=[]
result = client(GetDialogsRequest(
offset_date=last_date,
offset_id=0,
offset_peer=InputPeerEmpty(),
limit=chunk_size,
hash = 0
))
chats.extend(result.chats)
for chat in chats:
try:
if chat.megagroup== True:
groups.append(chat)
except:
continue
print('Choose a group to scrape members from:')
i=0
for g in groups:
print(str(i) + '- ' + g.title)
i+=1
g_index = input("Enter a Number: ")
target_group=groups[int(g_index)]
print('Fetching Members...')
all_participants = []
all_participants = client.get_participants(target_group, aggressive=True)
print('Saving In file...')
with open("Scraped.csv","w",encoding='UTF-8') as f:
writer = csv.writer(f,delimiter=",",lineterminator="\n")
writer.writerow(['username','user id', 'access hash','name','group', 'group id','last seen'])
for user in all_participants:
accept=True
try:
lastDate=user.status.was_online
num_months = (datetime.now().year - lastDate.year) * 12 + (datetime.now().month - lastDate.month)
if(num_months>1):
accept=False
except:
continue
if (accept) :
if user.username:
username= user.username
else:
username= ""
if user.first_name:
first_name= user.first_name
else:
first_name= ""
if user.last_name:
last_name= user.last_name
else:
last_name= ""
name= (first_name + ' ' + last_name).strip()
writer.writerow([username,user.id,user.access_hash,name,target_group.title, target_group.id,user.status])
print('Members scraped successfully.')
And this pretty much scrapes online & recently active members, how would i change this to ONLY scrape online members? I tried looking into the telethon docs but i don't seem to understand...
I'm not sure where else to ask for help regarding this issue so here i am...
Any sort of help is highly appreciated!
Thank you.
Scraping ONLY online members is not possible.
Try something like this:
import telethon
from telethon.sync import TelegramClient, events
from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.types import InputChannel
from telethon.tl.types import ChannelParticipantsSearch
from telethon.tl.functions.channels import GetFullChannelRequest
api_id = 123567
api_hash = "dsaopdas2131"
client = TelegramClient("RDJR", api_id, api_hash)
#client.on(events.NewMessage())
async def handler(event):
chat_id = event.message.peer_id.channel_id
offset = 0
limit = 200
my_filter = ChannelParticipantsSearch('')
channel = await client(GetFullChannelRequest(chat_id))
participants = await client(GetParticipantsRequest(channel=chat_id, filter=my_filter, offset=offset, limit=limit, hash=0))
for x in participants.users:
print(x.status)
with client as client:
print("USER_BOT ONLINE!")
client.run_until_disconnected()
You need to get all of the participants of a group/channel, then iterate them to print the status.
The output would be something like this:
User online:
UserStatusOnline(expires=datetime.datetime(2021, 7, 6, 21, 6, 22, tzinfo=datetime.timezone.utc))
Or:
None
User offline:
UserStatusOffline(was_online=datetime.datetime(2021, 7, 6, 18, 19, 35, tzinfo=datetime.timezone.utc))
I have some code:
master = InlineKeyboardMarkup()
master.add(InlineKeyboardButton(text='7(800)555-35-35', url='tel:+78005553535'),
InlineKeyboardButton(text='8(800)555-35-35', url='tel:+88005553535'))
But when I try to summon this keyboard I have an error:
aiogram.utils.exceptions.BadRequest: Wrong http url
You need to write the code as follows:
main.py
from aiogram
import Bot, Dispatcher, executor, types
import keyboards as kb
bot = Bot(token = 'BOT_TOKEN')
dp = Dispatcher(bot)
#dp.message_handler(commands = ['inline'])
async def show_items(message: types.Message):
await message.answer('It is buttons', reply_markup = kb.start_keyboard)
if __name__ == '__main__':
executor.start_polling(dp, skip_updates = True)
keybords.py
from aiogram.types
import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton
inline_keyboard = InlineKeyboardButton(text='7(800)555-35-35', url = 'tel:+78005553535'),
InlineKeyboardButton(text='8(800)555-35-35', url = 'tel:+88005553535')
start_keyboard = ReplyKeyboardMarkup(resize_keyboard = True).add(inline_keyboard)
I need help with this code.
A month ago, I modified it to fit with me.
since three days, I suffer from an error that I could not fix .. He used to work before and now no
Thank you so much.
This is the source of the project
I have just modified the previous mistakes
I'm a beginner with only a month and a half experience
https://github.com/P-Alban/Telegram-collector
import sys
from getpass import getpass
from time import sleep
from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
from telethon.errors import UsernameNotOccupiedError
from telethon.errors import FloodWaitError
from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.functions.contacts import ResolveUsernameRequest
from telethon.tl.types import ChannelParticipantsSearch, InputChannel
# First you need create app on https://my.telegram.org
api_id = api_id
api_hash = 'api_hash'
phone = '+....Phone'
limit = 100
def get_chat_info(username, client):
try:
chat = client(ResolveUsernameRequest(username))
except UsernameNotOccupiedError:
print('Chat/channel not found!')
sys.exit()
result = {
'chat_id': chat.peer.channel_id,
'access_hash': chat.chats[0].access_hash
}
return result
def dump_users(chat, client):
counter = 0
offset = 0
chat_object = InputChannel(chat['chat_id'], chat['access_hash'])
all_participants = []
print('Process...')
while True:
participants = client.invoke(GetParticipantsRequest(
chat_object, ChannelParticipantsSearch(''), offset, limit
))
if not participants.users:
break
all_participants.extend(['{} {}'.format(x.id, x.username)
for x in participants.users])
users_count = len(participants.users)
offset += users_count
counter += users_count
print('{} users collected'.format(counter))
sleep(2)
with open('users.txt', 'w') as file:
file.write('\n'.join(map(str, all_participants)))
def main():
channel_name = input('Input a channel name, without "#": ')
client = TelegramClient(phone, api_id, api_hash)
print('Connecting...')
client.connect()
if not client.is_user_authorized():
try:
client.send_code_request(phone)
print('Sending a code...')
client.sign_in(phone, code=input('Enter code: '))
print('Successfully!')
except FloodWaitError as FloodError:
print('Flood wait: {}.'.format(FloodError))
sys.exit()
except SessionPasswordNeededError:
client.sign_in(password=getpass('Enter password: '))
print('Successfully!')
dump_users(get_chat_info(channel_name, client), client)
print('Done!')
if __name__ == '__main__':
main()
I tried to learn how asnychron programming in python works and wrote a small tornado app which executues two asnyc loops with sleep commands.
If I wait for both coroutines with two await commands, it behaves as expected (first loop, than the second loop is executed.)
If I combine both coroutines with gather, nothing happens. (No errors, no print output, the webrequest is never finished.)
I don't understand what is happening with *await gather(coros, return_exceptions=True),
from asyncio import gather
import os.path
import tornado.ioloop
from tornado.options import define, options, parse_command_line
import tornado.web
import tornado.platform.asyncio
from tornado.gen import sleep
import datetime;
define("port", default=8888, help="run on the given port", type=int)
define("debug", default=False, help="run in debug mode")
class AsyncTestHandler(tornado.web.RequestHandler):
async def get(self):
operation = self.get_query_argument('operation')
if operation == 'with_two_waits':
await self._with_two_waits()
elif operation == 'with_gather':
await self._with_gather()
else:
self.finish('use operation=with_two_waits or operation=with_gather')
return
self.finish('finished ' + operation);
async def _with_two_waits(self):
print('_with_two_waits: start' + str(datetime.datetime.now()) )
w1 = self._wait_loop("First loop", 8)
w2 = self._wait_loop("Second loop", 6)
await w1
await w2
print('_with_two_waits: finished' + str(datetime.datetime.now()))
async def _with_gather(self):
print('_with_gather: start' + str(datetime.datetime.now()))
coros = []
coros.append(self._wait_loop("First loop", 8))
coros.append(self._wait_loop("Second loop", 6))
await gather(*coros, return_exceptions=True)
print ('_with_gather: finished' + str(datetime.datetime.now()))
async def _wait_loop(self, loop_name, count):
for i in range(1, count + 1):
print(loop_name + ' ' + str(i) + '/' + str(count) + ' ' + str(datetime.datetime.now()))
await sleep(0.1)
print(loop_name + ' complete')
def start_web_app():
parse_command_line()
app = tornado.web.Application(
[
(r"/asnycTest", AsyncTestHandler),
],
debug=options.debug,
)
app.listen(options.port)
tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
start_web_app()
gather uses the asyncio event loop. If you wish to mix asyncio with tornado, you need to install tornado's asyncio event loop:
Add this to your imports:
from tornado.platform.asyncio import AsyncIOMainLoop
Remove this:
import tornado.platform.asyncio
And add this line right after the imports:
AsyncIOMainLoop().install()