Telegram bot payments connected to Stripe - stripe-payments

I am trying to make payments through Telegram bots by contacting directly on Telegram. But when I get the "pre_checkout_query" and I answer it (I think correctly), but it gives me error. I leave here the code because I do not know what is wrong.
I have two parts, the first one where I deny the payment simply to be able to test easier if I answer correctly or not and the second one the code that I would use to answer correctly and accept the payment.
I am developing it in Apps Script.
Thanks!
if (contenido.pre_checkout_query != null) {
tg.enviarMensaje(telegramUrl, idAdri, "dentroif", "HTML")
var idoperacion = contenido.pre_checkout_query.id
UrlFetchApp.fetch(telegramUrl + '/answerShippingQuery?shipping_query_id =' + idoperacion + '&ok=False' + "&error_message=Test")
/*var precios = [
{
"label": "Listado Usuarios Karma",
"amount": "145"
},
{
"label": "Listado Usuarios Karma",
"amount": "150"
}
]
var opcionesprecio = [
{
"id": idoperacion,
"title": "Titulo",
"prices": encodeURIComponent(JSON.stringify(precios))
},
]
UrlFetchApp.fetch(telegramUrl + '/answerPreCheckoutQuery?shipping_query_id=' + idoperacion + '&ok=True' + "ShippingOption=" + encodeURIComponent(JSON.stringify(opcionesprecio)))
*/
}
A possible solution to the problem

Related

Calling the Response from Facebook Marketing API in Conditional Statements (Python)

I'm new to Python (using 3.7, no particular reason) and setting up some automated reporting for my company. I've tried several different ways of using the response from the Marketing API. This is my starting point:
def account_name():
#Don't worry, I have all of my credentials as global variables, as I'm setting this up on several accounts.
daily_budget = 200
ad_account_id = 'act_xxxxxxxxxxx'
response = (AdAccount(ad_account_id).get_insights(
fields=fields,
params=params,
))
spend = (AdAccount(ad_account_id).get_insights(
fields=fields2
))
print(response)
The response I get is:
[<AdsInsights> {
"action_values": [
{
"action_type": "omni_add_to_cart",
"value": "10489.9"
},
{
"action_type": "omni_purchase",
"value": "8283.81"
}
],
"actions": [
{
"action_type": "omni_add_to_cart",
"value": "1416"
},
{
"action_type": "omni_purchase",
"value": "288"
}
],
"clicks": "1907",
"cpc": "0.477787",
"cpm": "2.984927",
"ctr": "0.62474",
"date_start": "2020-12-14",
"date_stop": "2020-12-14",
"impressions": "305247",
"purchase_roas": [
{
"action_type": "omni_purchase",
"value": "9.091698"
}
],
"reach": "242920",
"spend": "911.14"
}]
The problem I'm having, is that I need to pull the value for "spend" out of the response, with a conditional to make it trigger a webhook if the amount is too high. Everything I've tried either doesn't work with the data type of the response, or gives me the error "'Cursor' object is not callable". An example of what I'm looking for is something to the effect of:
if 'spend' > daily_budget:
print("too high")
And yes, I know the above snippet is not formatted, and will not work, but that's the idea I'm looking for - just don't know how to make it happen. I'm incredibly new to Python, so I'm really stuck here. Thanks for any help!
Figured it out:
metrics = (AdAccount(ad_account_id).get_insights(
fields=fields,
params=params,
))
for i in metrics:
metrics_data = dict(i)
data = {
"Date:": metrics_data.get("date_start"),
"Spend:": metrics_data.get("spend"),
"Reach:": metrics_data.get("reach"),
"Impressions:": metrics_data.get("impressions"),
"Clicks:": metrics_data.get("clicks"),
"CPC:": metrics_data.get("cpc"),
"CTR:": metrics_data.get("ctr"),
"CPM:": metrics_data.get("cpm"),
"ROAS:": metrics_data.get("purchase_roas")
}
Then I can call the "spend" field specifically.

restricting users on nodejs telegram bot

I currently have a telegram bot giving my organisation users answers to FAQ questions
but I need to restrict it so only certain people that have paid for the mentorship are able to use the product
is there anyway I can ask people to enter a user id number that stores the information
in so if there not on the list in the code there cant access the bot
my bot is currently written in nodejs language (based on this repo).
[
{
"triggers": [
"hello",
"hi"
],
"replies": [
{
"reply": "Hi Im your Personal Lion Bot \r\n\r\nWhat do you want to focus on today\r\n\r\n>Trading Help\r\n\r\n>Business Help\r\n\r\n>Mindset\r\n\r\n>Schedule\r\n\r\n>Just Started\r\n\r\n>Help",
"type": "text"
}
]
},
{
"triggers": [
"trading",
"Trading"
],
"replies": [
{
"reply": "Ok you want to focus on trading\r\n\r\nHeres is what I can show you\r\n\r\n>Our Schedule\r\n\r\n>A Trading Gameplan\r\n\r\n>Trade Ideas\r\n\r\n>Trading Products",
"type": "text"
}
]
}
]
You need to know your paying users' ID, then you answer their messages only if msg.from.id is in your list of allowed users.
Something like:
function check_if_allowed(id) {
...
}
bot.on('message', (msg) => {
if (check_if_allowed(msg.from.id)) {
...
}
})
To find their ID depends on how you register them in your organization.
Add more details and code if you need further help

How to parse json when you have 'dynamic' values

Thanks for taking the time out to read this. I want to find a way of parsing the json below. I'm really struggling to get the correct values out. I am getting this info from an API, and want to save this data into a database.
I am really struggling to parse info_per_type because I first need to get the available_types. This can change depending on the info available (i.e. I might get 2 different types in the next call, there's a total of 4) so my code needs to be flexible enough to deal with this
```
{
"data": [
{
"home_team": "Ravenna",
"id": 82676,
"available_types": [
"type_a",
"type_b"
],
"info_per_type": {
"type_a": {
"options": {
"X": 0.302,
"X2": 0.61,
"X3": 0.692,
"X4": 0.698,
"X5": 0.39,
"X6": 0.308
},
"status": "pending",
"output": "12",
"option_values": {
"X": 3.026,
"X2": 1.347,
"X3": 1.516,
"X4": 1.316,
"X5": 2.936,
"X6": 2.339
}
},
"type_b": {
"options": {
"yes": 0.428,
"no": 0.572
},
"status": "pending",
"output": "no",
"option_values": {
"yes": null,
"no": null
}
}
}
}
]
}```
So far, I can get the available_types out. But after that, I'm stuck. I have tried eval and exec but I can't seem to get that working either.
```
r = requests.get(url, headers=headers).text
arrDetails = json.loads(r)
arrDetails = arrDetails['data']
x = arrDetails[0]['available_types']
print(x[1]) #I get the correct value here
y = exec("y = arrDetails[0]['info_per_type']['" + x[1] + "']")
print(y)```
When I print out y I get None. What I want is some way to reference that part of the json file, as the results within that node are what I need. Any help would be HIGHLY appreciated!
Something like this should work :
for row in arrDetails['data']:
for available_type in row['available_types']:
print(row['info_per_type'][available_type])

How to search for specific string but only return 1 email per email address?

Does the gmail API allow for a way to search for a string and return all the mail that has this string but limit the list to 1 email per email address.
For example, I want to find all mail that contain a certain username but the list only contains one email per address.
There is no support for these types of complex queries with the Gmail API, sadly.
Something along these lines will get it done though:
List messages with your search string as the q-parameter until there is no nextPageToken in the response (then you will have gotten every result of the query).
Get all matching messages, only asking for the From-header, and find all unique senders yourself (batch requests would work great for this use case).
Example
List message ids
q = feel the bern
fields = messages(id),nextPageToken
GET https://www.googleapis.com/gmail/v1/users/me/messages?q=feel+the+bern&fields=messages(id)%2CnextPageToken&access_token={YOUR_API_KEY}
Response
{
"messages": [
{
"id": "151c1de994ad5e43"
},
{
"id": "1514f614e6c07c3f"
}, ...
]
}
Get every message (in batch)
format = metadata
metadataHeaders = from
fields = id,payload/headers
GET https://www.googleapis.com/gmail/v1/users/me/messages/151c1de994ad5e43?format=metadata&metadataHeaders=from&fields=id%2Cpayload%2Fheaders&access_token={YOUR_API_KEY}
Response
{
"id": "151c1de994ad5e43",
"payload": {
"headers": [
{
"name": "From",
"value": "Official Bernie Store <info#berniesanders.com>"
}
]
}
},
{
"id": "151c1de994ad3e22",
"payload": {
"headers": [
{
"name": "From",
"value": "Emil Tholin <emtholin#gmail.com>"
}
]
}
}, ...
Then you can just filter messages out by email address, and then get the entire mail of the ones that are left.

Insert a card that can be directly responded to

update !important: The API has changed a lot, this question shouldn't be taken into consideration anymore
I am trying to use the REST api (via Node.js API) to create cards that the user can respond to and create an interaction in this way.
Reading the docs the creator attribute is not really specified anywhere, so I have no idea how to insert that.
Also this video doesn't help. Nor this guide =)
I believe there is an URL I should set as callback somehow? I'd like to know how to get these responses, please.
update
This is the card I am sending.
{
bundleId: 'veryuniqueBundle',
id: 'veryuniqueBundle:reply',
text: "want to hear moar?",
menuItems: [
{action: "REPLY"}
]
}
that's the response I get:
{
"collection": "timeline",
"itemId": "119c4dc8-c0ce-4a83-aa76-41aab4e8dbe1",
"operation": "INSERT",
"verifyToken": "42",
"userToken": "id:520ef63cde31145deb000001",
"userActions": [
{
"type": "REPLY"
}
]
}
The problem is, I can't see what the user responded (an text) and the reference to the original card id (or bundle) that was responded to. How can I get those
Cards do not provide a direct callback. Instead, when a user selects a menu item it causes the card to be updated with their menu selection. This change subsequently triggers a notification ping to your timeline subscription.
Follow these steps to detect a menu item selection:
Subscribe to notifications for changes in the timeline collection
{
"collection": "timeline",
"userToken": "awesome_kitty",
"verifyToken": "random_hash_to_verify_referer",
}
Insert a timeline card with a custom menu item
{
"text": "Hello world",
"menuItems": [
{
"action": "CUSTOM",
"id": "complete"
"values": [{
"displayName": "Complete",
"iconUrl": "http://example.com/icons/complete.png"
}]
}
]
}
Select the item on Glass
Receive the notification on your subscription URL
{
"collection": "timeline",
"itemId": "3hidvm0xez6r8_dacdb3103b8b604_h8rpllg",
"operation": "UPDATE",
"userToken": "harold_penguin",
"userActions": [
{
"type": "CUSTOM",
"payload": "PING"
}
]
}
Do cool stuff in your code
???
Profit

Resources