So I've been trying for a long time, I found some pre-made programs, but they costs 100$. I've tried multiple apps and programs like Telegram Auto and Telegram Kit, but they cost a lot and I don't have such money right now.
I am trying to do it in Python and Telethon(Don't have a lot of experience in it)
I already made an app on telegram developer tools, got the API Number and Hash, and found the following code online
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
import csv
api_id = My API ID
api_hash = 'MY API HASH'
phone = 'MY PHONE'
client = TelegramClient(phone, api_id, api_hash)
client.connect()
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("members.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'])
for user in all_participants:
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])
print('Members scraped successfully.')
I entered My information, and I started the program, but I keep getting this error.
Traceback (most recent call last):
File "c:\Users\User\Desktop\export.py", line 23, in
hash = 0
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\telethon\sync.py", line 39, in syncified
return loop.run_until_complete(coro)
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\asyncio\base_events.py", line 579, in run_until_complete
return future.result()
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\telethon\client\users.py", line 64, in call
result = await future
telethon.errors.rpcerrorlist.AuthKeyUnregisteredError: The key is not registered in the system (caused by GetDialogsRequest)
I searched everywhere for a fix or a tutorial and I didn't find anything. My only other choice was coming here.
Please Help.
Regards, Daniel
Admittedly, the docs aren't very clear about what that error means, but from the looks of it, you might be suffering at the hands of an unmanaged resource. The docs themselves suggest here:
The TelegramClient aggregates several mixin classes to provide all the common functionality in a nice, Pythonic interface. Each mixin has its own methods, which you all can use.
In short, to create a client you must run:
from telethon import TelegramClient
client = TelegramClient(name, api_id, api_hash)
async def main():
# Now you can use all client methods listed below, like for example...
await client.send_message('me', 'Hello to myself!')
with client:
client.loop.run_until_complete(main())
You don’t need to import these AuthMethods, MessageMethods, etc. Together they are the TelegramClient and you can access all of their methods.
See Client Reference for a short summary.
Consider using python's with statement to help manage client.
As an aside, did you know that one of the devs who contributed to Telethon has already written a free and open source scraper?
Related
import asyncio
from pyrogram import Client
from pyrogram.errors import PeerFlood
import time
api_id = 12905662
api_hash = "a7cfcd44cb95d26d7529d547c9a1d9ef"
vubor = input('Напишите "1" для парсинга, "2" для рассылки! ')
text = input('Текст для рассылки: ')
account = ['my_accont1', 'my_accont2', 'my_accont3', 'my_accont4', 'my_accont5', 'my_accont6', 'my_accont7',
'my_accont8', 'my_accont9']
async def main():
if vubor == '1':
user = []
with open('username.txt', 'r') as file:
for users in file.readlines():
y = users.strip()
user.append(y)
for acc in account:
try:
async with Client(f"{acc}", api_id, api_hash) as app:
for all_user in user[0:500]:
time.sleep(5)
await app.send_message(all_user, text)
user.remove(all_user)
except PeerFlood:
print('Аккаунт заблокировн')
asyncio.run(main())
pyrogram.errors.exceptions.bad_request_400.PeerIdInvalid: Telegram says: [400 PEER_ID_INVALID] - The peer id being used is invalid or not known yet. Make sure you meet the peer before interacting with it
Firstly, please learn how to ask a good question.
Secondly, your error message is already clear, you have to
Make sure you meet the peer before interacting with it
This means your session has to receive a message from them directly or via a shared chat, or have your session see any update to do with them in general.
I am creating a reference bot for chatting in VK in Python using the Callback Api. The bot works correctly if you write to the group messages. However, if you write to a conversation (to which the bot is added), it replies to private messages. All rights to read, etc. issued. As I understand it (studying information on the internet), I use user_id, not chat_id. But I didn't understand how to fix it correctly (
p.s. It is advisable that the bot write both in private messages and in a conversation, depending on where they ask.
p.p.s perhaps the question seems ridiculous, but I just started to study this area, and I did not find the answer on the net :-)
The bot itself:
import vk
import random
import messageHandler
# app.route ('/', methods = ['POST'])
def processing ():
data = json.loads (request.data)
if 'type' not in data.keys ():
return 'not vk'
if data ['type'] == 'confirmation':
return confirmation_token
elif data ['type'] == 'message_new':
messageHandler.create_answer (data ['object'] ['message'], token)
return 'ok'
"Responder":
import importlib
from command_system import command_list
def load_modules ():
# path from the working directory, it can be changed in the application settings
files = os.listdir ("mysite / commands")
modules = filter (lambda x: x.endswith ('. py'), files)
for m in modules:
importlib.import_module ("commands." + m [0: -3])
def get_answer (body):
# Default message if unrecognizable
message = "Sorry, I don't understand you. Write '/ help' to see my commands."
attachment = ''
for c in command_list:
if body in c.keys:
message, attachment = c.process ()
return message, attachment
def create_answer (data, token):
load_modules ()
user_id = data ['from_id']
message, attachment = get_answer (data ['text']. lower ())
vkapi.send_message (user_id, token, message, attachment)
I don't speak English well, so I apologize for the crooked translation)
Use Peer_id, instead of from_id. (data->object->peer_id)
(i used php, but i had a similar problem. this is the solution)
probably something like this:
def create_answer (data, token):
load_modules ()
user_id = data ['peer_id'] # id source edited
message, attachment = get_answer (data ['text']. lower ())
vkapi.send_message (user_id, token, message, attachment)
from_id - person who sent the message
peer_id - in which dealogue message was received. (for groups it looks like 20000005)
So, you will send the message to conversation (does not matter is this PM or conversation with a lot of people)
Error below:
Failed to convert James Blake - Never Dreamed.mp4 to media. Not an existing file, an HTTP URL or a valid bot-API-like file ID
Function that I am running is here.
async def gramain():
# Getting information about yourself
me = await bott.get_me()
print(me.stringify())
filesww = os.listdir('media').pop()
now = os.getcwd()
mediadir = now + "/media/"
vid = open(mediadir+filesww, 'r')
# await bot.send_message(chat, 'Hello, friend!')
await bott.send_file(chat, filesww, video_note=True)
I trying to use this function from the library Telethon.
https://docs.telethon.dev/en/latest/modules/client.html#telethon.client.uploads.UploadMethods.send_file
I am unable to find out what the issue is. Please help!
I have 10 groups in telegram with different name. I am able to print message with the below example code from telethon library. I also need the group name to be printed.
EG:
G10001 Bhuvan Testing (GroupName UserName/Phoneno Message)
#!/usr/bin/env python3
# A simple script to print some messages.
import os
import sys
import time
from telethon import TelegramClient, events, utils
def get_env(name, message, cast=str):
if name in os.environ:
return os.environ[name]
while True:
value = input(message)
try:
return cast(value)
except ValueError as e:
print(e, file=sys.stderr)
time.sleep(1)
session = os.environ.get('TG_SESSION', 'printer')
api_id = get_env('TG_API_ID', 'Enter your API ID: ', int)
api_hash = get_env('TG_API_HASH', 'Enter your API hash: ')
proxy = None # https://github.com/Anorov/PySocks
# Create and start the client so we can make requests (we don't here)
client = TelegramClient(session, api_id, api_hash, proxy=proxy).start()
# `pattern` is a regex, see https://docs.python.org/3/library/re.html
# Use https://regexone.com/ if you want a more interactive way of learning.
#
# "(?i)" makes it case-insensitive, and | separates "options".
#client.on(events.NewMessage(pattern=r''))#pattern=r'(?i).*\b(hello|hi)\b'))
async def handler(event):
sender = await event.get_sender()
#client.get_input_entity(PeerChannel(fwd.from_id))
#channel = await event.get_channel()
#group = event.group()
#group = event.get_group()
#print(group)
#print(utils.get_peer_id(sender))
#print(utils.get_input_location(sender))
#print(utils.get_input_dialog(sender))
#print(utils.get_inner_text(sender))
#print(utils.get_extension(sender))
#print(utils.get_attributes(sender))
#print(utils.get_input_user(sender))
name = utils.get_display_name(sender)
print(name, 'said', event.text, '!')
#print(utils.get_input_entity(PeerChannel(sender)))
#print(utils.get_input_channel(get_input_peer(channel)))
try:
print('(Press Ctrl+C to stop this)')
client.run_until_disconnected()
finally:
client.disconnect()
# Note: We used try/finally to show it can be done this way, but using:
#
# with client:
# client.run_until_disconnected()
#
# is almost always a better idea.
I also need help to send message to groups. I have gone through some answer given below, which doesn't working for me.
(Sending Telegram messages with Telethon: some entity parameters work, others don't?)
First get the chat from incoming event:
chat = await event.get_chat()
Print group name:
try:
if chat.title:
print(chat.title)
except AttributeError:
print('no such attribute present')
Send message to group:
await client.send_message(entity=chat.id,message='hi')
My name is Ken. I am trying to create a Slack Bot first one message every Monday at 9:00 am and asks for a input (goal number of sales for the week). Then responds with user input in new message saying (great your weekly sales goal is (#). Then it takes this number and adds it to Google sheets. Then asks user to enter daily goal for the day. And responds with (daily goal set as). Then at 9:00 pm asks user to input the number of deals written and keeps track of all data in Google sheets and provides graphs. I have been thinking maybe MySQL would work for that too.
Here is some of the code I have so far:
starterbot.py
import os
import time
from slackclient import slackclient
# starterbot's ID as an environment variable
BOT_ID = os.environ.get("BOT_ID")
# contasts
AT_BOT = "<#" + BOT_ID + ">"
EXAMPLE_COMMAND = "do"
#instantiate Slack & Twilio clients
slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))
def handle_command (command, channel)
"""
Receives commnds directed at the bot and determines if they
are valid commands. if so, then acts on the commands. if not,
returns back what it needs for clarification.
"""
response = "Not sure what you mean. Use the *" + EXAMPLE_COMMAND + \
"* command with numbers, delimited by spaces."
if command.startswitch(EXAMPLE_COMMAND):
response = "Sure ... wire some more c ie then I can do
that!"
slack_client.api_call("chat.postMessage", channel=channel,
text=response, as_user=True)
def parse_slack_output(slack_messages):
"""
The Slack Real Time Messaging API is an event firehose.
this parsing function returns None unless a message is
directed at the Bot, based on its ID.
"""
if slack_messages and len(slack_messages) > 0;
for message in slack_messages:
if message and 'text' in message and AT_BOT in message['text']:
command = message['text'].split(AT_BOT)[1].strip().lower()
channel = message['channel']
# return text after the # mention, white space removed
return command, channel
return None, None
if __name__ == "__main__":
READ_WEBSOCKET_DELAY = 1 # 1 second delay between reading from firehose
if slack_client.rtm_connect():
print("starterbot connected and running!")
while True:
command, channel = parse_slack_output(slack_client.rtm_read())
if command and channel:
handle_command(command, channel)
time.sleep(READ_WEBSOCKET_DELAY)
else:
print("Connection failed. Invalid Slack token or bot ID?")
bot_id_py
import os
from slackclient import SlackClient
import pdb
BOT_NAME = 'pilot4u'
slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))
if __name__ == "__main__":
api_call = slack_client.api_call("users.list")
if api_call.get('members'):
pdb.set_trace()
# Retrieve all users so we can find our bot
users = api_call.get('members')
for user in users:
if 'name' in user and user.get('name') == BOT_NAME:
print("Bot ID for '" + user ['name'] + "'is " +
user.get('id'))
else:
print("could not find bot user with the name " + BOT_NAME)