I'm experimenting with using user inputs and Twilio to create a "new messaging platform". (It sounds ridiculous, I know, but I mainly want to see if this would work.) Anyway, when I am running my python code, it throws the error call() got an unexpected keyword argument 'body'. I don't know if this is my formatting or something else, but it's really annoying because I'm pretty close to being finished. Here's my code:
account_sid = 'AC4b7b29794774f13edbaeb19121730dbb'
auth_token = '---'
client = Client(account_sid, auth_token)
def sendText():
myNum = '+19737848243'
num = input('Enter your sender\'s phone number here: ')
text = input('Enter your message here: ')
myNum = str(myNum)
text = str(text)
num = str(num)
message = client.messages(
body=text,
from_=myNum,
to=num
)
print(message.sid)
sendText()
(BTW I'm not showing my auth_token on this post, so that's not the error. Trust me.)
Any help would be gladly appreciated. Thanks!
Just so this shows as answered, converting my comment:
You're supposed to call the create method of messages, not messages itself:
client.messages.create(body=text,
from_=myNum,
to=num)
messages appears to be callable, but what it's supposed to be used for is not made clear in the documentation, and clearly it doesn't take the args you were passing.
Related
My code is as follows:
from pyokx import OKXClient, Account, Trade, Marketdata
# create the base client:
client = OKXClient(
key = 'KEY',
secret = 'SECRET',
passphrase = 'PASS'
)
market = Marketdata(client)
okx_ticker = market.get_ticker("BTC-USDT")
print(okx_ticker)
This prints APIReturn(response=<Response [200]>) when I should be getting the response detailed here(docs).
I haven't tried anything as I'm unsure where to begin. I'm not sure what I'm doing wrong, is anyone able to clarify?
Thanks to John Gordon's comments I was able to find the data I wanted using:
okx_ticker.response.content
I am trying to send an email using the smtplib library. It works fine, but if my message includes a fixed string + variable, it doesn't work.
Here is my code:
destinationEmail = 'destination#gmail.com'
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login("me#gmail.com", "password")
message = f"I am sending this variable: {variableName}"
server.sendmail("me#gmail.com", destinationEmail, message)
Using the above code does not work. I've tried using this format for the message as well:
message = "I am sending this variable: " + variableName
It also does not work. If my message has no variables, it sends successfully, like:
message = "testing"
Another thing I've tried is sending the variable alone, like:
message = variableName
And that works! So I'm really confused what is wrong here. It seems like it doesn't wanna send messages that has a fixed string + variable. BTW the variable I am using is of type string. Even when I use str( ) around the variable, it doesn't work.
This is usually a version problem, but you can always substitute your f-string for message = "I am sending this variable: {}".format(variable_name) which worked for me.
The answer is here: How to send variable value via email with smtplib in python3?
Use MIMEText to construct the message (with 'subject' 'from' and 'to'). #tax evader was right about the subject thing. I guess it requires a subject.
I would like to forward every updates from channels to my bot.
Is it Possible with ForwardMessagesRequest ?
I tried to use this Telethon example to build my personal code:
https://github.com/LonamiWebs/Telethon/wiki/Forwarding-messages
But i wasn't able to do it. And i don't know if it's possible to use that part of code inside a callback function. Someone can help me? Thank you
Ok, i'm really confused so let's go back.
In this code I just try to retrieve last messages from an user chat and forward them to my bot:
def callback(update):
source_chat_id = "here i put the user id"
source_hash = "here i put his access_hash"
source_chat = InputPeerUser(source_chat_id, source_hash)
total_count, messages, senders = client.get_message_history(
source_chat, limit=10)
for msg in reversed(messages):
print ("msg:", msg.id, msg)
msg = messages[0]
print ("msg id:", msg.id)
dest_chat = "here i tried to put the number of my bot ID"
result = client.invoke(ForwardMessagesRequest(from_peer=source_chat, id=[msg.id], random_id=[generate_random_long()], to_peer=dest_chat))
client.add_update_handler(callback)
The print was correct but i didn't receive anything to my bot chat.
I know that there will be a lot of errors so please be patient and sorry.
I'd like to start off by saying that I love this tool and the API is written in a very easy to follow way if you are familiar with Zap. The only troubles I've had is that I can't find much documentation on the python API, so I've gone off of the source code and verifying how it works against the app. I've been able to pull of scans and set contexts, but I can't seem to be able to correctly call anything from the authentication module. One of my problems, I believe, is that I'm not entirely sure the exact variables to use or their respective formats when calling the functions. below is some example code that I've scrapped together. Every use of the authentication functions below fail me. Even if someone were to look at this and tell me where to go or look to solve this problem myself, I would be very grateful.
from zapv2 import ZAPv2
context = 'new_attack'
authmethodname = 'formBasedAuthentication'
authmethodconfigparams = "".join('loginUrl=someloginpage' 'loginRequestData=username%3D%7B%25user1%25%7D%26' 'password%3D%7B%25password%25%7D')
target = 'some target but I cant put more than 2 links in this question'
apikey = 'password'
zap = ZAPv2(apikey=apikey)
print zap.context.new_context('new_attack')
print zap.context.include_in_context(context, 'https://192.168.0.1.*')
print zap.context.context(context)
#anything below here gives me 'Missing Params' an error from zap
print zap.authentication.set_logged_in_indicator(context, loggedinindicatorregex='Logged in')
print zap.authentication.set_logged_out_indicator(context, 'Sorry, the username or password you entered is incorrect')
print zap.authentication.set_authentication_method(context, authmethodname, authmethodconfigparams)
A Dev member on the project was able to answer my question so I thought I would put it here as well. Essentially the authentication functions take the contextid and userid as parameters and I was passing the context name and user name. There are a few other mistakes that I interpreted from the source code as well. Hopefully this helps someone else who's starting out with the API as well, since there is not a lot of documentation.
from github page zaproxy; username thc202 - "
from zapv2 import ZAPv2
context = 'new_attack'
authmethodname = 'formBasedAuthentication'
authmethodconfigparams = "".join('loginUrl=https://192.168.0.1/dologin.html' '&loginRequestData=username%3D%7B%25username%25%7D%26' 'password%3D%7B%25password%25%7D')
target = 'https://192.168.0.1'
apikey = 'password'
zap = ZAPv2(proxies={'http': 'http://127.0.0.1:8119', 'https': 'http://127.0.0.1:8119'}, apikey=apikey)
contextid = zap.context.new_context(context)
print contextid
print zap.context.include_in_context(context, 'https://192.168.0.1.*')
print zap.context.context(context)
print zap.authentication.set_authentication_method(contextid, authmethodname, authmethodconfigparams)
# The indicators should be set after setting the authentication method.
print zap.authentication.set_logged_in_indicator(contextid, loggedinindicatorregex='Logged in')
print zap.authentication.set_logged_out_indicator(contextid, 'Sorry, the username or password you entered is incorrect')
userid = zap.users.new_user(contextid, 'User 1')
print userid
print zap.users.set_authentication_credentials(contextid, userid, 'username=MyUserName&password=MySecretPassword')
print zap.users.set_user_enabled(contextid, userid, True)
print zap.spider.scan_as_user(contextid, userid, target)
"
I am extremely new to python , scripting and APIs, well I am just learning. I came across a very cool code which uses facebook api to reply for birthday wishes.
I will add my questions, I will number it so that it will be easier for someone else later too. I hope this question will clear lots of newbies doubts.
1) Talking about APIs, in what format are the usually in? is it a library file which we need to dowload and later import? for instance, twitter API, we need to import twitter ?
Here is the code :
import requests
import json
AFTER = 1353233754
TOKEN = ' <insert token here> '
def get_posts():
"""Returns dictionary of id, first names of people who posted on my wall
between start and end time"""
query = ("SELECT post_id, actor_id, message FROM stream WHERE "
"filter_key = 'others' AND source_id = me() AND "
"created_time > 1353233754 LIMIT 200")
payload = {'q': query, 'access_token': TOKEN}
r = requests.get('https://graph.facebook.com/fql', params=payload)
result = json.loads(r.text)
return result['data']
def commentall(wallposts):
"""Comments thank you on all posts"""
#TODO convert to batch request later
for wallpost in wallposts:
r = requests.get('https://graph.facebook.com/%s' %
wallpost['actor_id'])
url = 'https://graph.facebook.com/%s/comments' % wallpost['post_id']
user = json.loads(r.text)
message = 'Thanks %s :)' % user['first_name']
payload = {'access_token': TOKEN, 'message': message}
s = requests.post(url, data=payload)
print "Wall post %s done" % wallpost['post_id']
if __name__ == '__main__':
commentall(get_posts())`
Questions:
importing json--> why is json imported here? to give a structured reply?
What is the 'AFTER' and the empty variable 'TOKEN' here?
what is the variable 'query' and 'payload' inside get_post() function?
Precisely explain almost what each methods and functions do.
I know I am extremely naive, but this could be a good start. A little hint, I can carry on.
If not going to explain the code, which is pretty boring, I understand, please tell me how to link to APIs after a code is written, meaning how does a script written communicate with the desired API.
This is not my code, I copied it from a source.
json is needed to access the web service and interpret the data that is sent via HTTP.
The 'AFTER' variable is supposed to get used to assume all posts after this certain timestamp are birthday wishes.
To make the program work, you need a token which you can obtain from Graph API Explorer with the appropriate permissions.